Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java套接字编程实现两个不同网络之间的通信_Java - Fatal编程技术网

使用java套接字编程实现两个不同网络之间的通信

使用java套接字编程实现两个不同网络之间的通信,java,Java,我在我的笔记本电脑上写了一个服务器端套接字程序[java程序],在我朋友的笔记本电脑上写了一个客户端套接字程序。两者都连接到同一个wifi,工作正常,但当我将服务器插座笔记本电脑连接到我的移动数据网络时,情况并非如此 我尝试过使用IP地址,当我们连接到网络时,它会给我一个客户端错误 服务器端代码 import java.net.*; import java.io.*; class Server Demo { public static void main(String args[])throw

我在我的笔记本电脑上写了一个服务器端套接字程序[java程序],在我朋友的笔记本电脑上写了一个客户端套接字程序。两者都连接到同一个wifi,工作正常,但当我将服务器插座笔记本电脑连接到我的移动数据网络时,情况并非如此

我尝试过使用IP地址,当我们连接到网络时,它会给我一个客户端错误

服务器端代码

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

class Server Demo
{
public static void main(String args[])throws IOException
{
try 
{
int n=0;
ServerSocket ss=new ServerSocket(9999);
while(n!=1)
{
System.out.println("Waiting for Client!");
Socket sc=ss.accept();
BufferedReader in=new BufferedReader(new 
InputStreamReader(sc.getInputStream()));
PrintStream out=new PrintStream(sc.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s1=in.readLine();
System.out.println("From Client="+s1);
System.out.println("Enter the Message:");
String s2=br.readLine();
out.println(s2);
n=Integer.parseInt(br.readLine());
}
}
catch(Exception e1)
{
System.out.println(e1.getMessage());
}
}
}
客户端代码

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

public class CDemo
{
public static void main(String args[])throws IOException
 {
try
{
Socket cc=new Socket("192.168.0.103",9999);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintStream out=new PrintStream(cc.getOutputStream(),true);
BufferedReaderin=newBufferedReader(new 
InputStreamReader(cc.getInputStream());
System.out.println("Enter the Message:");
String s=br.readLine();
out.println(s);
String s1=in.readLine();
System.out.println(s1);
out.println(1);
cc.close();
}
catch(Exception e1)
{
System.out.println(e1.getMessage());
}
}
}

如果我明白了,你的问题是:你试图在一个网络中连接服务器端,在另一个网络中连接客户端。但这不起作用,因为每一方都看不到另一方,这是因为程序不在相同的IP地址范围内;他们需要留在同一个网络中,以便路由器提供彼此可见的IP地址。

有什么错误?您是否进行了端口转发,以便路由器知道将流量移动到何处?请修复您的格式,同时,不要吸收重要的异常信息。。。至少做
e.printStackTrace()
,它包含的信息比错误消息本身多得多。我的问题是,如果我想访问不同的网络范围ip地址,那么我应该怎么做…请提供帮助如果你想让计算机从外部访问,你需要使用internet连接,就像将你的程序部署到heroku机器上,或者只是配置一个网关,网关是计算机网络中的一个节点,它将流量从本地网络传递到其他网络或Internet。我对网络或配置网络了解不多,也帮不上忙。