udp&socket的问题!一天半没搞定!

freeleo 2003-04-28 06:51:51
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
private string server="127.0.0.1";
private Int32 port = 80;
private string message="GET / HTTP/1.1\r\nHost:127.0.0.1\r\nConnection: Close\r\n\r\n";//Hello, Net World!!!";
private Socket s;
private IPEndPoint ipep;
private Byte[] rvbuf=new Byte[1024];
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
new Class1().Start();
}
public void Start()
{
IPHostEntry iphe=Dns.Resolve(server);
ipep=new IPEndPoint(iphe.AddressList[0],port);
s=new Socket(ipep.AddressFamily,SocketType.Dgram,ProtocolType.Udp);
s.Connect(ipep);
Console.WriteLine("Connected!");
new Thread(new ThreadStart(this.Receive)).Start();
for(int i=0;i<10000;i++)
{
}
new Thread(new ThreadStart(this.Send)).Start();
Console.Read();


}
public void Receive()
{
try
{
byte[] d=new byte[1024];
s.BeginReceive(rvbuf,0,1024,SocketFlags.None,new AsyncCallback(Received),0);
}
catch(SocketException e)
{
Console.WriteLine(e.ToString());
}
}
public void Received(IAsyncResult iar)
{
/* for(int i=0;i<rvbuf.Length;i++)
{
Console.Write(rvbuf[i]);
}
*/
Console.WriteLine(Encoding.ASCII.GetString(rvbuf));
Console.WriteLine("\nReceived!");
}
public void Send()
{
try
{
s.Send(Encoding.ASCII.GetBytes(message),SocketFlags.None);
Console.WriteLine(message+" Sended!");
}
catch(SocketException e)
{
Console.WriteLine(e.ToString());
}

}
/* [STAThread]
static void Main(string[] args)
{
UdpClient udpClient = new UdpClient();
try
{
udpClient.Connect("localhost", 11000);

// Sends a message to the host to which you have connected.
Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");

udpClient.Send(sendBytes, sendBytes.Length);

// Sends a message to a different host using optional hostname and port parameters.
UdpClient udpClientB = new UdpClient();
udpClientB.Send(sendBytes, sendBytes.Length,"localhost", 11000);

//IPEndPoint object will allow us to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

// Blocks until a message returns on this socket from a remote host.
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);

// Uses the IPEndPoint object to determine which of these two hosts responded.
Console.WriteLine("This is the message you received " +
returnData.ToString());
Console.WriteLine("This message was sent from " +
RemoteIpEndPoint.Address.ToString() +
" on their port number " +
RemoteIpEndPoint.Port.ToString());

udpClient.Close();
udpClientB.Close();

}
catch (Exception e )
{
Console.WriteLine(e.ToString());
}
Console.Read();
}
*/
}

}

里面是两个程序,都搞不定了,弟兄姊妹们,帮忙!!!
...全文
52 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gbl777 2003-05-04
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/1717/1717922.xml?temp=.5142633
lczddd 2003-05-02
  • 打赏
  • 举报
回复
我顶!!
gbl777 2003-04-28
  • 打赏
  • 举报
回复
嗨,我也是类示问题,接受端问题,我都出了150分了,都没人来解决呀!

版主是不是回丈母娘家啦?:)
freeleo 2003-04-28
  • 打赏
  • 举报
回复
都在接收的地方出现问题

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧