简单的实现就行,主要想了解服务器和客户端如何通过一个连接,发送和接收信息(异步\C#)
我的E_mail:[email protected]
谢谢各位大侠了..

解决方案 »

  1.   

    这个通讯聊天的客户端!
    using System;
    using System.Net.Sockets;
    using System.Text;
    public class TcpTimeClient
    {    private const int portNum = 1555;
        private const string hostName = "localhost";
        public static int Main(String[] args)
        {
            while (true)
            {
                try
                {
                    Console.Write("Try to connect to " + hostName + ":" + portNum.ToString() + "\r\n");                 TcpClient client = new TcpClient(hostName, portNum);
                    while (true)
                    {
                        NetworkStream ns = client.GetStream();                    byte[] bytes = new byte[1024];
                        int bytesRead = ns.Read(bytes, 0, bytes.Length);
                        Console.WriteLine(Encoding.ASCII.GetString(bytes, 0, bytesRead));
                    }
                    string message=Console.ReadLine();
                    if (message == "stop")
                    {
                        client.Close();
                    }            }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    Console.ReadLine();
                }
                return 0;
            }    }
       
      

  2.   

    这是通讯源代码的服务端!
    using System; 
    using System.Net.Sockets; 
    using System.Text; 
    namespace TimeServer 

       class TimeServer 

         private const int portNum = 1555;
           private const string hostName = "localhost";
              [STAThread] 
         static void Main(string[] args) 
          { 
            bool done = false; 
            TcpListener listener = new TcpListener(portNum); 
            listener.Start();
            Console.Write("Waiting for connection...");
            TcpClient client = listener.AcceptTcpClient();
                   while (!done) 
                  { 
                                           Console.WriteLine("Connection accepted.");                              //    Socket soo = listener.AcceptSocket();
                                    //  if( soo.Connected)
                                      //{
                                        //  string ServerEndPoint = "服务端地址" + soo.LocalEndPoint.ToString() + "\r\n";
                                          
                                          //string mpoint = "请求的客户端地址用端口"+soo.RemoteEndPoint.ToString();
                                          //Console.WriteLine(ServerEndPoint);
                                          //Console.WriteLine (mpoint);
                                         // Console.ReadLine();
                                      //} 
                                                                        NetworkStream ns = client.GetStream();
                                      Console.WriteLine("请输入你要发送的信息");
                                      string s = Console.ReadLine();
                                   if (s =="pouse")
                                   {
                                       ns.Close();
                                       client.Close();
                                   }                               
                                 try 
                                   {
                                       byte[] byteTime = Encoding.ASCII.GetBytes(s.ToString());
                                        ns.Write(byteTime, 0, byteTime.Length); 
           
                                   } 
                                 catch (Exception e) 
                                                   { 
                                                        Console.WriteLine(e.ToString()); 
                                                   } 
                  }
                  
           } 
      } 
    }
      

  3.   

    这是用c#控制台应用程序写的,代码结构清晰,如有什么不懂的可以发e-mail与我联系,[email protected]
      

  4.   

    简单的实现就行,主要想了解服务器和客户端如何通过一个连接,发送和接收信息(异步\C#) (使用socket)客户端收到信息就显示到textbox中,发送信息和接收信息都使用同一连接.
      

  5.   

    我有一个TCP 一个UTP的 不知道你要哪个