Re: Socket restart issue by Paul
Paul
Wed Feb 14 14:10:29 CST 2007
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@gmail.com> wrote in message
news:1171479690.578533.295630@k78g2000cwa.googlegroups.com...
> 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);
> }
> }
>
>
> ####################################################################################################
>