Original Address: https: //www.cnblogs.com/sdyinfang/p/5519708.html
About C # socket communication, is divided into synchronous and asynchronous communication, this brief synchronous communication.
Ends of the communication are client (Client) and a server (Server):
(1)Cient:
1: Create a Socket on the image;
2: () method of the above image EndPoint established as a parameter, a connection request to the server socket with the image of Connect;
3: If the connection is successful, a socket () method sends the information to the server on the Send image;
4: receive information sent from the server with the image of the Receive socket () method;
5: After the communication must remember to close Socket;
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
|
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Net.Sockets;
using
System.Net;
namespace
Client
{
class
Program
{
static
Socket ClientSocket;
static
void
Main(
string
[] args)
{
String IP =
"127.0.0.1"
;
int
port =8885 ;
IPAddress ip = IPAddress.Parse(IP);
//将IP地址字符串转换成IPAddress实例
ClientSocket =
new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//使用指定的地址簇协议、套接字类型和通信协议
IPEndPoint endPoint =
new
IPEndPoint(ip, port);
// 用指定的ip和端口号初始化IPEndPoint实例
ClientSocket.Connect(endPoint);
//与远程主机建立连接
Console.WriteLine(
"开始发送消息"
);
byte
[] message = Encoding.ASCII.GetBytes(
"Connect the Server"
);
//通信时实际发送的是字节数组,所以要将发送消息转换字节
ClientSocket.Send(message);
Console.WriteLine(
"发送消息为:"
+ Encoding.ASCII.GetString(message));
byte
[] receive =
new
byte
[1024];
int
length = ClientSocket.Receive(receive);
// length 接收字节数组长度
Console.WriteLine(
"接收消息为:"
+ Encoding.ASCII.GetString(receive));
ClientSocket.Close();
//关闭连接
}
}
}
|
The client returns the result:
(2)Server:
1: Create a Socket on the image;
2: the image of the socket with the Bind () method of binding the EndPoint;
3: The socket on the image Listen () method starts listening;
4: received client connections, create a new socket with the socket of the image the Accept () method is used as the client and the requested communication;
5: receiving new socket object (the Receive) and a transmission (the Send) message.
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
|
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Net.Sockets;
using
System.Net;
using
System.Threading;
namespace
Server
{
class
Program
{
static
Socket ReceiveSocket;
static
void
Main(
string
[] args)
{
int
port = 8885;
IPAddress ip = IPAddress.Any;
// 侦听所有网络客户接口的客活动
ReceiveSocket =
new
Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//使用指定的地址簇协议、套接字类型和通信协议 <br> ReceiveSocket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,true); //有关套接字设置
IPEndPoint endPoint =
new
IPEndPoint(ip,port);
ReceiveSocket.Bind(
new
IPEndPoint(ip, port));
//绑定IP地址和端口号
ReceiveSocket.Listen(10);
//设定最多有10个排队连接请求
Console.WriteLine(
"建立连接"
);
Socket socket = ReceiveSocket.Accept();
byte
[] receive =
new
byte
[1024];
socket.Receive(receive);
Console.WriteLine(
"接收到消息:"
+ Encoding.ASCII.GetString(receive));
byte
[] send = Encoding.ASCII.GetBytes(
"Success receive the message,send the back the message"
);
socket.Send(send);
Console.WriteLine(
"发送消息为:"
+Encoding.ASCII.GetString(send));
}
}
}
|
服务器返回结果:
原文地址:https://www.cnblogs.com/sdyinfang/p/5519708.html
关于C#socket通信,分为同步和异步通信,本文简单介绍一下同步通信。
通信两端分别为客户端(Client)和服务器(Server):
(1)Cient:
1:建立一个Socket对像;
2:用socket对像的Connect()方法以上面建立的EndPoint对像做为参数,向服务器发出连接请求;
3:如果连接成功,就用socket对像的Send()方法向服务器发送信息;
4:用socket对像的Receive()方法接受服务器发来的信息 ;
5:通信结束后一定记得关闭socket;
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
|
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Net.Sockets;
using
System.Net;
namespace
Client
{
class
Program
{
static
Socket ClientSocket;
static
void
Main(
string
[] args)
{
String IP =
"127.0.0.1"
;
int
port =8885 ;
IPAddress ip = IPAddress.Parse(IP);
//将IP地址字符串转换成IPAddress实例
ClientSocket =
new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//使用指定的地址簇协议、套接字类型和通信协议
IPEndPoint endPoint =
new
IPEndPoint(ip, port);
// 用指定的ip和端口号初始化IPEndPoint实例
ClientSocket.Connect(endPoint);
//与远程主机建立连接
Console.WriteLine(
"开始发送消息"
);
byte
[] message = Encoding.ASCII.GetBytes(
"Connect the Server"
);
//通信时实际发送的是字节数组,所以要将发送消息转换字节
ClientSocket.Send(message);
Console.WriteLine(
"发送消息为:"
+ Encoding.ASCII.GetString(message));
byte
[] receive =
new
byte
[1024];
int
length = ClientSocket.Receive(receive);
// length 接收字节数组长度
Console.WriteLine(
"接收消息为:"
+ Encoding.ASCII.GetString(receive));
ClientSocket.Close();
//关闭连接
}
}
}
|
客户端返回结果:
(2)Server:
1:建立一个Socket对像;
2:用socket对像的Bind()方法绑定EndPoint;
3:用socket对像的Listen()方法开始监听;
4:接受到客户端的连接,用socket对像的Accept()方法创建新的socket对像用于和请求的客户端进行通信;
5:用新的socket对象接收(Receive)和发送(Send)消息。
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
|
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Net.Sockets;
using
System.Net;
using
System.Threading;
namespace
Server
{
class
Program
{
static
Socket ReceiveSocket;
static
void
Main(
string
[] args)
{
int
port = 8885;
IPAddress ip = IPAddress.Any;
// 侦听所有网络客户接口的客活动
ReceiveSocket =
new
Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//使用指定的地址簇协议、套接字类型和通信协议 <br> ReceiveSocket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,true); //有关套接字设置
IPEndPoint endPoint =
new
IPEndPoint(ip,port);
ReceiveSocket.Bind(
new
IPEndPoint(ip, port));
//绑定IP地址和端口号
ReceiveSocket.Listen(10);
//设定最多有10个排队连接请求
Console.WriteLine(
"建立连接"
);
Socket socket = ReceiveSocket.Accept();
byte
[] receive =
new
byte
[1024];
socket.Receive(receive);
Console.WriteLine(
"接收到消息:"
+ Encoding.ASCII.GetString(receive));
byte
[] send = Encoding.ASCII.GetBytes(
"Success receive the message,send the back the message"
);
socket.Send(send);
Console.WriteLine(
"发送消息为:"
+Encoding.ASCII.GetString(send));
}
}
}
|
服务器返回结果: