tags:

views:

46

answers:

2

hi,

I am doing projects in sockets.usually the server listens in a particular port,and the client has to connect to the port then the send and receives will happen.but we don't specify any port number in the client side,but i am in a situation to use a port in a client side, through this port only the messages will delivered to the server.how to do this?

In my client side they are restricting the ports ,so if want use to a valid free port i have to set it in the client program,instead of OS Choosing it.This is my problem.

A: 

Bind the client socket to local address (ip and port number) before connecting to server. Be ready to handle errors e.g. when the port is not available (choose next port, retry).

ygrek
A: 

I guess you're using the System.Net.Sockets namespace?

If so, classes like NetworkStream take the Socket as a constructor parameter:

http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.aspx

Similarly, the TcpClient takes Port and Server as constructor arguments, Port is the same as socket in this context:

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx

Finally. you can control the number of this socket in a few ways:

  • Command Line Parameter
  • Setting in an Application.Config file
  • Read it from the Registry

There are a few methods for this type of thing.

Russ C