Is it possible to access a TCP server running outside our LAN through internet? I thought we could use the router IP Addess(which I got from whatismyip.com) in the class Socket Constructor

Socket(InetAddress address, int port)

. But client wasn't able to find the server. Has anybody successfully achieved this before?

port forwarding .

I was thinking the same. Tried to did that also but it didnt work. Here is what I have done till now
Server Code

//The server code Server.java:



/**
 * This is to help people to write Client server application
 *  I tried to make it as simple as possible... the client connect to the server
 *  the client send a String to the server the server returns it in UPPERCASE thats all
 */
import java.io.*;
import java.net.*;
public class Server {

	// the socket used by the server
	private ServerSocket serverSocket;
	// server constructor
	Server(int port) {

		/* create socket server and wait for connection requests */
		try
		{
			serverSocket = new ServerSocket(port);
			System.out.println("Server waiting for client on port " + serverSocket.getLocalPort());

			while(true)
			{
				Socket socket = serverSocket.accept();  // accept connection
				System.out.println("New client asked for a connection");
				TcpThread t = new TcpThread(socket);    // make a thread of it
				System.out.println("Starting a thread for a new Client");
				t.start();
			}
		}
		catch (IOException e) {
			System.out.println("Exception on new ServerSocket: " + e);
		}
	}

//	you must "run" server to have the server run as a console application
	public static void main(String[] arg) {
		// start server on port 1500
		new Server(1500);
	}

	/** One instance of this thread will run for each client */
	class TcpThread extends Thread {
		// the socket where to listen/talk
		Socket socket;
		ObjectInputStream Sinput;
		ObjectOutputStream Soutput;

		TcpThread(Socket socket) {
			this.socket = socket;
		}
		public void run() {
			/* Creating both Data Stream */
			System.out.println("Thread trying to create Object Input/Output Streams");
			try
			{
				// create output first
				Soutput = new ObjectOutputStream(socket.getOutputStream());
				Soutput.flush();
				Sinput  = new ObjectInputStream(socket.getInputStream());
			}
			catch (IOException e) {
				System.out.println("Exception creating new Input/output Streams: " + e);
				return;
			}
			System.out.println("Thread waiting for a String from the Client");
			// read a String (which is an object)
			try {
				String str = Sinput.readObject().toString();
				str = str.toUpperCase();
				Soutput.writeObject(str);
				Soutput.flush();
			}
			catch (IOException e) {
				System.out.println("Exception reading/writing  Streams: " + e);
				return;
			}
			// will surely not happen with a String
			catch (ClassNotFoundException o) {
			}
			finally {
				try {
					Soutput.close();
					Sinput.close();
				}
				catch (Exception e) {
				}
			}
		}
	}
}

Client Code

//The client code Client.java:


import java.net.*;
import java.io.*;

public class Client {

	ObjectInputStream Sinput;		// to read the socker
	ObjectOutputStream Soutput;	// towrite on the socket
	Socket socket;

	// Constructor connection receiving a socket number
	Client(int port) {
		// we use "localhost" as host name, the server is on the same machine
		// but you can put the "real" server name or IP address
		try {
			socket = new Socket("122.170.27.18", port);
		}
		catch(Exception e) {
			System.out.println("Error connectiong to server:" + e);
			return;
		}
		System.out.println("Connection accepted " +
				socket.getInetAddress() + ":" +
				socket.getPort());

		/* Creating both Data Stream */
		try
		{
			Sinput  = new ObjectInputStream(socket.getInputStream());
			Soutput = new ObjectOutputStream(socket.getOutputStream());
		}
		catch (IOException e) {
			System.out.println("Exception creating new Input/output Streams: " + e);
			return;
		}
		// now that I have my connection
		String test = "aBcDeFgHiJkLmNoPqRsTuVwXyZ";
		// send the string to the server
		System.out.println("Client sending \"" + test + "\" to server");
		try {
			Soutput.writeObject(test);
			Soutput.flush();
		}
		catch(IOException e) {
			System.out.println("Error writting to the socket: " + e);
			return;
		}
		// read back the answer from the server
		String response;
		try {
			response = (String) Sinput.readObject();
			System.out.println("Read back from server: " + response);
		}
		catch(Exception e) {
			System.out.println("Problem reading back from server: " + e);
		}

		try{
			Sinput.close();
			Soutput.close();
		}
		catch(Exception e) {}
	}

	public static void main(String[] arg) {
		new Client(1500);
	}
}

Attached is the image of my port forwaded router. What do I need to do more??

Sorry, got it sorted out. Had firewall problem at the Client side.

Yes, firewall never allow socket unless we configure firewall to allow tcp socket at particular port.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.