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 閱讀(978) 評論(0)  編輯  收藏

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


          網(wǎng)站導航:
           

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 南和县| 水城县| 仁化县| 抚松县| 达州市| 吴旗县| 富宁县| 始兴县| 潼关县| 页游| 叙永县| 遵化市| 五华县| 泽普县| 岳西县| 花垣县| 连山| 台北县| 青龙| 五莲县| 甘南县| 都昌县| 三亚市| 宁陵县| 竹溪县| 鄄城县| 泰兴市| 沽源县| 思南县| 邹平县| 朝阳市| 墨脱县| 仁化县| 礼泉县| 靖宇县| 绥化市| 遵义市| 云安县| 尚义县| 唐海县| 景谷|