473,692 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(strin g serverIP,int serverPort)
{
clientSocket=nu ll;
socket=new
Socket(AddressF amily.InterNetw ork,SocketType. Stream,Protocol Type.Tcp);
IPEndPoint ep=new IPEndPoint(IPAd dress.Parse(ser verIP),serverPo rt);
socket.Bind(ep) ;
socket.Listen(1 0);
socket.BeginAcc ept(new AsyncCallback(A cceptConnection ),socket);
}

private void AcceptConnectio n(IAsyncResult ar)
{
Socket socket=(Socket) ar.AsyncState;
clientSocket=so cket.EndAccept( ar);
//throw new Exception("TCP Accepted on
#"+clientSocket .RemoteEndPoint .ToString());
throw new Exception("Yuhu u");
}

Jan 27 '06 #1
11 3689
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.Connecte d 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.goo glegroups.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(strin g serverIP,int serverPort)
{
clientSocket=nu ll;
socket=new
Socket(AddressF amily.InterNetw ork,SocketType. Stream,Protocol Type.Tcp);
IPEndPoint ep=new IPEndPoint(IPAd dress.Parse(ser verIP),serverPo rt);
socket.Bind(ep) ;
socket.Listen(1 0);
socket.BeginAcc ept(new AsyncCallback(A cceptConnection ),socket);
}

private void AcceptConnectio n(IAsyncResult ar)
{
Socket socket=(Socket) ar.AsyncState;
clientSocket=so cket.EndAccept( ar);
//throw new Exception("TCP Accepted on
#"+clientSocket .RemoteEndPoint .ToString());
throw new Exception("Yuhu u");
}

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.co m>
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.WriteLi ne or System.Diagnost ics.Debug.Write Line 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(strin g serverIP,int serverPort)
{
clientSocket=nu ll;
socket=new
Socket(AddressF amily.InterNetw ork,SocketType. Stream,Protocol Type.Tcp);
IPEndPoint ep=new IPEndPoint(IPAd dress.Parse(ser verIP),serverPo rt);
socket.Bind(ep) ;
socket.Listen(1 0);
socket.BeginAcc ept(new AsyncCallback(A cceptConnection ),socket);
}

private void AcceptConnectio n(IAsyncResult ar)
{
Socket socket=(Socket) ar.AsyncState;
clientSocket=so cket.EndAccept( ar);
//throw new Exception("TCP Accepted on
#"+clientSocket .RemoteEndPoint .ToString());
throw new Exception("Yuhu u");
}

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.co m>
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.WriteLi ne, 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(AddressF amily.InterNetw ork,SocketType. Stream,Protocol Type.Tcp);
IPEndPoint ep=new IPEndPoint(IPAd dress.Parse(ser verIP),serverPo rt);
socket.Bind(ep) ;
socket.Listen(1 0);
socket.BeginAcc ept(new AsyncCallback(A cceptConnection ),socket);
....
private void AcceptConnectio n(IAsyncResult ar)
{
throw new Exception("Yuhu u");
}

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.co m>
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.co m>
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.goo glegroups.com.. .
I understand that exceptions are very expensive but how can I, with
Console.WriteLi ne, 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.co m>
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

149
25139
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? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
0
341
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 I beleive that it is the asynchronous BeginAccept call which causes the
1
7054
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 server which accepts new connections using the Socket.BeginAccept() function. This is done in its own thread using the following code.
1
6280
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 Framework Developer's Guide" (in the MSDN docs) "Using an Asynchronous Server Socket" sample. As usual with docs like this, the example isn't very good. I had to
4
1838
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 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);
5
12790
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 more incoming connections and does the BeginAccpet() again. It does an infinite loop this way. My question is: What is the best way to stop this? I thought about putting a boolean in there, but then what if it's still waiting for an incoming...
10
4462
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 I stop it (with no clients connected). Once I press the stop button an ObjectDisposedException is fired. What is the reason for this? Is it because the thread is still running even after I closed the server socket? Here is the code:
39
5853
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 ISO-8859-1 -t UTF-8 mydb.sql mydb_utf8.sql mysqlCREATE DATABASE mydb_utf8 CHARACTER SET utf8 COLLATE utf8_general_ci;
0
8603
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8537
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9081
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8955
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8800
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5818
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4323
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2236
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1957
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.