473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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 3662
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
0
by: Joachim | last post by:
When closing down my server I get the following exception An unhandled exception of type 'System.InvalidOperationException' occurred in system.dll Additional information: AcceptCallback ...
1
by: scott | last post by:
Hi all hope some one can help me with this prob because it is really annoying me and I can't seem to solve it. Just like to say thx to any one that can offer any help. Ok the prob. I have a...
1
by: Chris Morse | last post by:
WARNING: Verbosity: skip to the very bottom paragraph for succinct version of my question.) Hi- I can't seem to find an answer to this. I am playing around with a variation of the ".NET...
4
by: Nuno Magalhaes | last post by:
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...
5
by: darthghandi | last post by:
I've created a class to listen to all interfaces and do a BeginAccept(). Once it gets a connection, it passes the connected socket off and stores it in a List. Next, it continues to listen for...
10
by: Clayton | last post by:
Hi all, I'm trying to develop a server that listens to incoming calls using the asycnhronous methods BeginAccept / EndAccept. I start the server (till this point it is ok) and few seconds later...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.