[C#] More TCP problems!

Soldato
Joined
12 Apr 2004
Posts
11,788
Location
Somewhere
Been playing around with async network streaming as Reezer suggested. I have a simple server that runs as a console app and displays any messages that are sent to it.

It works brilliantly when I run it locally, but when it's across the network, the server doesn't pick anything up, despite Ethereal showing incoming TCP packets.

Here's the relevant code:
Code:
public void Start()
{
	Console.WriteLine("Server starting...");

	m_Listener.Start();
	m_IsRunning = true;
	m_IsStopRequested = false;

	WaitCallback callback = Listen;
	ThreadPool.QueueUserWorkItem(callback);
}

...

private void Listen(object state)
{
	Console.WriteLine("Listening for connections on port {0}...", m_Port);
	Console.WriteLine();
	while (!m_IsStopRequested)
	{
		Socket socket = m_Listener.AcceptSocket();
		ClientHandler handler = new ClientHandler(socket);
		handler.StartRead();
	}

	Console.WriteLine("Server stopped.");
	m_IsRunning = false;
	m_IsStopRequested = false;
}

I've tried stepping through the code, and it seems to hang at the m_Listener.AcceptSocket() method call, implying that it can't detect any incoming connections.

Can anyone shead any light on this?
 
Associate
Joined
27 Oct 2002
Posts
897
It is supposed to stop there - it's a blocking call to wait for an incoming socket.

Can you step through both programs? Client and server side by side?

Get the server to the AcceptSocket line and then start the client and step until it gets to the Connect(...) line and see what happens.

If that doesn't help, get your packet scanner on both ends of the connection and see if the connection packets are going missing or something. Not quite sure how to explain the client being able to send stuff when it's not connected but it's a start...

It might be easier to make the AcceptSocket method Async too and then put a breakpoint in the callback - then you will only break when something has actually happened rather than when you are waiting for something to happen.

Also, if you are doing the reading in a thread/async you'll have to put a breakpoint in there somewhere or you'll just stick on the main thread when you step through.
 
Last edited:
Associate
Joined
15 Jun 2005
Posts
630
For async sockets, you need to instantiate a new Socket, and call BeginAccept on it. This will then callback into your async handler, like so:
Code:
s.BeginAccept(new AsyncCallback(AcceptReceiveDataCallback), s);

static void AcceptClientConnection(IAsyncResult ar)
{
  Socket listenerSocket = (Socket)ar.AsyncState;

  ...
}

Keep in mind that this will only handle a single request and exit, so put s.BeginAccept in a loop in your listener thread. Use a WaitHandle like ManualResetEvent for the AcceptClientConnection function to signal to the loop that is can listen for another connection.

As for it not working, which IP address are you binding to? I'm guessing 127.0.0.1?
 
Last edited:
Associate
Joined
27 Oct 2002
Posts
897
topbanana said:
Keep in mind that this will only handle a single request and exit, so put s.BeginAccept in a loop in your listener thread. Use a WaitHandle like ManualResetEvent for the AcceptClientConnection function to signal to the loop that is can listen for another connection.

Isn't it easier to put a Socket.BeginAccept inside the Async callback? That way you needn't bother with the loop or any reset events?

You may need to add some synchronisation to make it thread safe but if you are just trying 1 connection at a time to get started you needn't bother yet.
 
Associate
Joined
15 Jun 2005
Posts
630
Reezer said:
Isn't it easier to put a Socket.BeginAccept inside the Async callback? That way you needn't bother with the loop or any reset events?

Er which callback are we talking about here - the thread entry point? Yes it would be easier - thought he was talking about getting async sockets working :confused:

Anyhoo, like you say Reezer without a connection being established I can't imagine what's being seen in Ethereal other than the connection attempt itself.
 
Soldato
OP
Joined
12 Apr 2004
Posts
11,788
Location
Somewhere
edit: Sorry, I was being an idiot, I was initialising the TcpListener with the IP 127.0.0.1 rather than IPAddress.Any :o

Works now, thanks for the replies :)
 
Last edited:
Back
Top Bottom