日期:2014-05-17  浏览次数:20974 次

C# Socket异步通讯客户端之发送数据

C# Socket异步通讯客户端之主程序:

[c-sharp] view plaincopyprint?
  1. # public static int Main(String[] args)    
  2. # {    
  3. #    
  4. # IPAddress ipAddress = IPAddress.Parse("192.168.1.104");    
  5. # int port = 20000;    
  6. # IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);    
  7. #    
  8. # // 生成一个TCP/IP socket.    
  9. # Socket client = new Socket(AddressFamily.InterNetwork,    
  10. # SocketType.Stream, ProtocolType.Tcp);    
  11. #    
  12. # // 与目标终端连接.    
  13. # client.BeginConnect(remoteEP,    
  14. # new AsyncCallback(ConnectCallback), client);    
  15. # //等待,直到连接程序完成。在ConnectCallback中适当位置有connecDone.Set()语句    
  16. # connectDone.WaitOne();    
  17. #    
  18. # // 发送数据到远程终端.    
  19. # Send(client, "This is a test<EOF>");    
  20. # sendDone.WaitOne();    
  21. #    
  22. # // 接收返回数据.    
  23. # Receive(client);    
  24. # receiveDone.WaitOne();    
  25. #    
  26. # // Write the response to the console.    
  27. # Console.WriteLine("Response received : {0}", response);    
  28. #