110,549
社区成员
发帖
与我相关
我的任务
分享
class Program
{
static void Main(string[] args)
{
Server server = new Server();
server.Start();
}
}
public class Server
{
private UdpClient server;
private Thread serverThread;
private IPEndPoint remotePoint;
public Server()
{
remotePoint = new IPEndPoint(IPAddress.Any, 0);
serverThread = new Thread(new ThreadStart(Run));
}
public void Start()
{
try
{
server = new UdpClient(22800);
serverThread.Start();
}
catch (Exception exp)
{
throw exp;
}
}
private void Run()
{
while (true)
{
byte[] msgBuffer = server.Receive(ref remotePoint);//不管客户端怎么发送,服务端始终在这里阻塞,说明一直没数据
//代码省略
}
}
}
class Program
{
static void Main(string[] args)
{
Client client = new Client("服务端IP");//
client.ConnectToServer();
}
}
public class Client
{
private UdpClient client;
private IPEndPoint hostPoint;
public Client(string serverIP)
{
hostPoint = new IPEndPoint(IPAddress.Parse(serverIP), 22800);
client = new UdpClient();
}
public void ConnectToServer()
{
// 发送消息到服务器
client.Send(数据信息, 数据长度, hostPoint);
}
}