public static void StartClient()
                  {
                      // Data buffer for incoming data.
                      byte[] bytes = new byte[1024];

                      // Connect to a remote device.
                      try
                      {
                          // Establish the remote endpoint for the socket.
                          // This example uses port 11000 on the local computer.
                          IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
                          IPAddress ipAddress = ipHostInfo.AddressList[0];
                          IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);

                          // Create a TCP/IP  socket.
                          Socket sender = new Socket(AddressFamily.InterNetwork,
                             SocketType.Stream, ProtocolType.Tcp);
                          // Connect the socket to the remote endpoint. Catch any errors.
                          try
                          {
                              sender.Connect(remoteEP);

                              Console.WriteLine("Socket connected to {0}",
                                 sender.RemoteEndPoint.ToString());

                              string tempStr = GetContent(Console.ReadLine());
                              while (true)
                              {

                            

                             
                                  Console.WriteLine("輸入信息");


                                  // Encode the data string into a byte array.
                                  byte[] msg = Encoding.GetEncoding("gb2312").GetBytes(tempStr + "<EOF>");


                                  // Send the data through the socket.
                                  int bytesSent = sender.Send(msg);

                                  // Receive the response from the remote device.
                                  int bytesRec = sender.Receive(bytes);
                                  Console.WriteLine("Echoed test = {0}",
                                      Encoding.ASCII.GetString(bytes, 0, bytesRec));
                           
                                  Console.WriteLine("輸入信息 輸入e退出");
                                  tempStr = GetContent(Console.ReadLine());
                                   if (tempStr == "e")
                                       break;
                              }
                              // Release the socket.
                              sender.Shutdown(SocketShutdown.Both);
                              sender.Close();

                          }
                          catch (ArgumentNullException ane)
                          {
                              Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
                          }
                          catch (SocketException se)
                          {
                              Console.WriteLine("SocketException : {0}", se.ToString());
                          }
                          catch (Exception e)
                          {
                              Console.WriteLine("Unexpected exception : {0}", e.ToString());
                          }

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

          posted on 2011-01-11 13:18 sanmao 閱讀(976) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 名山县| 扎囊县| 棋牌| 兴隆县| 青岛市| 五台县| 筠连县| 绥棱县| 开远市| 河东区| 田阳县| 临安市| 和林格尔县| 仙居县| 邵阳县| 泽库县| 西畴县| 碌曲县| 十堰市| 淮南市| 庄河市| 阿克陶县| 宁城县| 古浪县| 安阳县| 望都县| 连州市| 连江县| 平山县| 华蓥市| 尚义县| 普陀区| 钦州市| 新龙县| 沙河市| 祁门县| 泰安市| 改则县| 廊坊市| 静海县| 吐鲁番市|