Hi all,
I am new to sockets and the way .NET implements it. I am trying to develop a
small test app. where a client connects to a server application and server
and sends messages. The server displays it in a WinForm. I am using asynch.
socket calls to achieve that. I have two issues with this.
First, i can only display the first message sent to the server. How can i
set the server run in a loop to listen to the incoming messages. The server
code here is below.
public ChatServer()
{
System.Net.IPHostEntry ipHostEntry =
Dns.Resolve(System.Net.Dns.GetHostName());
int port = FindUnusedPort(ipHostEntry.AddressList[0]);
System.Net.IPEndPoint ipEndPoint = new
IPEndPoint(ipHostEntry.AddressList[0],port);
listener = new
Socket(ipEndPoint.AddressFamily,SocketType.Stream,ProtocolType.Tcp);
listener.Bind(ipEndPoint);
listener.Listen(10);
IAsyncResult ar = listener.BeginAccept(new
System.AsyncCallback(OnConnectRequst),listener);
}
public void OnConnectRequst(IAsyncResult result)
{
try
{
listener = (Socket ) result.AsyncState;
m_socClient = listener.EndAccept(result);
IAsyncResult ar =
m_socClient.BeginReceive(m_DataBuffer,0,m_DataBuffer.Length,SocketFlags.None
,new AsyncCallback(OnRecieve),m_socClient);
}
catch (SocketException e)
{
Console.WriteLine( e.Message);
}
}
public void OnMessageRecieved(string message)
{
this.messageRecieved(message);
}
public void OnRecieve(IAsyncResult result)
{
m_socClient = (Socket) result.AsyncState;
int iRx = 0;
iRx = m_socClient.EndReceive(result);
if (iRx>1)
{
string message = Encoding.ASCII.GetString(m_DataBuffer);
this.OnMessageRecieved(message);
}
}
Second, The client sends a message and the server recieves it. I close the
client application and then reopen it connect to server and send message.
But the server doesn't recieves it.
Navin
Ken Kolda - 31 Dec 2004 18:51 GMT
In your OnReceive() method you need to call BeginReceive() on the socket
again to receive any additional data, e.g.
public void OnRecieve(IAsyncResult result)
{
m_socClient = (Socket) result.AsyncState;
int iRx = 0;
iRx = m_socClient.EndReceive(result);
if (iRx>1)
{
string message = Encoding.ASCII.GetString(m_DataBuffer);
this.OnMessageRecieved(message);
m_socClient.BeginReceive(m_DataBuffer,0,m_DataBuffer.Length,SocketFlags.None,new
AsyncCallback(OnRecieve),m_socClient);
}
}
That said, you have a number of issues withi this code. For example, with
your current implementation, you'll only be able to accept a single client.
That's because you don't call BeginAccept() again in your OnConnectRequest()
method. Also, you're using a variable named m_Buffer for your receive
buffer, which is presumably a class variable. Since your server will
potentially be receiving data from multiple clients in different threads at
the same time, you have to have a different buffer per socket.
I'd advise looking at sample code to see how this is done because you should
certainly be able to find lots of examples. Just google "asynchronous
sockets .NET sample code".
Ken
> Hi all,
>
[quoted text clipped - 64 lines]
>
> Navin