> 馆藏中心

var receive = e.Argument as object;

来源:cnblogs 编辑:杨丽

正如楼上所说,网络编程涉及的东西还是挺多的,而且要写出一个有优秀的socket服务器是非常有技术含量的,那我只贴一个非常简单的示例,让你看看。另外假如你的应用对网络通信要求不高的话,.NET已经有几个非常好用的socket封装(TcpClient,TcpListener),用它们很方便。 public class SocketServer { private Socket listenSocket; private List<Socket> clientList = new List<Socket>(); private object locker = new object(); public void Listen(int port) { listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //监听线程 Thread thread = new Thread(new ParameterizedThreadStart((p) => { Socket socket = (Socket)p; socket.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Any, port)); socket.Listen(10); while (true) { try { Socket acceptSocket = socket.Accept();//侦听到一个socket lock (locker) { SocketAsyncEventArgs e = new SocketAsyncEventArgs(); //根据Socket的默认的接收缓冲区大小设置数据接收事件的缓冲区大小 byte[] receivebuff = new byte[socket.ReceiveBufferSize]; e.SetBuffer(receivebuff, 0, receivebuff.Length); e.Completed += SendOrReceiveCompleted; acceptSocket.ReceiveAsync(e);//开启异步接收数据 clientList.Add(acceptSocket); } } catch { } } })); thread.IsBackground = true; thread.Start(listenSocket); } private void SendOrReceiveCompleted(object sender, SocketAsyncEventArgs e) { switch (e.LastOperation) { case SocketAsyncOperation.Receive: byte[] data = new byte[e.BytesTransferred]; Array.Copy(e.Buffer, data, data.Length); string result = System.Text.UTF8Encoding.UTF8.GetString(data); Console.WriteLine("Receive:" + result); ((Socket)sender).ReceiveAsync(e);//继续异步接收数据 break; case SocketAsyncOperation.Send: break; } } /// <summary> /// 群发 /// </summary> public void SendAll(string data) { lock (locker) { foreach (var socket in clientList) { socket.Send(System.Text.UTF8Encoding.UTF8.GetBytes(data)); } } } } public class SocketClient { private Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); public void Connect(string host, int port) { socket.Connect(host, port); SocketAsyncEventArgs e = new SocketAsyncEventArgs(); //根据Socket的默认的接收缓冲区大小设置数据接收事件的缓冲区大小 byte[] receivebuff = new byte[socket.ReceiveBufferSize]; e.SetBuffer(receivebuff, 0, receivebuff.Length); e.Completed += SendOrReceiveCompleted; socket.ReceiveAsync(e);//开启异步接收数据 } private void SendOrReceiveCompleted(object sender, SocketAsyncEventArgs e) { switch (e.LastOperation) { case SocketAsyncOperation.Receive: byte[] data = new byte[e.BytesTransferred]; Array.Copy(e.Buffer, data, data.Length); string result = System.Text.UTF8Encoding.UTF8.GetString(data); Console.WriteLine("Receive:" + result); ((Socket)sender).ReceiveAsync(e);//继续异步接收数据 break; case SocketAsyncOperation.Send: break; } } /// <summary> /// 发送 /// </summary> public void Send(string data) { int result = socket.Send(System.Text.UTF8Encoding.UTF8.GetBytes(data)); } } 服务器端用法: SocketServer socketServer = new SocketServer(); socketServer.Listen(8080); while (true) { string input = Console.ReadLine(); if (input == "exit") { break; } if (input != string.Empty) { socketServer.SendAll(input); } } 客户端用法: SocketClient socketClient = new SocketClient(); socketClient.Connect("localhost", 8080); while (true) { string input = Console.ReadLine(); if (input == "exit") { break; } if (input != string.Empty) { socketClient.Send(input); } } 以上示例很简单,还有很多情况没有处理,仅供参考!www.179s.com防采集请勿采集本网。

把e.Argument 賦予receive

型別為object奖励园豆:5

Option Explicit Dim sendvar(0) As Byte Dim i As Integer Dim sum As Integer Private Sub Command1_Click() For i = 0 To 15 sendvar(0) = i MSComm1.Output = sendva

D7确实经典,但已经落伍了,用xe版本吧udp发送这个东西不检测是否通不通,用TCP连接可以检测;实在想用可以在server发送一个字符串,client接受到以后,反馈一个什么的字符串,表示正常通讯了在Client端:procedure TUDPMainForm.SendButtonClick(Sender: TObject);varMessageID: Integer;ThisMessage: String;ReceivedString: String;beginMessageID := Random(MAXINT);ThisMessage := 'Message: ' + IntToStr(MessageID);UDPMemo.Lines.Add('Sending ' + ThisMessage);UDPClient.Send(ThisMessage);ReceivedString := UDPClient.ReceiveString(); //该方法取得由服务端返回的信息。if ReceivedString = '' thenUDPMemo.Lines.Add('No response received from the server after ' + IntToStr(UDPClient.ReceiveTimeout) + ' millseconds.')elseUDPMemo.Lines.Add('Received: ' + ReceivedString)end;Server端:procedure TUDPMainForm.UDPServerUDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);varDataStringStream: TStringStream;s: String;beginDataStringStream := TStringStream.Create('');tryDataStringStream.CopyFrom(AData, AData.Size);UDPMemo.Lines.Add('Received "' + DataStringStream.DataString + '" from ' + ABinding.PeerIP + ' on port ' + IntToStr(ABinding.PeerPort));s := 'Replied from ' + UDPServer.LocalName + ' to "' + DataStringStream.DataString + '"';ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, s[1], Length(s));finallyDataStringStream.Free;end;内容来自www.179s.com请勿采集。

本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。本文《var receive = e.Argument as object;》转载自cnblogs,版权归原作者所有,若侵权请联系:E-MAIL:513175919@qq.com
违法违规信息请立即联系本网可获得现金奖励,TEL:1-8-2-1-0-2-3-3-3-8-1(电话仅供违法违规信息举报,侵权类信息请EMAIL。)

www.179s.com false 互联网 http://www.179s.com/jsblogcnk/po/pohvyd.html report 5788 把e.Argument 賦予receive型別為object奖励园豆:5

热门图片

经济金融企业管理法律法规社会民生科学教育降生活体育运动文化艺术电子数码电脑网络娱乐休闲行政地区心理分析医疗卫生