110,477
社区成员
发帖
与我相关
我的任务
分享
public class StateObject
{
//服务器端
public Socket udpServer = null;
//接受数据缓冲区
public byte[] buffer = new byte[10*1024];
//远程终端
public EndPoint remoteEP;
public bool flag = false;
}
private void ServerForm_Load(object sender, EventArgs e)
{
StartServer startServer = new StartServer();
thread = new Thread(new ThreadStart(startServer.ServerBind));
thread.Start();
}
public void ServerBind()
{
//主机IP
IPEndPoint serverIp = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8686);
Socket udpServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpServer.Bind(serverIp);
IPEndPoint clientIp = new IPEndPoint(IPAddress.Any, 0);
state = new StateObject();
state.udpServer = udpServer;
state.remoteEP = (EndPoint)clientIp;
AsynRecive();
}
#endregion
#region 异步接受消息
public void AsynRecive()
{
lock (state)
{
state.udpServer.BeginReceiveFrom(state.buffer, 0, state.buffer.Length, SocketFlags.None, ref state.remoteEP,
new AsyncCallback(ReciveCallback), null);
receiveDone.WaitOne();
}
}
#endregion
#region 异步接受消息回调函数
public void ReciveCallback(IAsyncResult asyncResult)
{
if (asyncResult.IsCompleted)
{
//获取发送端的终节点
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 0);
EndPoint remoteEP = (EndPoint)ipep;
state.udpServer.EndReceiveFrom(asyncResult, ref remoteEP);
data = new byte[10*1024];
state.buffer.CopyTo(data, 0);
state.buffer = new byte[10*1024];
int i = 0;
///取出命令
for (i = 0; i < leng; i++)
{
if (Convert.ToInt32(data[i]) == 0)
break;
}
byte[] cmdbyte = new byte[i];
for (int k = 0; k < i; k++)
{
cmdbyte[k] = data[k];
}
commandStr = Encoding.ASCII.GetString(cmdbyte);
if(!commandStr.Equals("connect"))
MessageBox.Show(commandStr, "kkk");
handleMsg(commandStr);
state.remoteEP = remoteEP;
receiveDone.Set();
AsynRecive();//继续接受消息
}
}
#endregion
#region 异步发送消息
public void AsynSend(byte[] buffer)
{
state.udpServer.BeginSendTo(buffer, 0, buffer.Length, SocketFlags.None, state.remoteEP,
new AsyncCallback(SendCallback), null);
sendDone.WaitOne();
}
#endregion
#region 异步发送消息回调函数
public void SendCallback(IAsyncResult asyncResult)
{
//消息发送完毕
if (asyncResult.IsCompleted)
{
state.udpServer.EndSendTo(asyncResult);
}
sendDone.Set();
}
#endregion
private void handleMsg(String msg)
{
}
public class StateObject
{
//客户端套接字
public Socket udpClient = null;
//接收信息缓冲区
public byte[] buffer = new byte[10*1024];
//服务器端终节点
public IPEndPoint serverIp;
//远程终端节点
public EndPoint remoteEP;
}
private void 登陆_Load(object sender, EventArgs e)
{
contactWithServer = new ContactWithServer(this);
clientcontactserverThread = new Thread(new ThreadStart(contactWithServer.AsynRecive));
clientcontactserverThread.Start();
}
public ContactWithServer(登陆 loginForm)
{
this.loginForm = loginForm;
state = new StateObject();
state.udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
state.serverIp = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8686);
state.remoteEP = (EndPoint)(new IPEndPoint(IPAddress.Any, 0));
//此处注意:
// 由于当前是客户端,所以没有绑定终节点
// 不可直接接收消息,必须先向其他终端发送信息告知本机终节点
string msg = "connect";
byte[] buffer = Encoding.ASCII.GetBytes(msg);
AsynSend(buffer);
}
#region 异步接收来自其他终端发送的消息
public void AsynRecive()
{
state.udpClient.BeginReceiveFrom(state.buffer, 0, state.buffer.Length, SocketFlags.None, ref state.remoteEP,
new AsyncCallback(ReciveCallback), null);
receiveDone.WaitOne();
}
#endregion
#region 异步接收来自其他终端发送的消息回调函数
public void ReciveCallback(IAsyncResult asyncResult)
{
//信息接收完成
if (asyncResult.IsCompleted)
{
state.udpClient.EndReceiveFrom(asyncResult, ref state.remoteEP);
data=new byte[10*1024];
state.buffer.CopyTo(data,0);
if (data.Length < 20) //只有命令
commandStr = Encoding.ASCII.GetString(data);
else
{
int i = 0;
///取出命令
for (i = 0; i < leng; i++)
{
if (Convert.ToInt32(data[i]) == 0)
break;
}
byte[] cmdbyte = new byte[i];
for (int k = 0; k < i; k++)
{
cmdbyte[k] = data[k];
}
commandStr = Encoding.ASCII.GetString(cmdbyte);
}
MessageBox.Show(commandStr, "来自服务器的命令!");
handleMsg(commandStr);
state.buffer=new byte[10*1024];
AsynRecive();
receiveDone.Set();
}
}
#endregion
#region 异步发送消息
public void AsynSend(byte[] buffer)
{
state.udpClient.BeginSendTo(buffer, 0, buffer.Length, SocketFlags.None, state.serverIp,
new AsyncCallback(SendCallback), null);
sendDone.WaitOne();
}
#endregion
#region 异步发送消息回调函数
public void SendCallback(IAsyncResult asyncResult)
{
//消息发送完成
if (asyncResult.IsCompleted)
{
state.udpClient.EndSendTo(asyncResult);
}
sendDone.Set();
}
#endregion