471,319 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

BeginAccept doesn't work

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.Strea m,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");
}

Jan 27 '06 #1
11 3563
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.

--

Stoitcho Goutsev (100)

"Nuno Magalhaes" <nu************@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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.Strea m,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");
}

Jan 27 '06 #2
Nuno Magalhaes <nu************@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 27 '06 #3
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Nuno Magalhaes" wrote:
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.Strea m,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");
}

Jan 27 '06 #4
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.

Thanks for any help.

Jon wrote:
Nuno Magalhaes <nu************@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Jan 28 '06 #5
I understand that exceptions are very expensive but how can I, with
Console.WriteLine, see it's output? In the debug window?

Jan 28 '06 #6
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.Strea m,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?

Jon wrote:
Nuno Magalhaes <nu************@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Jan 28 '06 #7
Nuno Magalhaes <nu************@hotmail.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 28 '06 #8

"Nuno Magalhaes" <nu************@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
I understand that exceptions are very expensive but how can I, with
Console.WriteLine, see it's output? In the debug window?


Use Debug.WriteLine and a program like DebugView
(http://www.sysinternals.com/Utilities/DebugView.html)

This will allow you to see the output whether or not you are running in the
debugger
Jan 28 '06 #9
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:
Nuno Magalhaes <nu************@hotmail.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Jan 28 '06 #10
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.

"Nuno Magalhaes" <nu************@hotmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
| 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:
| > Nuno Magalhaes <nu************@hotmail.com> 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 - <sk***@pobox.com>
| > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
| > If replying to the group, please do not mail me too
|
Jan 28 '06 #11
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.

Willy Denoyette [MVP] wrote:
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.

"Nuno Magalhaes" <nu************@hotmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
| 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:
| > Nuno Magalhaes <nu************@hotmail.com> 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 - <sk***@pobox.com>
| > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
| > If replying to the group, please do not mail me too
|


Jan 28 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

149 posts views Thread by Christopher Benson-Manica | last post: by
reply views Thread by Joachim | last post: by
1 post views Thread by scott | last post: by
1 post views Thread by Chris Morse | last post: by
4 posts views Thread by Nuno Magalhaes | last post: by
10 posts views Thread by Clayton | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.