无法连接到服务器 [英] Couldn't connect to Server

查看:89
本文介绍了无法连接到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hai all,

我正在用Java编写服务器套接字编程来监听端口并获取有效数据并将其保存在数据库中。它在具有动态IP的LAN连接系统中工作。虽然我在公共IP系统中运行此程序,但我尝试通过HyperTerminal TCP客户端从动态IP系统连接,它无法连接到公共IP。关于这个plzzz的任何一个帮助...这是我的代码..........



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");
		 }
      }
}

推荐答案

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(从:::收到的连接+ connectionSocket.getInetAddress()。getHostName ());
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient。 readLine();
System.out.println(已收到:+ c lientSentence);
capitalizedSentence = clientSentence.toUpperCase()+''\ r''+''\ n'';
outToClient.writeBytes(From Server ::+ capitalizedSentence);
Str2 = clientSentence.substring(0,2);
System.out.println(前两个字符串::+ Str2 +我们定义的字符串::+ Str1);
if(Str1.equals(Str2))
{
System.out.println(数据有效);
outToClient.writeBytes(From Server :: Valid Data);
Class.forName(driver);
con = DriverManager.getConnection(url + db,root,root);
语句st = con.createStatement();
String sql =INSERT INTO DataReceived(ReceivedData)+
VALUES(''+ clientSentence +'');
try {

int val = st.executeUpdate(sql);
System.out.println(1行受影响);
}
catch(SQLException s){
System.out.println(SQL语句未执行!);
}
}
else
{
System.out.println(传入数据无效);
outToClient.writeBytes(From Server :: InValid Data);
}
}
}
catch(exception ex)
{
System.out.print(哎呀!它没用! \
);
}
}
}
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"); } } }


我认为你不能或不想将你的代码连接到公众根据您的定义,IP至少仅使用这些代码。要将您的代码与公共IP连接起来,您至少需要在您的计算机上使用Web主机/服务器或服务器 - 而不仅仅是代码,这将为您提供永久IP地址。从那时起,您必须将其配置为将请求转发到特定端口。



为了便于理解,您可以访问本地网络中的代码的原因是所有其他计算机都通过我们的路由器了解您的计算机。路由器将在连接到网络时为您的计算机分配IP地址。但是你无法连接到公共IP,ISP-at&t等的原因是他们实际上并不直接了解你的计算机。所有它知道的是子服务器和子服务器分层次地知道子服务器,直到您的家庭网络。因此,ISP的子子服务器 - 子服务器一定会知道您的家庭网络。然后您的路由器是根据请求将数据分发到不同计算机的路由器。考虑到这一点,要使此代码与公共网络连接,您需要获取Webhosting / Server,以便互联网知道如何连接到您的特定地址/计算机。





*注意,上面的解释是一个简短的概念。如果此代码适用于您的课程,您将很快了解所有网络如何相互通信。
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.


这篇关于无法连接到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆