Socket restart issue

K

Krupa

Hi All,

I am working on a CF 2.0 C# application for windows CE 5.0 device. My
application has a socket server feature in it and we have a Flash
application as a socket client.

The Flash client connects to the C# server and data flows between
them. At some point in time, the C# app has to close the Flash movie
and restart it at a later point in time. Here lies the issue. When the
C# app relaunches the swf, the swf starts looking for the server. But
the server stops listening to clients any more. I tried shutting down
and closing the socket object and creating that object (with the same
name) again and start listening, but it gave me a socket exception. Is
it not legal to recreate the socket object with the same name? How
else can I go about it? I cannot use new names each time I create
socket object. Also, I cannot use different IP address or port numbers
as they are hard-coded in Flash.

Thanks,
Krupa
 
P

Paul G. Tobey [eMVP]

I don't understand. When you create a listening socket, that's socket #1.
You should never have to close that socket. When a client makes a
connection, your call to Accept() on socket #1 returns and gives you a
connected socket with the client, socket #2. You can do whatever you want,
at that point. When you no longer want to talk to the client, close socket
#2, *NOT* socket #1. Call Accept() on socket #1 again and, when another
client tries to connect, you'll get another new, connected socket back.

Paul T.
 
K

Krupa

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);
}
}


####################################################################################################
 
P

Paul G. Tobey [eMVP]

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 said:
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);
}
}


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top