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

所属分类: 软件编程 / C#教程 阅读数: 60
收藏 0 赞 0 分享

本文以实例详述了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就可以通信了。

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

更多精彩内容其他人还在看

C#使用oledb读取excel表格内容到datatable的方法

这篇文章主要介绍了C#使用oledb读取excel表格内容到datatable的方法,涉及C#操作oledb及datatable的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#使用oledb操作excel文件的方法

这篇文章主要介绍了C#使用oledb操作excel文件的方法,涉及C#中oledb操作excel的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#使用IHttpModule接口修改http输出的方法

这篇文章主要介绍了C#使用IHttpModule接口修改http输出的方法,涉及C#操作IHttpModule接口的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#给图片加水印的简单实现方法

这篇文章主要介绍了C#给图片加水印的简单实现方法,涉及C#操作图片的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#生成随机数的方法小结

这篇文章主要介绍了C#生成随机数的方法,实例总结了C#生成随机数的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#使用jQuery实现无刷新评论提交的方法

这篇文章主要介绍了C#使用jQuery实现无刷新评论提交的方法,涉及C#结合jQuery进行Ajax操作的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#读取中文文件出现乱码的解决方法

这篇文章主要介绍了C#读取中文文件出现乱码的解决方法,涉及C#中文编码的操作技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#图像对比度调整的方法

这篇文章主要介绍了C#图像对比度调整的方法,涉及C#实现图像对比度操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#图像灰度级拉伸的方法

这篇文章主要介绍了C#图像灰度级拉伸的方法,涉及C#灰度操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#图像线性变换的方法

这篇文章主要介绍了C#图像线性变换的方法,涉及C#操作图像线性变换的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多