Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Server Sockets
Hai all,
I'm writing Server socket programming in Java to listen Port and take the valid data and save it in Data base. It was working in LAN connection systems with dynamic IP. While i'm running this program in Public IP system, I tried to connect from dynamic IP systems through HyperTerminal TCP client, it couldn't connect to Public IP. CAn any one help regarding this plzzz... Here is my code..........
 
// Program : GPRS Packet fetching and saved it into Database
// Author : Himachandra C

import java.io.*;
import java.net.*;
import java.sql.*;
 
class TCPServer
{
   public static void main(String argv[]) throws Exception
      {
         String clientSentence;
		 String Str1 = "$S";
		 String Str2;
         String capitalizedSentence;
         ServerSocket welcomeSocket = new ServerSocket(6789);
		 Connection con = null;
		 String url = "jdbc:mysql://localhost:3306/";
		 String db = "testserver";
		 String driver = "com.mysql.jdbc.Driver";
		 
		 try
		 {
		 
         while(true)
         {
            Socket connectionSocket = welcomeSocket.accept();
			
			System.out.println("Connection received from ::: " + connectionSocket.getInetAddress().getHostName());
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            clientSentence = inFromClient.readLine();
            System.out.println("Received: " + clientSentence);
            capitalizedSentence = clientSentence.toUpperCase() + '\r' + '\n';
            outToClient.writeBytes("From Server :: " + capitalizedSentence);
			Str2 = clientSentence.substring(0,2);
			System.out.println("First two chars of string::" + Str2 + "Our defined String::" + Str1);
			if (Str1.equals(Str2))
			{
				System.out.println("The data is valid");
				outToClient.writeBytes("From Server :: Valid Data" );
				Class.forName(driver);
				con = DriverManager.getConnection(url + db,"root","root");
				Statement st = con.createStatement();
				String sql = "INSERT INTO DataReceived(ReceivedData) " +
                   "VALUES ('"+clientSentence+"')";
				try{
					
					int val = st.executeUpdate(sql);
					System.out.println("1 row affected");
				}
				catch (SQLException s){
					System.out.println("SQL statement is not executed!");
				}
			}
				else
			{
				System.out.println("The incoming data is Invalid");
				outToClient.writeBytes("From Server :: InValid Data" );
			}
         }
		 }
		 catch (Exception ex)
		 {
			 System.out.print("Whoops! It didn't work!\n");
		 }
      }
}
 

 

 

 

Posted 31-Aug-12 21:02pm
Comments
Richard MacCutchan at 1-Sep-12 3:23am
   
What do you mean by "public IP"? Are you sure that the path between your client and server is valid and is not blocked by your firewall?
Himachandra at 1-Sep-12 3:28am
   
I don't know about firewall settings.
Public IP : IP address which has been assigned by ISP

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I don't think you can or want to connect your code to the Public IP, according to your definition, at least with these codes alone. To connect the your code with public IP, you will at least need a Web hosting/Server or a Server on your computer - not just the code, that would give you a permanent IP address. From that point on, you have to configure it to forward the request to the specific port.
 
To make it easy to understand, the reason that you can access to your code in you local network is that all other computers know your computer through our router. The router will assign IP address for your computer when it connect to the network. But the reason that you cannot connect to Public IP, the ISP- at&t and so on, is that they actually do not know your computer directly. All it knows are the sub-Server and sub-Server knows sub-sub-Server hierarchically until to you home network. So at one point the sub-sub- .... - sub -Server of the ISP will know your home network. Then your router is the one who distribute data to different computers upon the request. Having that in mind, to make this code connect with to public network, you will need to get Webhosting/Server so that the internet know how to connect to your specific address/computer.
 

* Note, the above explanation is a brief concept. If this code is for your class, you will learn soon how all the network communicate to one another.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Mathew Soji 309
1 BillWoodruff 300
2 Afzaal Ahmad Zeeshan 252
3 DamithSL 225
4 Maciej Los 215
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 22 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100