通讯聊天

来源:互联网 发布:知乎励志的话 编辑:程序博客网 时间:2024/04/28 23:07

客户端:

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()   +   " ");   

                                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
                } 

        } 

 服务器端:

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()   +   " "; 
                                                                            
                                                                            
//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());   
                                                                                              }   
                            } 
                            
              }   
    }   
}
原创粉丝点击