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

原文地址http://www.jb51.net/article/52244.htm

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

1、首先打开VS新建两个控制台应用程序:

ConsoleApplication_socketServer和ConsoleApplication_socketClient。

2、在ConsoleApplication_socketClient中输入以下代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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中输入以下代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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( "客户端已关闭!" );
       }
     }
   }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,可以使用System.Net.Sockets命名空间中的类来实现Socket通信。以下是一个简单的示例来说明如何在C#实现Socket通信: 1. 首先,需要使用`Socket`类来创建服务器端和客户端的Socket对象。可以使用`SocketType.Stream`来创建一个基于TCP协议的Socket对象。 2. 服务器端需要使用`Socket.Bind`方法Socket对象绑定到一个IP地址和端口号上。这样,服务器端就可以监听指定的地址和端口,等待客户端的连接。 3. 启动服务器端的监听,并使用`Socket.Accept`方法接受客户端的连接。一旦有客户端连接成功,服务器端会返回一个新的Socket对象,用于与该客户端进行通信。 4. 客户端需要使用`Socket.Connect`方法连接到服务器端指定的地址和端口。一旦连接成功,客户端就可以与服务器端进行通信。 5. 在服务器端和客户端之间可以使用`Socket.Send`方法和`Socket.Receive`方法来发送和接收数据。可以将数据转换为字节数组进行传输。 6. 当通信完成后,需要使用`Socket.Close`方法关闭Socket对象。 下面是一个简单的C#代码示例,演示了服务器端和客户端之间的Socket通信: ```csharp // 服务器端 Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(new IPEndPoint(IPAddress.Any, 8080)); serverSocket.Listen(10); Console.WriteLine("Waiting for client connection..."); Socket clientSocket = serverSocket.Accept(); Console.WriteLine("Client connected."); byte[] buffer = new byte
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值