Remote IP addresses

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Remote IP addresses

Post by WallShadow on Wed Apr 11, 2012 11:34 am
([msg=65614]see Remote IP addresses[/msg])

Hello,

I recently learned basic Java socket programming and successfully tested it on my local network with 2 computers (thanks to whomever posted the good HTTP tutorial). I was able to use the ipconfig command to get my computer's local IP address and used that to connect the sockets on the 2 computers. But I wanted to ask, how can I connect to a remote network, for example, my friend's network?

thanks for reading.
User avatar
WallShadow
Contributor
Contributor
 
Posts: 535
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: Remote IP addresses

Post by centip3de on Wed Apr 11, 2012 3:31 pm
([msg=65619]see Re: Remote IP addresses[/msg])

WallShadow wrote:Hello,

I recently learned basic Java socket programming and successfully tested it on my local network with 2 computers (thanks to whomever posted the good HTTP tutorial). I was able to use the ipconfig command to get my computer's local IP address and used that to connect the sockets on the 2 computers. But I wanted to ask, how can I connect to a remote network, for example, my friend's network?

thanks for reading.


How can you connect to the network? Or connect to the computer?

--I will tell you now that I rarely ever, to never program with Internet related items. This is all just what I believe will work, and verification is needed.--

Computer:
Server -You would need a server program on that computer that would wait for all connections to a certain port, and accept them. You would then place whatever you want to happen once connection is made after that.
Client - You would also need a client program that attempts to use the provided I.P address (you'll need to get your friends, computers I.P address) and port number, to connect to that port.

Network:
Server - The only difference here is that you _could_ possibly use your friends computer to get access to his LAN, and use it that way. Though it would be tricky. Or, you could go sit outside his house with a laptop.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1217
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: Remote IP addresses

Post by WallShadow on Wed Apr 11, 2012 5:15 pm
([msg=65626]see Re: Remote IP addresses[/msg])

Thing is, I already know how to program a basic server and client in Java (with ServerSocket and Socket classes), but what I'm asking is how do I make a socket connection across 2 different networks? I know how to connect a socket within a local network:
Code: Select all
ConnectionSocket = new Socket("192.168.1.2", 17);


(192.168.1.2 is the IP of my main computer in my local network, and 17 is just because I like WOTD)

Question is, how do I instantiate a socket to connect to an IP on a different network, not just my local network?
User avatar
WallShadow
Contributor
Contributor
 
Posts: 535
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: Remote IP addresses

Post by centip3de on Wed Apr 11, 2012 5:32 pm
([msg=65630]see Re: Remote IP addresses[/msg])

WallShadow wrote:Thing is, I already know how to program a basic server and client in Java (with ServerSocket and Socket classes), but what I'm asking is how do I make a socket connection across 2 different networks? I know how to connect a socket within a local network:
Code: Select all
ConnectionSocket = new Socket("192.168.1.2", 17);


(192.168.1.2 is the IP of my main computer in my local network, and 17 is just because I like WOTD)

Question is, how do I instantiate a socket to connect to an IP on a different network, not just my local network?


Server would be something like:

Code: Select all
ConnectionSocket = new Socket(localhost, portNum);


and the client would be something like:

Code: Select all
ConnectionSocket = new Socket(IPofComputer, portNum);
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1217
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: Remote IP addresses

Post by ghost107 on Thu Apr 12, 2012 6:28 am
([msg=65633]see Re: Remote IP addresses[/msg])

WallShadow wrote:
Code: Select all
ConnectionSocket = new Socket("192.168.1.2", 17);

I suggest using ports over 1000 since most of the ports under this value are reserved ports.

WallShadow wrote:Question is, how do I instantiate a socket to connect to an IP on a different network, not just my local network?


If the computer you want to connect is on a different network, you could try to make a Peer to peer connection where the traffic goes from a computer in which has access to both networks(more of a centralized model).

Server would be(0.0.0.0 - default route ip, which this ip specifies all networks, that the computer is connected):
Code: Select all
ConnectionSocket = new Socket("0.0.0.0", 17);

Client would connect to this server and any packet would be routed to the other computer(2 clients and 1 server).
ghost107
Poster
Poster
 
Posts: 110
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)


Re: Remote IP addresses

Post by WallShadow on Thu Apr 12, 2012 8:25 am
([msg=65635]see Re: Remote IP addresses[/msg])

Hmm, so by using IP "0.0.0.0", your saying that the connection will try all IPs available to it?
User avatar
WallShadow
Contributor
Contributor
 
Posts: 535
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests