Re: Socket restart issue

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



I can't follow the threading of your application, which is what I think is
probably wrong, but I'd be much more likely to have something like this,
running in a separate thread:

// listen is the server socket.
// client is the socket connected when a client connects to the server

listen.Bind()
listen.Listen()
while ( !timeToExit )
{
client = listen.Accept()

// Do whatever with the client.

// Time to close the client and allow another connection.
client.Close();
client = null;
}

This basically allows just one client at a time, of course. You might want
to configure things so that a thread is spawned for each client, up to the
limit of your server, if multiple clients are allowed.

Paul T.

"Krupa" <krupa.p@xxxxxxxxx> wrote in message
news:1171479690.578533.295630@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Thanks for your reply Paul. I modified my code according to your
comments (please find below). "handler" is the listening socket and
"socket" is the socket object connected to the client. connectAgain()
is the function I use to disconnect the existing connected object and
begin listening for new clients.

When I disconnect the connected socket and do a BeginAccept on handler
again, I get an exception in ReadCallback, and of course it's not
connecting to the new client! Do you see where I am going wrong?

Thanks,
Krupa

####################################################################################################

public class SocketServer
{
private Socket handler;
public Socket socket;

public SocketServer()
{
ipHostInfo = Dns.Resolve(Dns.GetHostName());
ipAddress = ipHostInfo.AddressList[0];

handler = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
}

public void StartServer(int port)
{
localEndPoint = new IPEndPoint(ipAddress,
port);
handler.Bind(localEndPoint);
handler.Listen(1000);

allDone.Set();
connected = false;

handler.BeginAccept(new AsyncCallback(AcceptCallback),
handler);
allDone.WaitOne();
}

public void connectAgain()
{
socket.Shutdown(SocketShutdown.Both);
socket.Close();

handler.BeginAccept(new AsyncCallback(AcceptCallback), handler);
}

public void AcceptCallback(IAsyncResult ar)
{
Socket listenSocket = (Socket)ar.AsyncState;
Socket serverSocket = listenSocket.EndAccept(ar);

StateObject state = new StateObject();
state.workSocket = serverSocket;

serverSocket.BeginReceive(state.buffer, 0,
StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}

public void ReadCallback(IAsyncResult ar)
{
StateObject curr_state = (StateObject)ar.AsyncState;
socket = curr_state.workSocket;

int bytesRead = socket.EndReceive(ar);
}

private void Send(Socket handler, String data)
{
byte[] byteData = Encoding.ASCII.GetBytes(data);
handler.BeginSend(byteData, 0, byteData.Length, 0, new
AsyncCallback(SendCallback), handler);
}

private void SendCallback(IAsyncResult ar)
{
Socket sender = (Socket)ar.AsyncState;
int bytesSent = sender.EndSend(ar);
}
}


####################################################################################################



.



Relevant Pages

  • Re: How do I tell an object to free up an owned object from thta object itself?
    ... I tested running a sequence of connect/disconnect from the client ... client address for those connections that were active. ... In the ClientConnect event I create a handler object for processing ... >Socket is in fact a TServerClientWinSocket which acts as a end-connection ...
    (comp.lang.pascal.delphi.misc)
  • Reading data on Aysnchronous socket server
    ... To test this I have a simulated client that sends 100 byte packets. ... I have set up the socket server so that its buffer is bigger than this. ... // State object for reading client data asynchronously ... Socket handler = listener.EndAccept; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Socket restart issue
    ... "socket" is the socket object connected to the client. ... When I disconnect the connected socket and do a BeginAccept on handler ... public void StartServer ... public void connectAgain() ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Reading data on Aysnchronous socket server
    ... the connected socket as shown below in code I got form MSDN:- ... // State object for reading client data asynchronously ... Socket handler = listener.EndAccept; ...
    (microsoft.public.dotnet.languages.csharp)
  • [PATCH 0/5] [RFC] AF_RXRPC socket family implementation [try #3]
    ... These patches together supply secure client-side RxRPC connectivity as a Linux ... kernel socket family. ... presentation side is left to the client. ... Each connection goes to a particular "service". ...
    (Linux-Kernel)