首页上一页 1 下一页尾页 1 条记录 1/1页
《C#从入门到精通》中例24.13关于UdpClient的问题!?
发表在C#图书答疑
2012-03-15
是否精华
是
否
版块置顶:
是
否
作者原程序写的聊天程序的一个公共类如下:
public class Class1
{
public Class1()
{
}
//设置端口号
public const int port = 11000;
public void StartListener()
{
UdpClient udpclient = new UdpClient(port);
//将网络端点表示为IP地址和端口号
IPEndPoint ipendpoint = new IPEndPoint(IPAddress.Any, port);
try
{
while (true)
{
byte[] bytes = udpclient.Receive(ref ipendpoint);
string strIP = "信息来自" + ipendpoint.Address.ToString();
string strInfo = Encoding.GetEncoding("gb2312").GetString(bytes, 0, bytes.Length);
MessageBox.Show(strInfo, strIP);
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
udpclient.Close();
}
}
public string Send(string strServer,string strContent)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//将输入的字符串转换为IP地址
IPAddress ipaddress = IPAddress.Parse(strServer);
//将发送的内容存储到byte数组中
byte[] btContent = Encoding.GetEncoding("gb2312").GetBytes(strContent);
IPEndPoint ipendpoint = new IPEndPoint(ipaddress, 11000);
socket.SendTo(btContent, ipendpoint);
socket.Close();
return "发送成功";
}
}
为什么我把public string Send(string strServer,string strContent)里面改为:
public void Send(string strServer, string strContent)
{
UdpClient udpclient = new UdpClient();
udpclient.Connect(strServer, 11000);
byte[] btContent = Encoding.GetEncoding("gb2312").GetBytes(strContent);
udpclient.Send(btContent, btContent.Length);
udpclient.Close();
}
程序运行时总是卡在udpclient.connect那里了,说是“向一个无法连接的网络尝试了一个套接字操作”,怎么回事啊?为什么不能用udpclient建立连接并send数据呢?只能用socket方式?
public class Class1
{
public Class1()
{
}
//设置端口号
public const int port = 11000;
public void StartListener()
{
UdpClient udpclient = new UdpClient(port);
//将网络端点表示为IP地址和端口号
IPEndPoint ipendpoint = new IPEndPoint(IPAddress.Any, port);
try
{
while (true)
{
byte[] bytes = udpclient.Receive(ref ipendpoint);
string strIP = "信息来自" + ipendpoint.Address.ToString();
string strInfo = Encoding.GetEncoding("gb2312").GetString(bytes, 0, bytes.Length);
MessageBox.Show(strInfo, strIP);
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
udpclient.Close();
}
}
public string Send(string strServer,string strContent)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//将输入的字符串转换为IP地址
IPAddress ipaddress = IPAddress.Parse(strServer);
//将发送的内容存储到byte数组中
byte[] btContent = Encoding.GetEncoding("gb2312").GetBytes(strContent);
IPEndPoint ipendpoint = new IPEndPoint(ipaddress, 11000);
socket.SendTo(btContent, ipendpoint);
socket.Close();
return "发送成功";
}
}
为什么我把public string Send(string strServer,string strContent)里面改为:
public void Send(string strServer, string strContent)
{
UdpClient udpclient = new UdpClient();
udpclient.Connect(strServer, 11000);
byte[] btContent = Encoding.GetEncoding("gb2312").GetBytes(strContent);
udpclient.Send(btContent, btContent.Length);
udpclient.Close();
}
程序运行时总是卡在udpclient.connect那里了,说是“向一个无法连接的网络尝试了一个套接字操作”,怎么回事啊?为什么不能用udpclient建立连接并send数据呢?只能用socket方式?