Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic IP address and socket programming.

 
Sandeep Kumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

I am using a ADSL modem to get connected to the net and each time I log in it gives me a dynamic IP address

My question is.

Can i use socket programming to make my computre as the host/server and communicate with this system from a remote area?
I tried using a basic socket clint server program to connect with the local ip address 127.0.0.1 .(both the programming running in the same machine)
But when I tried using to assign the Dynamic IP address generated in the same client programming , it was not able to connect. (Again both the programming running on the same machine) What could be the reason? Do I need to run the clinet and the server programs on diff. machines?

regards.
 
Joe Ess
Bartender
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch.

Can i use socket programming to make my computre as the host/server and communicate with this system from a remote area?


Certainly, however your modem is probably configured to block incoming connections. Go to PortForward.com for router-specific instructions on how to configure your router. You may also want to make sure your ISP does not block incoming connections too, as ISP's usually forbid server functions on residential DSL lines.
 
Sandeep Kumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you joe. Lot of information on that link. I am using huawei MT882 modem and it has inbuild setting for Virtual Server which itself is a IP forwarding.While setting the Virtual Server it asks for the following information.

1. Private IP : Your local IP address where your FTP server is inastalled
2. Protocol Type : TCP
3. Private Port : default 21 (you can change it to anything which ever you have assigned to your ftp server software.
4. Public Port : Default 21 ( change that if required - external persons will have to conenct to the public Port



Now which IP address do i put here ( This is just for socket communication . I know nothing about FTP). The IP address that I am using is to invoke my ADSL router or the external IPAddress ?

Default port they say is 21 . But I guess I should be using 1024 onewards. as 0-1024 are reserved .


Thanks in advance.
 
Joe Ess
Bartender
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sand K:

Now which IP address do i put here


The IP of your computer. If you didn't specify one your router probably has DHCP configured and you'll have to figure out what IP you were assigned.
21 is the default port for FTP. You can use it or something else on your external interface, then redirect it to something else on your computer. For example, I have my DSL router configured to redirect port 80, standard HTTP to port 8080 on my computer so I can run tomcat as a non-privlidged user.
 
Sandeep Kumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you joe,

Thanks a million. The socket programming is working on my computer when I execute the cleint and server programme seperately on 2 diff. dos windows.

Now i will try to execute the client programme on a seperate computer in the Internet. Should this computer too be the subscriber of the ADSL lines as I do? What kind of setting do I need to make on the client machine?


Thanks in advance

Sandeep
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sand,

Welcome to JavaRanch!

We have a strict policy on display names, which must be a real first and last name with a space between. A single name and a last initial ain't good enough.

Please go here and fix your display name up, pronto. Thanks, pardner!
 
Joe Ess
Bartender
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sand K:

Now i will try to execute the client programme on a seperate computer in the Internet. Should this computer too be the subscriber of the ADSL lines as I do? What kind of setting do I need to make on the client machine?


The client should be able to access the external IP address just like any other server on the internet.
 
Sandeep Kumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ernest,

Thanks for correcting.

regards.
 
Sandeep Kumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Joe,

I am not in station. Will test and tell you the results by weekend.

thank you
 
Sandeep Kumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Joe,

When I type the external IP address , it works only on my browser. Other computers on the internet are not able to access it. Also my client Socket is not able to access it when I set the IP address to the external IP.


I am pasting my socket programs.



Client Side.

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

public class trialClient
{


public static final int SERVER_PORT=80;



public static void main(String[] args) throws IOException
{

//check for validity of args[0] here.



InetAddress addr = InetAddress.getByName("59.92.243.151"); //--------comment1

System.out.println("addr = " + addr);
Socket socket = new Socket(addr,SERVER_PORT);


// Guard everything in a try-finally to make
// sure that the socket is closed:
try
{
System.out.println("socket = " + socket);
BufferedReader in = new BufferedReader( new InputStreamReader(
socket.getInputStream()));
// Output is automatically flushed
// by PrintWriter:

System.out.println("coming....1");

PrintWriter out = new PrintWriter( new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())),true);


System.out.println("coming....2");


for(int i = 0; i < 10; i ++)
{
out.println("howdy " + i);
String str = in.readLine();
System.out.println("The msg frm server is :"+str);

}
out.println("END");

System.out.println("coming....3");


}
finally
{
System.out.println("closing...");
socket.close();
}
}
}







Server Side



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

public class trialServer
{

// Choose a port outside of the range 1-1024:

public static final int PORT = 80;

public static void main(String[] args) throws IOException
{
ServerSocket s = new ServerSocket(PORT);
System.out.println("Started: " + s);

try
{
// Blocks until a connection occurs:

Socket socket = s.accept();


try
{
System.out.println( "Connection accepted: "+ socket);
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));


// Output is automatically flushed by PrintWriter:

PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
while (true)
{
String str = in.readLine();
if (str.equals("END"))
break;
System.out.println("Echoing: " + str);
out.println(str);
}
// Always close the two sockets...
}
finally
{
System.out.println("closing...");
socket.close();
}
}
finally
{
s.close();
}
}
}





I am opening 2 seperate Dos Windos n run client n server seperately.


1)When I set the ip address at <Comment1> to 192.168.1.2 the program works fine


2. When I set the ip addres at <comment1> to the dynamic ip at that particular moment ,

The server side socket doesnt recive the message sent by client.Also the client program terminates , it recieves a 'bad request' from the server.

HTTP 1.1 /400 bad request


Could you tell me what actually is happening and where am I going wrong?

Thanks

Sandeep
 
Sandeep Kumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everyone,


I succeded in making my MT882 ADSL router as Virtual Server. The problem was I was trying my external IP connection from my own system , some routers invoke the default ATSL configuration Window when you try this way.
I forwarded my dynamic IP to my friend and asked him to check it and it worked. I had installed Personal WebServer and JRun Servers listening at different ports.Though I am yet to check if my socket program would work this way , I guess it should not be much of a problem.


Here is how to do it.

First of all do the portforwarding

For MT 882- do the following
In the Tcp/ip properties
Go to IP Address and hard code an IP Address which is from the gateway. For eg. If gateway is 192.168.1.1 , you can make the your IP Addrss as 192.168.1.2 or for that matter it could be 192.168.1.x where "x" is not equal to 1 and <=255 . Also if you are in a Local Area Network and enabling the DHCP Server enabled , make sure the IP you set for your machine doesnt conflict with the IP range that you specify to be alloted dynamically to other computers on the LAN.

Once you are done with this do the port forwarding.

In your browser type 192.168.1.1 (supposing thats your gateway)

ADSL Configuration window appears before you after giving the username and the password(If you have not modified the settings generally the username and password would be "admin" and "admin" respectively)

Click on "Advance" and select "virtual server"
In the status select "enabled" radiao button.

Give a name so that you can identify which application you are running at the forwarded port.
Private IP is the one you have hard coded like 192.168.1.x in the previous e.g. we have made it 192.168.1.2
Protocol type you can select TCP or UDP or Both. Click on Both

Private Port type 80 for http request. (I wonder if v can change it for other ports for http requests!!!)

You should know which application you want to run in your virtual server before setting the public port. Personal Webserver listens at port 80. Jrun server - Admin server listens at 8000 , Default server listens at 8100. In brief you need to know which port your application is meant to run.To use JRun Default server type 8100 as the Public Port.

In the Schedule click "always" or for a spcified time to which the port should be open.Bear in mind port forwarding opens a port of your computer to the external world. And you are not safe unless you insatl a Firewall.

Now click on apply-> and then Save and Reboot the Modem.

Now you have forwarded a port .




To check if the port you forwarded is successful,

go to http://www.myserver.org/portsniff.asp and verify if the port you forwarded is open. If its not some problem which what you did or your ISP is not supporting port forwarding.

Next. go to a site which tells your external WAN ip or the dynamic IP.
http://www.whatismyip.com/ http://www.portforward.com shows your ip address.

Ask a friend of yours to type your external ip address: public port in his broswer.

Example if your external ip is 51.23.234.133 and the port you forwarded was 8100 then your friend needs to type,

http://51.23.234.133:8100

He should be able to see the page sent by Jrun Default Server.

Note: your external IP keeps changing each time your router reboots you may need to visit the above said sites to get your latestexternal IP. you can see you external IP in the modem configuration windows summary page under WAN Channel->IP Address.

You can also check the opened port after port forwarding at -

https://www.grc.com/ -> selct shields up ->Again select Shields up and follow the instructions.


Lots of information in the following websites.

www.portward.com

http://www.dslwebserver.com/ --Author is a Doctor!!!

One word of caution- keep your port open only for the time you want it to be open unless you have a firewall.Use the "enable " disable" option of the Virtual Server for this purpose and reboot.

Hope this is will help some one who wants to try remote accessing his PC. Once again my sincere thanks to Joe for his initial push .

regards.
 
kartikperi kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello sandeep,

I Tried to do the same with MTNL broadband connection but was not successful.
I configured the virtual server at gateway of my ISP, still i am not able to communicate with server on my friends machine running the server program.



could you give me a solution.

P.Kartik Kumar
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your friend's firewall configured to allow connections to that server?
 
kartikperi kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Rob, My friend has configured the firewall so that anyone can reach that machine.
but his ISP is different from mine. Does it make a difference?
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It shouldn't. Can others reach that server, or are you not the only one with problems?
 
Hari haran Ravi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I am new to Coderanch. I also had the same problem. I have created a server program in Visual Basic and ran it on my friend's system. I tried to connect with it using a java program, which works similar to the above code posted by Sandeep. I don't know what to do.. Please help me out.

My friend removed firewall. I cross checked whether the ports are open. They are open. But they are not connecting.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic