我正在学习客户端/服务器编程,我正在制作异步客户端/服务器聊天应用程序。
我可以将客户端连接到服务器并发送消息但是我在向客户端发回相同的消息(仅用于测试目的)时遇到问题。任何帮助表示赞赏...
客户代码
byte[] dataBuffer = new byte[10];
public AsyncCallback callBack;
public Socket clientSocket;
IAsyncResult ar;
Random rnd=new Random();
private void btnSend_Click(object sender, RoutedEventArgs e)
{
try
{
Object objData = txtMessage.Text;
byte[] byData = Encoding.ASCII.GetBytes(objData.ToString());
if (clientSocket != null)
{
clientSocket.Send(byData);
lbxMessages.Items.Add(txtMessage.Text);
}
txtMessage.Text = "";
btnSend.IsEnabled = false;
}
catch(SocketException se)
{
MessageBox.Show(se.Message);
}
}
//SocketPacket CLASS
public class SocketPacket
{
public Socket thisSocket;
public byte[] dataBuffer = new byte[10];//test no, prevous [1]
}
//METHODS
private void WaitForData()
{
try
{
if (callBack == null)
{
callBack = new AsyncCallback(OnDataReceived);
}
SocketPacket sckPack = new SocketPacket();
sckPack.thisSocket = clientSocket;
ar = clientSocket.BeginReceive(sckPack.dataBuffer, 0, sckPack.dataBuffer.Length, SocketFlags.None, callBack, sckPack);
}
catch(SocketException se)
{
MessageBox.Show(se.Message);
}
}
public void OnDataReceived(IAsyncResult ar)
{
try
{
SocketPacket sckID = (SocketPacket)ar.AsyncState;
int iRx = sckID.thisSocket.EndReceive(ar);
char[] chars = new char[iRx + 1];
Decoder d = Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(sckID.dataBuffer, 0, iRx, chars, 0);
String szData = new String(chars);
this.Dispatcher.Invoke((Action)(() =>
{
lbxMessages.Items.Add(txtMessage.Text + szData);
}));
WaitForData();
}
catch (ObjectDisposedException)
{
Debugger.Log(0, "1", "\n OnDataRecieved: Socket has been closed\n");
}
catch(SocketException se)
{
MessageBox.Show(se.Message);
}
}
服务器代码
private void OnClientConnect(IAsyncResult asyncResult)
{
try
{
workerSocket[clientCount] = listenSocket.EndAccept(asyncResult);
WaitForData(workerSocket[clientCount]);
++clientCount;
if (clientCount<4)//allow max 3 clients
{
String str = String.Format("Client # {0} connected", clientCount);
this.Dispatcher.Invoke((Action)(() =>
{
lbxMessages.Items.Add(str);
lblConnectionStatus.Content =clientCount + " Connected";
}));
listenSocket.BeginAccept(OnClientConnect, null);
}
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\n OnClientConnection: Socket has been closed\n");
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
public class SocketPacket
{
public Socket currentSocket;
public byte[] dataBuffer = new byte[50];//allowing the 50 digist to be sent at once
}
private void WaitForData(Socket socket)
{
try
{
if (workerCallBack == null)
{
workerCallBack = OnDataReceived;
}
SocketPacket sckPack = new SocketPacket();
sckPack.currentSocket = socket;
socket.BeginReceive(sckPack.dataBuffer, 0, sckPack.dataBuffer.Length, SocketFlags.None, workerCallBack, sckPack);
}
catch(SocketException se)
{
MessageBox.Show(se.Message);
}
}
public void OnDataReceived(IAsyncResult asyncResult)
{
try
{
SocketPacket socketData = (SocketPacket)asyncResult.AsyncState;
int iRx = 0;
iRx = socketData.currentSocket.EndReceive(asyncResult);
char[] chars = new char[iRx];
Decoder decoder = Encoding.UTF8.GetDecoder();
int charLen = decoder.GetChars(socketData.dataBuffer, 0, iRx, chars, 0);
String receivedData = new String(chars);
this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => lbxMessages.Items.Add(receivedData)));
//Does not work - does not send same message back to client
//byte[] byData = Encoding.ASCII.GetBytes(receivedData);
//if (listenSocket != null)
//{
// listenSocket.Send(byData);
//}
WaitForData(socketData.currentSocket);
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\n OnDataRecieved: Socket has been closed\n");
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
答案 0 :(得分:0)
您应该使用相同的套接字来发送和接收数据。你在socketData.currentSocket上调用EndReceive,所以你也应该调用socketData.currentSocket.Send。
在服务器端有两个插槽。一个用于&#34; listen&#34;,你在上面调用accept。一旦客户端连接,accept调用就会为该客户端连接创建一个套接字,然后您可以使用该套接字为该特定客户端发送和接收数据。
你没有使用&#34;听&#34;用于发送/接收数据的套接字。它没有连接到特定的连接。它仅用作接受新客户端连接的方式。