一、TCP协议:
TCP协议是面向有连接的,所以服务器要与客户端建立连接
服务器端:
using System;
using System.Net.Sockets;
using System.Net;
using System.Text;
public static void Main(string[] args){
//创建服务器
Socket server=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPAddress ip= IPAddress.Parse("192.168.32.60");//创建IP地址,可以是本机的私网IP
EndPoint ep=new IPEndPoint(ip,4567);//ip地址与端口号的组合,端口号大一些就
server.Bind(ep);//绑定EP
//服务器开始监听
server.Listen(100);//设置最大连接数量为100
Socket client= server.Accept();//获取客户端的socket,用来与客户端通信
//发送信息
string message="你好,我是服务器!";
byte[] data=Encoding.UTF8.GetBytes(message);//转成能传送的byte类型的数据
client.Send(data);
while(true){
int length= client.Receive(data);
message=Encoding.UTF8.GetString(data,0,length);
Console.WriteLine("客户端:"+message);
}
}
客户端
public static void Main(string[] args)
{
//创建客户端
Socket client=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//创建IP地址(IP地址和端口号是服务器端的,用来和服务器进行通信)
IPAddress ip=IPAddress.Parse("192.168.32.60");
EndPoint ep=new IPEndPoint(ip,4567);
//用来接服务器端的数据
byte[] data=new byte[1024];
int length=0;//数据的长度
string message;
//连接服务器
client.Connect(ep);
length= client.Receive(data);
message=Encoding.UTF8.GetString(data,0,length);
Console.WriteLine(message);
while(true){
message=Console.ReadLine();//发送信息
data=Encoding.UTF8.GetBytes(message);
client.Send(data);
}
}
效果(客户端可以一直向服务器发送消息,服务器也可以一直接收消息):
image.png
image.png
基于unity的客户端与服务器通信,能一直向服务器发送消息
unity 客户端代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System;
using UnityEngine.UI;
using System.Threading;
public class Client_UI : MonoBehaviour {
private Socket client;//创建客户端
private IPAddress ip;
private EndPoint ep;
private byte[] data=new byte[1024];//用来接收和发送数据
private int length=0;
private string message;
public Text inputText;//输入栏
public Text outputText;//显示栏
private string allMessage;
public Button send;
private Thread t;
void Start () {
t = new Thread (SendMessageToServer);
client = new Socket (AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//绑定IP地址和端口号
ip=IPAddress.Parse("192.168.32.60");
ep = new IPEndPoint (ip, 4567);
//连接服务器
client.Connect(ep);
length= client.Receive(data);
message=Encoding.UTF8.GetString(data,0,length);
allMessage=message+'\n';
outputText.text = allMessage;
}
public void SendMessageToServer(){
message = inputText.text;
inputText.text = " ";
if (message != null && message != "") {
allMessage+=message+'\n';
outputText.text = allMessage;
data=Encoding.UTF8.GetBytes(message);
message = "";
client.Send(data);
}
}
}
效果:
image.png
image.png
二、UDP协议
UDP是面向无连接的,所以客户端与服务器不需要建立连接
服务器端代码:
using System;
using System.Net.Sockets;
using System.Net;
using System.Text;
namespace C_高级课程练习
{
/// <summary>
/// Description of UDP_my.
/// </summary>
public class UDP_my
{
public UDP_my()
{
}
public static void Main(string[] args){
//建立udp服务器,参数2:udp协议以数据报的方式传输,参数3:UDP协议
Socket udpServer=new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
//为服务器绑定IP
IPAddress ip=IPAddress.Parse("192.168.32.60");
EndPoint ep=new IPEndPoint(ip,3456);
udpServer.Bind(ep);
//接收数据
EndPoint endP=new IPEndPoint(IPAddress.Any,0);
string message;
byte[] data=new byte[1024];
int length=0;
//把数据的来源放到第二个参数上
while(true){
length= udpServer.ReceiveFrom(data,ref endP);
message=Encoding.UTF8.GetString(data,0,length);
Console.WriteLine("从IP:" +(endP as IPEndPoint).Address +"取到了消息:"+message);
}
}
}
}
客户端代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System;
using UnityEngine.UI;
using System.Threading;
public class Client_UI : MonoBehaviour {
private Socket udpClient;//创建客户端
private IPAddress ip;
private EndPoint ep;
private byte[] data=new byte[2048];//用来接收和发送数据
private int length=0;
private string message;
public Text inputText;//输入栏
public Text outputText;//显示栏
private string allMessage;
void Start () {
UDPInit ();
}
private void UDPInit(){
//建立udp客户端,参数2:udp协议以数据报的方式传输,参数3:UDP协议
udpClient = new Socket (AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
//发送信息准备工作
ip=IPAddress.Parse("192.168.32.60");
ep = new IPEndPoint (ip,3456);
}
public void SendMessageToServer(){
message = inputText.text;//得到输入框中的信息
inputText.text="";
data=Encoding .UTF8.GetBytes(message);
allMessage+=message+'\n';
outputText.text = allMessage;//将要发送的信息显示出来
udpClient.SendTo(data,ep);
}
}
服务器端运行效果:
image.png
客户端运行效果:
image.png
网友评论