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)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 鹤山市| 鹤壁市| 酉阳| 龙川县| 英德市| 苏尼特右旗| 远安县| 探索| 十堰市| 新余市| 兴文县| 如东县| 龙岩市| 洛宁县| 章丘市| 荣成市| 老河口市| 敦煌市| 安泽县| 缙云县| 昌黎县| 彝良县| 绥阳县| 连江县| 宁城县| 新营市| 平罗县| 温州市| 湟源县| 富锦市| 商南县| 芜湖县| 仲巴县| 依安县| 冷水江市| 赤城县| 安福县| 平顶山市| 二连浩特市| 荔波县| 蒲江县|