关于网络侦听,高手请进 看一下System.Net.Sockets.UdpClient的内容,发送、接收。广播,只需要向广播地址发送消息就可以了。如果不知道广播地址,请翻一下TCP/IP的书。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以给一个server和client端的例程吗?测试通过立刻给分 //服务器端public void HandleThread() { Socket ClientSocket=new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); IPHostEntry ipHostInfo = Dns.GetHostByAddress("192.168.11.110"); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint ipe = new IPEndPoint(ipAddress,2300); System.Net.Sockets.TcpListener ClientSocket = new System.Net.Sockets.TcpListener(ipAddress,2300); ClientSocket.Start(); System.Net.Sockets.TcpClient mySocket; while (true) { Application.DoEvents();//同时接受其他消息 mySocket=ClientSocket.AcceptTcpClient(); try { ReviceMsg(mySocket); mySocket.Close(); } catch(Exception Err) { mySocket.Close(); System.Windows.Forms.MessageBox.Show(Err.ToString()); } } } //接受消息 private void ReviceMsg(System.Net.Sockets.TcpClient mySocket) { byte[] bytes = new byte[2048]; string Str=null; System.Text.Encoding encoding =System.Text.Encoding.Unicode; System.Net.Sockets.NetworkStream ns = mySocket.GetStream();//Receive(bytes,bytes.Length,0); ns.Read(bytes,0,2048); //int i = stream.Read(bytes,0,1024); string Rstr=System.Text.Encoding.Unicode.GetString(bytes,0,bytes.Length); //ASCII.GetString(RecvBytes, 0, bytes); txtSocket.Text = Rstr + "server"; //接受新连接发送在线用户列表到连接Socket //mySocket.Send(txtdst.Text); byte[] bString = System.Text.Encoding.Unicode.GetBytes(txtSocket.Text); ns.Write(bString,0,bString.Length); return; }//客户端IPHostEntry ipHostInfo = Dns.GetHostByAddress("192.168.11.110"); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint ipe = new IPEndPoint(ipAddress,2300); TcpClient mySocket = new TcpClient(); mySocket.Connect(ipe); System.Net.Sockets.NetworkStream ns = mySocket.GetStream(); byte[] bytes = new byte[2048]; ns.Read(bytes,0,2048); string Rstr=System.Text.Encoding.Unicode.GetString(bytes,0,bytes.Length); txtsrc.Text = Rstr+"client"; byte[] bString = System.Text.Encoding.Unicode.GetBytes(txtSocket.Text); ns.Write(bString,0,bString.Length); mySocket.Close(); to:veaven(风林火山) 你贴的代码好象有问题.1是重复定义了ClientSocket,2没有txtsocket的定义... 这是TCP/IP网络编程的必修例子啊!!很多书上都有例子!可以参考!! 无分请教电影格式转换问题,悲剧 哪位大虾能帮我提供一个自动显示题目的模版,不胜感激 关于在.net中跳转站点的问题 关于listbox问题 怎么在C#中取得AD用户的用户名及密码 未处理的“System.Data.OleDb.OleDbException”类型的异常出现在 system.data.dll 中。搞了好久总是出现这个问题,郁闷,在线等待中.... dll调用时 权限问题 WPF,ListBox的这几个功能是怎么的? 根据字符串截取生成数组 如何执行VBSCRIPT 请问哪位有二进制文件的传输程序 大哥们!帮帮我好吗???
public void HandleThread()
{
Socket ClientSocket=new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); IPHostEntry ipHostInfo = Dns.GetHostByAddress("192.168.11.110");
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint ipe = new IPEndPoint(ipAddress,2300);
System.Net.Sockets.TcpListener ClientSocket = new System.Net.Sockets.TcpListener(ipAddress,2300);
ClientSocket.Start();
System.Net.Sockets.TcpClient mySocket;
while (true)
{
Application.DoEvents();//同时接受其他消息
mySocket=ClientSocket.AcceptTcpClient();
try
{
ReviceMsg(mySocket);
mySocket.Close();
}
catch(Exception Err)
{
mySocket.Close();
System.Windows.Forms.MessageBox.Show(Err.ToString()); }
}
}
//接受消息
private void ReviceMsg(System.Net.Sockets.TcpClient mySocket)
{ byte[] bytes = new byte[2048];
string Str=null;
System.Text.Encoding encoding =System.Text.Encoding.Unicode; System.Net.Sockets.NetworkStream ns = mySocket.GetStream();//Receive(bytes,bytes.Length,0);
ns.Read(bytes,0,2048);
//int i = stream.Read(bytes,0,1024);
string Rstr=System.Text.Encoding.Unicode.GetString(bytes,0,bytes.Length);
//ASCII.GetString(RecvBytes, 0, bytes);
txtSocket.Text = Rstr + "server"; //接受新连接发送在线用户列表到连接Socket
//mySocket.Send(txtdst.Text);
byte[] bString = System.Text.Encoding.Unicode.GetBytes(txtSocket.Text);
ns.Write(bString,0,bString.Length);
return;
}
//客户端
IPHostEntry ipHostInfo = Dns.GetHostByAddress("192.168.11.110");
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint ipe = new IPEndPoint(ipAddress,2300);
TcpClient mySocket = new TcpClient();
mySocket.Connect(ipe);
System.Net.Sockets.NetworkStream ns = mySocket.GetStream();
byte[] bytes = new byte[2048];
ns.Read(bytes,0,2048);
string Rstr=System.Text.Encoding.Unicode.GetString(bytes,0,bytes.Length);
txtsrc.Text = Rstr+"client";
byte[] bString = System.Text.Encoding.Unicode.GetBytes(txtSocket.Text);
ns.Write(bString,0,bString.Length);
mySocket.Close();
你贴的代码好象有问题.
1是重复定义了ClientSocket,
2没有txtsocket的定义...
很多书上都有例子!可以参考!!