Hipolito Lopez

Hi to all,

I need to make a connection using socket. For this I used tcpClient and connect to the server and port that the client told me, but for security reason, the client wants that I use a specific local port for this connection.

In later C++ (C++ Builder) version I did that using ClientSocket and an Event called ClientLookup), that fired when the cliet try to connect to server, then I can specify the local port that I want to use, using sockaddr_in struct and the bind function in the socket.

In C#, are there any way that I can do this

Thanks in advance.



Re: .NET Framework Networking and Communication Client Socket Local Port

Alan J. McFarlane

Have a read through MSDN for the TcpClient class, see http://msdn2.microsoft.com/en-us/library/system.net.sockets.tcpclient.tcpclient.aspx note the third item says "and binds it to the specified local endpoint". So instead of doing:

TcpClient cli = new TcpClient();

instead do

int localPort = 9999;
TcpClient cli = new TcpClient(new IPEndPoint(IPAddress.Any, localPort));


Replacing 9999 with the port number you want to use.





Re: .NET Framework Networking and Communication Client Socket Local Port

Hipolito Lopez

Thanks Alan for your response.

I understand all that you told me. But that is not that I want.

When I run my Server Application, it's listening on specific port (for this test port 6669), if I do a 'netstat -a' command to see al listening port (win nt or later, in my case win xp pro), I saw this port 6669 listening:

TCP hlopezdev:6669 hlopezdev:0 LISTENING

When I run my client application, and execute 'netstat -b' command, to see all active connection (sokect level), I got this:

TCP hlopezdev:1463 hlopezdev:6669 ESTABLISHED 3712
[myclientappsocket.exe]

This say, that I'm connected using remote port 6669, but the local port that I'm using is the 1463. This is the port number That I need to control.





Re: .NET Framework Networking and Communication Client Socket Local Port

Alan J. McFarlane

I guess you didn't try it. And also that you didn't read the MSDN page either. Let me double check, you want your client socket to bind to a local port that you specify Quoting from your original post "I use tcpClient" and "[I want that] I can specify the local port that I want to use, using sockaddr_in struct and the bind function in the socket."

From the MSDN page: "Initializes a new instance of the TcpClient class and binds it to the specified local endpoint."

Do try it and check the netstat output.





Re: .NET Framework Networking and Communication Client Socket Local Port

Hipolito Lopez

Thanks for all,

I can find what I want after a lot of reading (help book, forums, etc).

I can do what I want using the Bind method.

The Bind Method (for socket) accept a IPEndPoint (as LocalEndPoint) where I can specify the local port to use.

Thanks for all





Re: .NET Framework Networking and Communication Client Socket Local Port

Alan J. McFarlane

Why post a question if you're not going to read any answers... :-'(

Look at my answers, they both mentioned "bind", even putting it twice in bold in the second... TcpClient wraps socket, guess what the TcpClient constructor taking IPEndPoint calls...Socket.Bind...






Re: .NET Framework Networking and Communication Client Socket Local Port

Hipolito Lopez

:-s

Sorry, Alan, was my mistake.

I appreciate your help, and sorry ... I was a 'little' confused