Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
Comments
Richard MacCutchan 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 1-Sep-12 3:28am    
I don't know about firewall settings.
Public IP : IP address which has been assigned by ISP

1 solution

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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900