[1.0/1.1] Setting the Local Port number when sending Tcp messages
Hi. I'm trying to connect to another server application. I instantiated the object as:
IPHostEntry RemoteipHostInfo2 = Dns.Resolve
(CM.MAIN.CONFIG.GetConfig(192.563.1.2));
IPAddress RemoteipAddress2 = RemoteipHostInfo2.AddressList[0];
IPEndPoint RemoteEndPoint2 = new IPEndPoint(RemoteipAddress2,
Int32.Parse("45236"));
Socket handler = new Socket(
AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
/* Thsi instantiates a socket to make a connection to the server inputting the server's ip and port information. */
/* However, when i print out the IP and port of both the local and remote machine as: */
(((IPEndPoint)handler.LocalEndPoint).Address.ToString()) -> prints correct
((IPEndPoint)handler.LocalEndPoint).Port.ToString() -> prints wrong.
IPAddress.Parse(((IPEndPoint)handler.RemoteEndPoint).Address.ToString()) -> prints correct.
((IPEndPoint)handler.RemoteEndPoint).Port.ToString() -> prints correct.
In other words, i'm connecting to a server specifying the server;'s ip and port. But when I read the port number that I am sending on, I'm getting a different port number every time.
Is this the default way this works or am I diong something wrong?
Jennifer
Re: [1.0/1.1] Setting the Local Port number when sending Tcp messages
I believe that by default you will get a random, unused port. If you want to specify the port, you may need to Bind the socket to the appropriate port.
Re: [1.0/1.1] Setting the Local Port number when sending Tcp messages
Is it possible to be lisening on a port say 5000, and when I want to send a message, use that same port to send the message? e.g.
socket.bind(5000) ....
// want to send message.
// remote infor
IPHostEntry RemoteipHostInfo1 = Dns.Resolve
("192.563.1.2");
IPAddress RemoteipAddress1 = RemoteipHostInfo1.AddressList[0];
IPEndPoint RemoteEndPoint1 = new IPEndPoint(RemoteipAddress1,
Int32.Parse("45236"));
// local enfor
IPHostEntry RemoteipHostInfo2 = Dns.Resolve
("localhost");
IPAddress RemoteipAddress2 = RemoteipHostInfo2.AddressList[0];
IPEndPoint RemoteEndPoint2 = new IPEndPoint(RemoteipAddress2,
Int32.Parse("5000"));
socket.bind(RemoteEndPoint2 )
socket.connect(RemoteEndPoint1)
In other words, I want to set the local end point port and then connect to the server. And at the same time, I'm also listening on this port. Is that possible or is it that I'm trying to use the same port twice?
Jennifer