关于 c# socket的多客户端同时在线通信问题?
我要用异步通信方式实现一对多同时通信,我写的代码这么老是只能有一个客户端,请大家帮忙看看 --------------------编程问答-------------------- public Socket Serverlistener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);public const int buffersize = 1024;
public byte[] ReceiveBuffer= new byte[buffersize];
public int i = 0;
private void btn_listen_Click(object sender, EventArgs e)
{
lstbox_status.Items.Add("开始监听,等待连接中……");
btn_start.Enabled = false;
IPAddress ServerIpAddress = IPAddress.Parse(txtbox_ServIP.Text);
IPEndPoint localEndPoint = new IPEndPoint(ServerIpAddress,Convert.ToInt16(txtbox_ServPort.Text));
Serverlistener.Bind(localEndPoint);
Serverlistener.Listen(100);
Serverlistener.Accept(new AsyncCallback(AcceptCallback), Serverlistener);
}
public void AcceptCallback(IAsyncResult ar)
{
// Get the socket that handles the client request.
Socket Serverlistener = (Socket)ar.AsyncState;
socket ClientSocket = Serverlistener.EndAccept(ar);
ClientSocket.BeginReceive(ReceiveBuffer, 0, buffersize, 0, new AsyncCallback(ReadCallback), ClientSocket);
}
public void ReadCallback(IAsyncResult ar)
{
String content = String.Empty;
// Retrieve the state object and the handler socket
// from the asynchronous state object.
ClientSocket = (Socket)ar.AsyncState;
//Socket handler = ClientSocket.workSocket;
// Read data from the client socket.
int bytesRead = ClientSocket.EndReceive(ar);
if (bytesRead > 0)
{
content = Encoding.ASCII.GetString(ReceiveBuffer, 0, bytesRead);
if (content.IndexOf("<EOF>") > -1)
{
Send(ClientSocket, content);
}
else
{
MessageBox.Show(content);
ClientSocket.BeginReceive(ReceiveBuffer, 0, buffersize, 0, new AsyncCallback(ReadCallback), ClientSocket);
}
}
} --------------------编程问答-------------------- mark --------------------编程问答-------------------- 你只使用了一个ClientSocket对象吧?
public void AcceptCallback(IAsyncResult ar)
{
// Get the socket that handles the client request.
Socket Serverlistener = (Socket)ar.AsyncState;
socket ClientSocket = Serverlistener.EndAccept(ar);
ClientSocket.BeginReceive(ReceiveBuffer, 0, buffersize, 0, new AsyncCallback(ReadCallback), ClientSocket);
}
在这其中,你得到了连接到服务器端的这个客户端对应在服务器端的SOCKET:ClientSocket。
你应当把这个ClientSocket保存起来,然后在根据情况处理,从你给出的代表中我未看出你是如何保存这个ClientSocket的。我猜测你只保存了一个ClientSocket因此,当有新的客户端连接时,旧的ClientSocket对象就被删除了。 --------------------编程问答-------------------- 帮忙顶先! --------------------编程问答-------------------- 服务器端要用个死循环的
在while(true)里边用accptsocket --------------------编程问答-------------------- private void btn_listen_Click(object sender, EventArgs e)
{
lstbox_status.Items.Add( "开始监听,等待连接中…… ");
btn_start.Enabled = false;
IPAddress ServerIpAddress = IPAddress.Parse(txtbox_ServIP.Text);
IPEndPoint localEndPoint = new IPEndPoint(ServerIpAddress,Convert.ToInt16(txtbox_ServPort.Text));
Serverlistener.Bind(localEndPoint);
Serverlistener.Listen(100);
Serverlistener.BeginAccept(new AsyncCallback(AcceptCallback), Serverlistener); //BeginAccept异步接受无需死循环,不过在接受了一个连接后,在回调AcceptCallback最后还要重新对Serverlistener开启这个方法,
}
public void AcceptCallback(IAsyncResult ar)
{
// Get the socket that handles the client request.
Socket Serverlistener = (Socket)ar.AsyncState;
socket ClientSocket = Serverlistener.EndAccept(ar);
ClientSocket.BeginReceive(ReceiveBuffer, 0, buffersize, 0, new AsyncCallback(ReadCallback), ClientSocket);
Serverlistener.BeginAccept(new AsyncCallback(AcceptCallback), Serverlistener);
//不过在这个方法退出前必须保存 ClientSocket对象,否则你之前的客户端连接将被释放掉,所以,你那个地方只有最后一个客户端在其作用,之前的都将丢失 }
--------------------编程问答-------------------- 3楼的跟6楼的都正确,不保存的话,就会只有最后那个客户端了! --------------------编程问答-------------------- 帮顶! --------------------编程问答-------------------- 用多线程实现每个线程产生一个clientSOCKET比较好 --------------------编程问答-------------------- public void AcceptCallback(IAsyncResult ar)
{
// Get the socket that handles the client request.
Socket Serverlistener = (Socket)ar.AsyncState;
socket ClientSocket = Serverlistener.EndAccept(ar);
ClientSocket.BeginReceive(ReceiveBuffer, 0, buffersize, 0, new AsyncCallback(ReadCallback), ClientSocket);
Serverlistener.Accept(new AsyncCallback(AcceptCallback), Serverlistener);
//再加上这句就行了
} --------------------编程问答-------------------- up up --------------------编程问答-------------------- 在while(true)里边用accptsocket --------------------编程问答-------------------- 不知道可以用集合保存不呢? --------------------编程问答-------------------- 学习一下,最近也在学习这个
补充:.NET技术 , C#