aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Dynamic IP address and socket programming. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Dynamic IP address and socket programming." Watch "Dynamic IP address and socket programming." New topic
Author

Dynamic IP address and socket programming.

Sandeep Kumar
Greenhorn

Joined: Sep 07, 2006
Posts: 7
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

Joined: Oct 29, 2001
Posts: 8713
    
    6

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Sandeep Kumar
Greenhorn

Joined: Sep 07, 2006
Posts: 7
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

Joined: Oct 29, 2001
Posts: 8713
    
    6

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

Joined: Sep 07, 2006
Posts: 7
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

Joined: Jul 08, 2003
Posts: 24168
    
  30

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!


[Jess in Action][AskingGoodQuestions]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8713
    
    6

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

Joined: Sep 07, 2006
Posts: 7
Hello Ernest,

Thanks for correcting.

regards.
Sandeep Kumar
Greenhorn

Joined: Sep 07, 2006
Posts: 7
Hello Joe,

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

thank you
Sandeep Kumar
Greenhorn

Joined: Sep 07, 2006
Posts: 7
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

Joined: Sep 07, 2006
Posts: 7
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

Joined: Mar 03, 2010
Posts: 2
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

Joined: Oct 27, 2005
Posts: 19556
    
  16

Is your friend's firewall configured to allow connections to that server?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
kartikperi kumar
Greenhorn

Joined: Mar 03, 2010
Posts: 2
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

Joined: Oct 27, 2005
Posts: 19556
    
  16

It shouldn't. Can others reach that server, or are you not the only one with problems?
Hari haran Ravi
Greenhorn

Joined: Aug 11, 2010
Posts: 26
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic IP address and socket programming.
 
Similar Threads
HELP : Connecting to machine in different network
Address already in use and Socket operation on nonsocket
Client Server Chat Network Issue
Location of stub and Remote Interface...Urgent pls
Socket connections and Browser