using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace UDPClient
{
class UDPServer
{
private static IPEndPoint ip;
private static Socket newsock;
private static UDPServer instance=new UDPServer();
private UDPServer()
{
}
public static UDPServer getInstance()
{
return instance;
}
public void start()
{
ip = new IPEndPoint(IPAddress.Any, 8001);
newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
newsock.Bind(ip);
}
public String recieve()
{
byte[] data = new byte[1024];
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)(sender);
int length=newsock.ReceiveFrom(data, ref Remote);//获取client内容
response(Remote);
return Encoding.ASCII.GetString(data,0,length) ;
}
private void response(EndPoint remote)
{
string msg = "1";
byte[] data = Encoding.ASCII.GetBytes(msg);
newsock.SendTo(data,data.Length,SocketFlags.None,remote);
}
}
}
c# UDP
最新推荐文章于 2024-03-09 08:23:11 发布