N
Nuno Magalhaes
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);
....
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.Stream,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.
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);
....
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.Stream,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.