欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用C#实现基于TCP和UDP协议的网络通信程序的基本示例

程序员文章站 2022-11-24 09:13:22
c#中使用tcp通信 tcp通信需要通信双方都在线,所以需要先启动服务端进行监听,客户端才能获得连接,服务端代码: static void main(strin...

c#中使用tcp通信

tcp通信需要通信双方都在线,所以需要先启动服务端进行监听,客户端才能获得连接,服务端代码:

static void main(string[] args) 
    { 
      tcpclient client = null; 
      networkstream stream = null; 
      byte[] buffer = null; 
      string receivestring = null; 
 
      ipaddress localip = ipaddress.parse("127.0.0.1"); 
      int localport = 11000; 
      tcplistener listener = new tcplistener(localip, localport);//用本地ip和端口实例化listener 
      listener.start();//开始监听 
      while (true) 
      { 
        client = listener.accepttcpclient();//接受一个client 
        buffer = new byte[client.receivebuffersize]; 
        stream = client.getstream();//获取网络流 
        stream.read(buffer, 0, buffer.length);//读取网络流中的数据 
        stream.close();//关闭流 
        client.close();//关闭client 
 
        receivestring = encoding.default.getstring(buffer).trim('\0');//转换成字符串 
        console.writeline(receivestring); 
      } 
    } 

只有服务端开启监听后,客户端才能正确连接,所以服务端要一直开启监听,客户端每次发送数据,都要首先与服务端建立连接,连接建立完成后才进行数据发送。客户端代码:

static void main(string[] args) 
    { 
      string sendstring = null;//要发送的字符串 
      byte[] senddata = null;//要发送的字节数组 
      tcpclient client = null;//tcpclient实例 
      networkstream stream = null;//网络流 
 
      ipaddress remoteip = ipaddress.parse("127.0.0.1");//远程主机ip 
      int remoteport = 11000;//远程主机端口 
 
      while (true)//死循环 
      { 
        sendstring = console.readline();//获取要发送的字符串 
        senddata = encoding.default.getbytes(sendstring);//获取要发送的字节数组 
        client = new tcpclient();//实例化tcpclient 
        try 
        { 
          client.connect(remoteip, remoteport);//连接远程主机 
        } 
        catch (system.exception ex) 
        { 
          console.writeline("连接超时,服务器没有响应!");//连接失败 
          console.readkey(); 
          return; 
        } 
        stream = client.getstream();//获取网络流 
        stream.write(senddata, 0, senddata.length);//将数据写入网络流 
        stream.close();//关闭网络流 
        client.close();//关闭客户端 
      } 
    } 

使用C#实现基于TCP和UDP协议的网络通信程序的基本示例

c#中使用udp通信
udp通信是无连接通信,客户端在发送数据前无需与服务器端建立连接,即使服务器端不在线也可以发送,但是不能保证服务器端可以收到数据。
服务器端代码:

static void main(string[] args) 
    { 
      udpclient client = null; 
      string receivestring = null; 
      byte[] receivedata = null; 
      //实例化一个远程端点,ip和端口可以随意指定,等调用client.receive(ref remotepoint)时会将该端点改成真正发送端端点 
      ipendpoint remotepoint = new ipendpoint(ipaddress.any, 0); 
 
      while (true) 
      { 
        client = new udpclient(11000); 
        receivedata = client.receive(ref remotepoint);//接收数据 
        receivestring = encoding.default.getstring(receivedata); 
        console.writeline(receivestring); 
        client.close();//关闭连接 
      } 
    } 

客户端代码:

static void main(string[] args) 
    { 
      string sendstring = null;//要发送的字符串 
      byte[] senddata = null;//要发送的字节数组 
      udpclient client = null; 
 
      ipaddress remoteip = ipaddress.parse("127.0.0.1"); 
      int remoteport = 11000; 
      ipendpoint remotepoint = new ipendpoint(remoteip, remoteport);//实例化一个远程端点 
 
      while (true) 
      { 
        sendstring = console.readline(); 
        senddata = encoding.default.getbytes(sendstring); 
 
        client = new udpclient(); 
        client.send(senddata, senddata.length, remotepoint);//将数据发送到远程端点 
        client.close();//关闭连接 
      } 
    } 

使用C#实现基于TCP和UDP协议的网络通信程序的基本示例