BeginAccept Question

N

Nuno Magalhaes

One thing I've noticed is that if I try that code on a simple form load
event it works ok like this:

....form load event...
Socket socket=new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint ep=new IPEndPoint(IPAddress.Parse(serverIP),serverPort);
socket.Bind(ep);
socket.Listen(10);
socket.BeginAccept(new AsyncCallback(AcceptConnection),socket);
....
private void AcceptConnection(IAsyncResult ar)
{
throw new Exception("Yuhuu");
}

But when I move this code into a private class it doesn't work.
Anyone knows why?

Below is the code:
---------------------------------------
using System;
using System.Net;
using System.Net.Sockets;

namespace RTSPServer
{
public class TcpServer:IDisposable
{
private Socket socket;
private Socket clientSocket;
public TcpServer(string serverIP,int serverPort)
{
clientSocket=null;
socket=new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint ep=new
IPEndPoint(IPAddress.Any,555);//IPAddress.Parse(serverIP),serverPort);
socket.Bind(ep);
socket.Listen(10);
socket.BeginAccept(new AsyncCallback(AcceptConnection),socket);
}
private void AcceptConnection(IAsyncResult ar)
{
socket=(Socket)ar.AsyncState;
clientSocket=socket.EndAccept(ar);
throw new Exception("TCP Accepted on
#"+clientSocket.RemoteEndPoint.ToString());
}
public void Dispose()
{
if(clientSocket!=null)
{
clientSocket.Close();
}
socket.Close();
}
}
}

I think it has something to do with the callback that is *not* called.
If I do an Accept instead of a BeginAccept it works waits ok until a
client establish a connection... but the BeginAccept callback won't
work while inside the private class TcpServer.

I tried from a command line project also and it works but not when
called from a System.Windows.Forms.Form object.
 
N

Nuno Magalhaes

If someone could test the two source files posted above, I would be
REALLY appreciated cause it's possibly a bug or something easy I'm not
seeing.

Thank you very much.
 
W

Willy Denoyette [MVP]

| If someone could test the two source files posted above, I would be
| REALLY appreciated cause it's possibly a bug or something easy I'm not
| seeing.
|
| Thank you very much.
|

You must keep the TcpServer instance "alive", you set Server by creating an
instance of TcpServer in StartButton_Click, but you don't use the variable
anywhere else in the program, so the JIT compiler reports the variable
'eligible for collection' by setting it's value to null.
Try with a static reference like this:
static TcpServer Server;
but better would be to redesign your application such that you don't create
an instance in a click handler, your handler should only call a method on
the class (StartServer, StopServer etc...).



Willy.
 
N

Nuno Magalhaes

None of the solutions actually worked but creating a thread and
determining if the client is connected (through a function) worked now.

Thanks for your reply.
 

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