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

C#实现Socket通信的解决方法

程序员文章站 2024-02-21 19:32:52
本文以实例详述了c#实现socket通信的解决方法,具体实现步骤如下: 1、首先打开vs新建两个控制台应用程序: consoleapplication_socketse...

本文以实例详述了c#实现socket通信的解决方法,具体实现步骤如下:

1、首先打开vs新建两个控制台应用程序:
consoleapplication_socketserver和consoleapplication_socketclient。
 
2、在consoleapplication_socketclient中输入以下代码:

using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.net; 
using system.net.sockets; 
 
namespace consoleapplication_socketclient 
{ 
  class program 
  { 
    static socket clientsocket; 
    static void main(string[] args) 
    { 
      //将网络端点表示为ip地址和端口 用于socket侦听时绑定  
      ipendpoint ipep = new ipendpoint(ipaddress.parse("*.*.*.*"), 3001); //填写自己电脑的ip或者其他电脑的ip,如果是其他电脑ip的话需将consoleapplication_socketserver工程放在对应的电脑上。 
      clientsocket = new socket(ipep.addressfamily,sockettype.stream,protocoltype.tcp);  
      //将socket连接到服务器  
      try 
      { 
        clientsocket.connect(ipep); 
        string outbufferstr; 
        byte[] outbuffer = new byte[1024]; 
        byte[] inbuffer = new byte[1024]; 
        while (true) 
        { 
          //发送消息  
          outbufferstr = console.readline(); 
          outbuffer = encoding.ascii.getbytes(outbufferstr); 
          clientsocket.send(outbuffer, outbuffer.length, socketflags.none); 
           
          //接收服务器端信息        
          clientsocket.receive(inbuffer, 1024, socketflags.none);//如果接收的消息为空 阻塞 当前循环 
          console.writeline("服务器说:"); 
          console.writeline(encoding.ascii.getstring(inbuffer)); 
        } 
      } 
      catch 
      { 
        console.writeline("服务未开启!"); 
        console.readline(); 
      } 
    } 
  } 
} 

3、在consoleapplication_socketserver中输入以下代码:

using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.io; 
using system.net; 
using system.net.sockets; 
using system.threading; 
 
namespace consoleapplication_socketserver 
{ 
  class program 
  { 
    static socket serversocket; 
    static socket clientsocket; 
    static thread thread; 
    static void main(string[] args) 
    { 
      ipendpoint ipep = new ipendpoint(ipaddress.any, 3001); 
      serversocket = new socket(ipep.addressfamily, sockettype.stream, protocoltype.tcp); 
      serversocket.bind(ipep); 
      serversocket.listen(10); 
      while (true) 
      { 
        clientsocket = serversocket.accept(); 
        thread = new thread(new threadstart(dowork)); 
        thread.start(); 
      } 
    } 
    private static void dowork() 
    { 
      socket s = clientsocket;//客户端信息 
      ipendpoint ipendpoint = (ipendpoint)s.remoteendpoint; 
      string address = ipendpoint.address.tostring(); 
      string port = ipendpoint.port.tostring(); 
      console.writeline(address + ":" + port + " 连接过来了"); 
      byte[] inbuffer = new byte[1024]; 
      byte[] outbuffer = new byte[1024]; 
      string inbufferstr; 
      string outbufferstr; 
      try 
      { 
        while (true) 
        { 
          s.receive(inbuffer, 1024, socketflags.none);//如果接收的消息为空 阻塞 当前循环 
          inbufferstr = encoding.ascii.getstring(inbuffer); 
          console.writeline(address + ":" + port + "说:"); 
          console.writeline(inbufferstr); 
          outbufferstr = console.readline(); 
          outbuffer = encoding.ascii.getbytes(outbufferstr); 
          s.send(outbuffer, outbuffer.length, socketflags.none); 
        } 
      } 
      catch 
      { 
        console.writeline("客户端已关闭!"); 
      } 
    } 
  } 
} 
 

4、先运行consoleapplication_socketserver,后运行consoleapplication_socketclient就可以通信了。

本例给出了基本的实现代码,读者可以根据自身的需求进一步完成个性化功能。