Reputation: 3982
Well, i am at new at Socket programming in java. I tried to implement a simple socket program to send the message with 127.0.0.1:4242 as localhost. But i want to send message to specific IP. How can i achieve that? Will it be possible to send message to my own IP as client-server running simultaneously?
Upvotes: 2
Views: 1488
Reputation: 84189
Every IP datagram has a source address and a destination address in the IP header, plus a transport protocol number, which is for majority of Internet traffic is either TCP or UDP. Then the header for that transport protocol lists source and destination port numbers.
So here you have it - sending, or better said "client", application gets assigned source address and port, usually automatically - address determined by the local routing table, port number assigned out of range of ephemeral ports, while "server" application listens on a well known port bound to an address at a particular machine. This tuple (source IP, source port, destination IP, destination port)
is enough for the datagram to get from here to there.
127.0.0.1
, and actually all the addresses in the range 127/8
, are reserved for the loopback, a virtual local interface, i.e. it's the way to say "no matter what my real address is, or even if I don't have one, connect to this machine I'm at right now".
Read up on the TCP/IP suite of protocols - it's a fairly simple concept (with ton of interesting details, of cource).
Upvotes: 0
Reputation: 14376
An endpoint in socket communications is an endpoint. Anything you can do using 127.0.0.1 can be done using that machines ip address. See here for more details.
Upvotes: 1