473,779 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BeginAccept Question

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 a private class it doesn't work.
Anyone knows why?

Below is the code:
---------------------------------------
using System;
using System.Net;
using System.Net.Sock ets;

namespace RTSPServer
{
public class TcpServer:IDisp osable
{
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.Any,555);//IPAddress.Parse (serverIP),serv erPort);
socket.Bind(ep) ;
socket.Listen(1 0);
socket.BeginAcc ept(new AsyncCallback(A cceptConnection ),socket);
}
private void AcceptConnectio n(IAsyncResult ar)
{
socket=(Socket) ar.AsyncState;
clientSocket=so cket.EndAccept( ar);
throw new Exception("TCP Accepted on
#"+clientSocket .RemoteEndPoint .ToString());
}
public void Dispose()
{
if(clientSocket !=null)
{
clientSocket.Cl ose();
}
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.

Jan 28 '06 #1
4 1844
Here is the two sample codes:
http://pwp.netcabo.pt/0141404701/RTSPServer.cs
http://pwp.netcabo.pt/0141404701/TcpServer.cs

It's a very short program. I tried the TcpServer class on a Console
project and it works ok doing in the main thread:
TcpServer ts=new TcpServer(IPStr ing,555);
Console.Read()

But from a System.Windows. Forms.Form object, for example on the
Form_Load function it won't work.

It's a strange problem!

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

Jan 28 '06 #3

"Nuno Magalhaes" <nu************ @hotmail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
| 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_Cli ck, 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.
Jan 28 '06 #4
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.

Willy Denoyette [MVP] wrote:
"Nuno Magalhaes" <nu************ @hotmail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
| 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_Cli ck, 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.


Jan 28 '06 #5

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

Similar topics

3
5043
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
2665
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask it differently. FOUR QUESTIONS: The background: I got three (3) files
3
3091
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table before rowID answID qryrow questionID datafield 1591 12 06e 06e 06e question 1593 12 06f 06f 06f question 1594 12 answer to the question 06f
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
7071
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
6292
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
11
3706
by: Nuno Magalhaes | last post by:
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) {
5
12804
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
4470
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:
0
9636
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
9474
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
10306
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
10138
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...
1
10074
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9930
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...
1
7485
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6724
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();...
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.