471,315 Members | 1,467 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,315 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 1758
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Stevey | 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
11 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.