在网上找了个例子,服务器端只有一个端口负责通信,现在要做C/S多窗体应用程序,希望服务器再收到客户端的第一个数据后,第二个数据从另一个端口收到,并通过这个端口与客户端进行会话。想了好久都不知道怎样改才是正确的,大家帮忙看下
服务器端:
public class StateObject
{
//服务器端
public Socket udpServer = null;
//接受数据缓冲区
public byte[] buffer = new byte[1024];
//远程终端
public EndPoint remoteEP;
}
public class AsynUdpServer
{
public StateObject state; #region 服务器绑定终端节点
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);
Console.WriteLine("server ready...");
IPEndPoint clientIp = new IPEndPoint(IPAddress.Any, 0);
state = new StateObject();
state.udpServer = udpServer;
state.remoteEP = (EndPoint)clientIp;
AsynRecive();
}
#endregion #region 异步接受消息
public void AsynRecive()
{
state.udpServer.BeginReceiveFrom(state.buffer, 0, state.buffer.Length, SocketFlags.None, ref state.remoteEP,
new AsyncCallback(ReciveCallback), null);
}
#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);
Console.WriteLine("server<--<--client:{0}", Encoding.UTF8.GetString(state.buffer));
//向发送端通知:收到消息
state.remoteEP = remoteEP;
AsynSend("收到消息");
//继续接受消息
AsynRecive();
}
}
#endregion #region 异步发送消息
public void AsynSend(string message)
{
Console.WriteLine("server-->-->client:{0}", message);
byte[] buffer = Encoding.UTF8.GetBytes(message);
state.udpServer.BeginSendTo(buffer, 0, buffer.Length, SocketFlags.None, state.remoteEP,
new AsyncCallback(SendCallback), null);
}
#endregion #region 异步发送消息回调函数
public void SendCallback(IAsyncResult asyncResult)
{
//消息发送完毕
if (asyncResult.IsCompleted)
{
state.udpServer.EndSendTo(asyncResult);
}
}
#endregion
}
AsynUdpServer asynServer = new AsynUdpServer();
asynServer.ServerBind();客户端代码
public class StateObject
{
//客户端套接字
public Socket udpClient = null;
//接收信息缓冲区
public byte[] buffer = new byte[1024];
//服务器端终节点
public IPEndPoint serverIp;
//远程终端节点
public EndPoint remoteEP;
} /// <summary>
/// Udp协议异步通讯类(客户端)
/// </summary>
public class AsynUdpClient
{
#region 容器对象
/// <summary>
/// 容器对象
/// </summary>
public StateObject state;
#endregion #region 客户端初始化
public void InitClient()
{
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));
//此处注意:
// 由于当前是客户端,所以没有绑定终节点
// 不可直接接收消息,必须先向其他终端发送信息告知本机终节点
AsynSend("第1次发送消息");
AsynSend("第2次发送消息");
AsynRecive();
}
#endregion #region 异步接收来自其他终端发送的消息
public void AsynRecive()
{
state.udpClient.BeginReceiveFrom(state.buffer, 0, state.buffer.Length, SocketFlags.None, ref state.remoteEP,
new AsyncCallback(ReciveCallback), null);
}
#endregion #region 异步接收来自其他终端发送的消息回调函数
public void ReciveCallback(IAsyncResult asyncResult)
{
//信息接收完成
if (asyncResult.IsCompleted)
{
state.udpClient.EndReceiveFrom(asyncResult, ref state.remoteEP);
Console.WriteLine("client<--<--{0}:{1}", state.remoteEP.ToString(), Encoding.UTF8.GetString(state.buffer));
AsynRecive();
}
}
#endregion #region 异步发送消息
public void AsynSend(string message)
{
Console.WriteLine("client-->-->{0}:{1}", state.serverIp.ToString(), message);
byte[] buffer = Encoding.UTF8.GetBytes(message);
state.udpClient.BeginSendTo(buffer, 0, buffer.Length, SocketFlags.None, state.serverIp,
new AsyncCallback(SendCallback), null);
}
#endregion #region 异步发送消息回调函数
public void SendCallback(IAsyncResult asyncResult)
{
//消息发送完成
if (asyncResult.IsCompleted)
{
state.udpClient.EndSendTo(asyncResult);
}
}
#endregion
}AsynUdpClient asynClient = new AsynUdpClient();
asynClient.InitClient();
大家多指导下
服务器端:
public class StateObject
{
//服务器端
public Socket udpServer = null;
//接受数据缓冲区
public byte[] buffer = new byte[1024];
//远程终端
public EndPoint remoteEP;
}
public class AsynUdpServer
{
public StateObject state; #region 服务器绑定终端节点
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);
Console.WriteLine("server ready...");
IPEndPoint clientIp = new IPEndPoint(IPAddress.Any, 0);
state = new StateObject();
state.udpServer = udpServer;
state.remoteEP = (EndPoint)clientIp;
AsynRecive();
}
#endregion #region 异步接受消息
public void AsynRecive()
{
state.udpServer.BeginReceiveFrom(state.buffer, 0, state.buffer.Length, SocketFlags.None, ref state.remoteEP,
new AsyncCallback(ReciveCallback), null);
}
#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);
Console.WriteLine("server<--<--client:{0}", Encoding.UTF8.GetString(state.buffer));
//向发送端通知:收到消息
state.remoteEP = remoteEP;
AsynSend("收到消息");
//继续接受消息
AsynRecive();
}
}
#endregion #region 异步发送消息
public void AsynSend(string message)
{
Console.WriteLine("server-->-->client:{0}", message);
byte[] buffer = Encoding.UTF8.GetBytes(message);
state.udpServer.BeginSendTo(buffer, 0, buffer.Length, SocketFlags.None, state.remoteEP,
new AsyncCallback(SendCallback), null);
}
#endregion #region 异步发送消息回调函数
public void SendCallback(IAsyncResult asyncResult)
{
//消息发送完毕
if (asyncResult.IsCompleted)
{
state.udpServer.EndSendTo(asyncResult);
}
}
#endregion
}
AsynUdpServer asynServer = new AsynUdpServer();
asynServer.ServerBind();客户端代码
public class StateObject
{
//客户端套接字
public Socket udpClient = null;
//接收信息缓冲区
public byte[] buffer = new byte[1024];
//服务器端终节点
public IPEndPoint serverIp;
//远程终端节点
public EndPoint remoteEP;
} /// <summary>
/// Udp协议异步通讯类(客户端)
/// </summary>
public class AsynUdpClient
{
#region 容器对象
/// <summary>
/// 容器对象
/// </summary>
public StateObject state;
#endregion #region 客户端初始化
public void InitClient()
{
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));
//此处注意:
// 由于当前是客户端,所以没有绑定终节点
// 不可直接接收消息,必须先向其他终端发送信息告知本机终节点
AsynSend("第1次发送消息");
AsynSend("第2次发送消息");
AsynRecive();
}
#endregion #region 异步接收来自其他终端发送的消息
public void AsynRecive()
{
state.udpClient.BeginReceiveFrom(state.buffer, 0, state.buffer.Length, SocketFlags.None, ref state.remoteEP,
new AsyncCallback(ReciveCallback), null);
}
#endregion #region 异步接收来自其他终端发送的消息回调函数
public void ReciveCallback(IAsyncResult asyncResult)
{
//信息接收完成
if (asyncResult.IsCompleted)
{
state.udpClient.EndReceiveFrom(asyncResult, ref state.remoteEP);
Console.WriteLine("client<--<--{0}:{1}", state.remoteEP.ToString(), Encoding.UTF8.GetString(state.buffer));
AsynRecive();
}
}
#endregion #region 异步发送消息
public void AsynSend(string message)
{
Console.WriteLine("client-->-->{0}:{1}", state.serverIp.ToString(), message);
byte[] buffer = Encoding.UTF8.GetBytes(message);
state.udpClient.BeginSendTo(buffer, 0, buffer.Length, SocketFlags.None, state.serverIp,
new AsyncCallback(SendCallback), null);
}
#endregion #region 异步发送消息回调函数
public void SendCallback(IAsyncResult asyncResult)
{
//消息发送完成
if (asyncResult.IsCompleted)
{
state.udpClient.EndSendTo(asyncResult);
}
}
#endregion
}AsynUdpClient asynClient = new AsynUdpClient();
asynClient.InitClient();
大家多指导下
要实现通信:
*必须S\C都处于监听
*必须保持异步通信的端口一致。比方:【S send(8080) C recive(8080)】 【 C send(9090) S recive(9090)】
*至于你说的用两个端口。必要性不是很大。只要对发送的消息进行预处理。接收后进行类型判断即可。
如果是这样,那服务端收到客户端的第一次请求后,启动一个新的UDP端口监听,然后将这个新的端口号发送给客户端,客户端收到后就改变通信对象端口就是了。
是啊。你就是没弄清楚UDP。不像TCP,是无连接的。