3 Replies - 3249 Views - Last Post: 01 April 2011 - 06:02 PM Rate Topic: -----

#1 Dankwansere   User is offline

  • D.I.C Regular

Reputation: 83
  • View blog
  • Posts: 257
  • Joined: 09-November 09

java program to communicate over the internet

Posted 30 March 2011 - 04:27 PM

Hey, I wrote 2 programs that i'm trying to make them communicate over different machines on the internet. I'm able to make them communicate between 2 machines on my network, but for some reason i'm not unable to make them communicate over the internet or different networks. can someone please help. This is my codes so far.

This is the code for the server program

 

import java.util.*;
import java.lang.*;;
import java.io.*;
import java.net.*;

public class Server {
  public static void main(String[] args) {
	  
	  Scanner input = new Scanner(System.in);
	
	  //Create a server socket
	  try
	  {
	  	  ServerSocket serverSocket = new ServerSocket(8000);
	  
		  //Listen for a connection request
		  Socket socket = serverSocket.accept();
		  
		  //Input stream
		  DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
		  
		  //Output Stream
		  DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
	
		 // Double radius = inputFromClient.readDouble();
		  boolean test = true;
		  String Smessage;
		  String Cmessage;
		  while(test)
		  {
		  	  System.out.print("Enter a message: ");
		  	  Smessage = input.nextLine();
		  	  if(Smessage.equals("-3"))  
		  	  {
		  	  	  	  System.exit(0);
		  	  	  }
		  	  else
		  	  {
		  	  	  
		  	  	  outputToClient.writeUTF(Smessage);
		  	  	  System.out.print("Incoming message.. ");
		  	  	  Cmessage = inputFromClient.readUTF();
		  	  	  System.out.println(Cmessage);
		  	  	  
		  	  	  test = true;
		  	  }
		  }
		  
			  outputToClient.writeUTF("We got your number and its less than 1");
		 
	  }
	  
	  catch(IOException ex)
	  {
	  	  System.err.println(ex);
	  }
	  	  

	  }
	  
} 




and this is the code for the client side..

import java.util.*;
import java.lang.*;;
import java.io.*;
import java.net.*;

public class Client {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		
		try
		{
		
			Socket socket = new Socket("192.168.1.103", 8000);
	
			//Write to server
			DataOutputStream toServer = new DataOutputStream(socket.getOutputStream());
			
			//Input stream
			DataInputStream fromServer = new DataInputStream(socket.getInputStream());
			boolean test = true;
			
			while(test)
			{
				
			
				System.out.print("Message from Eric: ");
				String messageRead = fromServer.readUTF();
				System.out.println(messageRead);
			
				System.out.print("Enter Your Message: ");
				String Cmessage = input.nextLine();
				if(Cmessage.equals("-3"))  
				{
		  	  	  	  System.exit(0);
		  	  	 }
		  	  	 else
		  	  	 {
		  	  	 	 toServer.writeUTF(Cmessage);
		  	  	 	 test = true;
		  	  	 }
		  	}
			
			
		}
		
		catch(IOException ex) {
			System.out.println(ex.toString());
		}
	}
}






Is This A Good Question/Topic? 1
  • +

Replies To: java program to communicate over the internet

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: java program to communicate over the internet

Posted 30 March 2011 - 04:50 PM

There are a few thousand of PCs with this address

new Socket("192.168.1.103", 8000);

over the world.

Most private networks in the world, with less than 255 hosts, use these 192.168.1.xxx range of addresses.

If you want to connect to a host which is not on your local lan, you will have to put the address of the router (the link between your lan and the world)
You will have to inform this router to route the calls for port 8000 to your PC which is 192.168.1.103 on your lan.
Was This Post Helpful? 1
  • +
  • -

#3 Dankwansere   User is offline

  • D.I.C Regular

Reputation: 83
  • View blog
  • Posts: 257
  • Joined: 09-November 09

Re: java program to communicate over the internet

Posted 01 April 2011 - 05:52 PM

View Postpbl, on 30 March 2011 - 07:50 PM, said:

There are a few thousand of PCs with this address

new Socket("192.168.1.103", 8000);

over the world.

Most private networks in the world, with less than 255 hosts, use these 192.168.1.xxx range of addresses.

If you want to connect to a host which is not on your local lan, you will have to put the address of the router (the link between your lan and the world)
You will have to inform this router to route the calls for port 8000 to your PC which is 192.168.1.103 on your lan.


Thnx, so i'm guessing i'd have to do some router configurations on my router side right? I have a Cisco Lynksis router and on the home administrator page its kind of confusing to look for where to route the calls.
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: java program to communicate over the internet

Posted 01 April 2011 - 06:02 PM

Depends of your ISP (Internet Server Provider)
if you are on Unix iproute will do the job
on Windows you will have to rely on the router protocol
On VMS I'll fix it on a glance :)

This post has been edited by pbl: 01 April 2011 - 06:06 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1