473,372 Members | 877 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,372 software developers and data experts.

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

Below is the code:
---------------------------------------
using System;
using System.Net;
using System.Net.Sockets;

namespace RTSPServer
{
public class TcpServer:IDisposable
{
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.Any,555);//IPAddress.Parse(serverIP),serverPort);
socket.Bind(ep);
socket.Listen(10);
socket.BeginAccept(new AsyncCallback(AcceptConnection),socket);
}
private void AcceptConnection(IAsyncResult ar)
{
socket=(Socket)ar.AsyncState;
clientSocket=socket.EndAccept(ar);
throw new Exception("TCP Accepted on
#"+clientSocket.RemoteEndPoint.ToString());
}
public void Dispose()
{
if(clientSocket!=null)
{
clientSocket.Close();
}
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 1822
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(IPString,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.googlegr oups.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_Click, 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.googlegr oups.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_Click, 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
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
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...
3
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...
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...
11
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...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.