赞
踩
Socket抽象层是存在于应用层(用户进程)和传输层(TCP和UDP)之间的。是一组接口,在设计模式当中把复杂的TCP/IP协议族隐藏在Socket接口之后。
TCP协议是面向连接的可靠传输,也就是计算机网络中所学的知识,TCP实现可靠传输靠的是三握手,TCP通信示意图如下
1)建立Socket,注意引用命名空间
Socket tcpServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2)绑定Bind()服务器端IP地址以及端口号——服务器端
IPAddress iPAddress = new IPAddress(new byte[] { 192, 168, 1, 3 });
EndPoint endPoint = new IPEndPoint(iPAddress, 8899);
tcpServer.Bind(endPoint);
3)利用Listen()方法开启监听——服务器端
tcpServer.Listen(100);
Console.WriteLine("开始监听");
4)利用Accept()方法尝试与客户端建立一个连接——服务器端
Socket client = tcpServer.Accept();
Console.WriteLine("-------有个客户端已连接--------");
5)利用Connect()方法与服务器建立连接——客户端
tcpClient.Connect(new IPEndPoint(IPAddress.Parse("192.168.1.3"), 8899));
6)利用Send()方法向建立连接的主机发送消息
string s = Console.ReadLine();
tcpClient.Send(Encoding.UTF8.GetBytes(s));
7)利用Receive()方法接受来自建立连接主机的消息(实现可靠连接)
byte[] data = new byte[1024]; //接收服务器传过来的信息
int length = tcpClient.Receive(data); //传递信息的有效长度
string message = Encoding.UTF8.GetString(data, 0, length);
Console.WriteLine(message);
//1.创建socket Socket tcpServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //2.绑定ip IPAddress iPAddress = new IPAddress(new byte[] { 192, 168, 1, 3 }); EndPoint endPoint = new IPEndPoint(iPAddress, 8899); tcpServer.Bind(endPoint); //3.监听 tcpServer.Listen(100); Console.WriteLine("开始监听"); Socket client = tcpServer.Accept(); Console.WriteLine("-------有个客户端已连接--------"); string message = "欢迎你连接我"; byte[] data = Encoding.UTF8.GetBytes(message); client.Send(data); Console.WriteLine("以下是客户端传送过来的数据:"); while (true) { byte[] buffer = new byte[1024]; int number = client.Receive(buffer); string message1 = Encoding.UTF8.GetString(buffer, 0, number); Console.WriteLine(message1); } tcpServer.Close(); Console.ReadKey();
//1.创建Socket
Socket tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//2.产生连接
tcpClient.Connect(new IPEndPoint(IPAddress.Parse("192.168.1.3"), 8899));
byte[] data = new byte[1024]; //接收服务器传过来的信息
int length = tcpClient.Receive(data); //传递信息的有效长度
string message = Encoding.UTF8.GetString(data, 0, length);
Console.WriteLine(message);
Console.WriteLine("请输入你想传递给服务器的消息:");
while (true)
{
string s = Console.ReadLine();
tcpClient.Send(Encoding.UTF8.GetBytes(s));
}
Console.ReadKey();
总的来说TCP通信大致就是六步,建立socket->绑定Bind->监听Listen->通过Accept()与客户端建立连接->客户端Connect()连接服务器->Send()给服务器发送消息->通过Receive()方法来建立连接,从而实现可靠传输。
UDP协议是无连接不可靠传输,实现的流程图如下
1)建立Socket,注意引用命名空间
Socket udpServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
2)绑定Bind()服务器端IP地址以及端口号——服务器端
IPAddress iPAddress = new IPAddress(new byte[] { 192, 168, 1, 3 });
EndPoint endPoint = new IPEndPoint(iPAddress, 8899);
udpServer.Bind(endPoint);
3)通过SendTo()方法向建立连接的主机发送消息(需要提供IP地址及端口)
string message = Console.ReadLine();
udpServer.SendTo(Encoding.UTF8.GetBytes(message), new IPEndPoint(IPAddress.Parse("192.168.1.3"), 8899));
4)通过ReciveFrom()方法接收指定主机发送的消息(需要提供主机IP地址及端口)
byte[] data = new byte[1024];
EndPoint endPoint1 = new IPEndPoint(IPAddress.Any, 0); //下一句要重新赋值的,所以先随便给一个
int length = udpServer.ReceiveFrom(data, ref endPoint1); //ref是既进又出,out是只出不进
string message = Encoding.UTF8.GetString(data, 0, length);
Console.WriteLine(message + "-----ip:" + (endPoint1 as IPEndPoint).Address + "-----port:" + (endPoint1 as IPEndPoint).Port);
//创建Socket Socket udpServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //绑定ip地址和端口号 IPAddress iPAddress = new IPAddress(new byte[] { 192, 168, 1, 3 }); EndPoint endPoint = new IPEndPoint(iPAddress, 8899); udpServer.Bind(endPoint); Console.WriteLine("连接成功"); while (true) { byte[] data = new byte[1024]; EndPoint endPoint1 = new IPEndPoint(IPAddress.Any, 0); int length = udpServer.ReceiveFrom(data, ref endPoint1); string message = Encoding.UTF8.GetString(data, 0, length); Console.WriteLine(message + "-----ip:" + (endPoint1 as IPEndPoint).Address + "-----port:" + (endPoint1 as IPEndPoint).Port); } Console.ReadKey();
static Socket udpServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); static void Main(string[] args) { udpServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); Thread thread = new Thread(SendMessage); thread.Start(); Console.ReadKey(); } static void SendMessage() { while (true) { string message = Console.ReadLine(); udpServer.SendTo(Encoding.UTF8.GetBytes(message), new IPEndPoint(IPAddress.Parse("192.168.1.3"), 8899)); } }
用死循环来传递消息的情况的话会有短暂的卡顿现象,但是用开启线程的方式来进行消息的传递的话并不会出现卡顿的现象。
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。