|
-
Jun 7th, 2006, 04:01 PM
#1
Thread Starter
Hyperactive Member
[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
-
Jun 7th, 2006, 07:22 PM
#2
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.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Jun 9th, 2006, 07:31 AM
#3
Thread Starter
Hyperactive Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|