BeginAccept doesn't work

N

Nuno Magalhaes

Does anyone know why the BeginAccept doesn't work? If, in the code
below, I do the normal Accept function I can get the client socket but
it seems that the callback isn't really called.
Here's the code:
-----------------------------
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.Parse(serverIP),serverPort);
socket.Bind(ep);
socket.Listen(10);
socket.BeginAccept(new AsyncCallback(AcceptConnection),socket);
}

private void AcceptConnection(IAsyncResult ar)
{
Socket socket=(Socket)ar.AsyncState;
clientSocket=socket.EndAccept(ar);
//throw new Exception("TCP Accepted on
#"+clientSocket.RemoteEndPoint.ToString());
throw new Exception("Yuhuu");
}
 
S

Stoitcho Goutsev \(100\)

Nuno,

I don't see the error in this code.
However the code doesn't show what serverIP and serverPort are. What I'd
suggest after calling socket.Listen check socket.Connected to make sure that
all the socket initialization went OK.
Second is make sure that there are no any firewalls or antivirus programs
that may block the access to the listening port.
 
J

Jon Skeet [C# MVP]

Nuno Magalhaes said:
Does anyone know why the BeginAccept doesn't work? If, in the code
below, I do the normal Accept function I can get the client socket but
it seems that the callback isn't really called.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

You haven't really posted enough of the code for us to try it for
ourselves.
 
G

Guest

One more comment - throwing exceptions to send yourself "messages" is
probably a good habit to get out of!

Console.WriteLine or System.Diagnostics.Debug.WriteLine or even
MessageBox.Show work just fine. Exceptions are expensive when they are
thrown, and should be used only when something goes really wrong.
Peter
 
N

Nuno Magalhaes

I understand that exceptions are very expensive but how can I, with
Console.WriteLine, see it's output? In the debug window?
 
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 another class it doesn't work.
I tried this code on a command line class and it works but it doesn't
work when I try to call the class from a form.
Anyone knows why?
 
J

Jon Skeet [C# MVP]

Nuno Magalhaes said:
It's a really short program cause it's the beginning...

The form: http://pwp.netcabo.pt/0141404701/RTSPServer.cs
The server: http://pwp.netcabo.pt/0141404701/TcpServer.cs

If you could help me I would be really appreciated... doing an Accept
on TcpServer.cs does accept the client (like Internet Explorer
"http://localhost:555/") so it's not from any firewall or antivirus.

Well, I'm not sure why there's a difference between Accept and
BeginAccept, but if you change the socket address to IPAddress.Any, it
works fine. I suspect the problem is that it's not listening on the
loopback address if you just specify the actual network interface
address.
 
N

Nuno Magalhaes

It works now. It was the GC that was marking the server object to be
collected. It didn't detect that the callback was present.

Writing a bit more of my RTSP server solved this problem, in a simple
thread on RTSPServer.cs.
 
W

Willy Denoyette [MVP]

Funny, this is what I told you what happened to your object and what you
could do to solve this issue in the other thread of yours, but in that same
thread you said:
<
None of the solutions actually worked but creating a thread and
determining if the client is connected (through a function) worked now., mind to share what you did exactly?

Willy.

| It works now. It was the GC that was marking the server object to be
| collected. It didn't detect that the callback was present.
|
| Writing a bit more of my RTSP server solved this problem, in a simple
| thread on RTSPServer.cs.
|
| Jon wrote:
| > > It's a really short program cause it's the beginning...
| > >
| > > The form: http://pwp.netcabo.pt/0141404701/RTSPServer.cs
| > > The server: http://pwp.netcabo.pt/0141404701/TcpServer.cs
| > >
| > > If you could help me I would be really appreciated... doing an Accept
| > > on TcpServer.cs does accept the client (like Internet Explorer
| > > "http://localhost:555/") so it's not from any firewall or antivirus.
| >
| > Well, I'm not sure why there's a difference between Accept and
| > BeginAccept, but if you change the socket address to IPAddress.Any, it
| > works fine. I suspect the problem is that it's not listening on the
| > loopback address if you just specify the actual network interface
| > address.
| >
| > --
| > Jon Skeet - <[email protected]>
| > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
| > If replying to the group, please do not mail me too
|
 
N

Nuno Magalhaes

Yes, you were right. Using the object elsewhere in the code makes the
object unavailable for GC and the BeginAccept then works inside the
TcpServer object.
 

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