java - Java Web 客户端远程连接
问题描述
我正在编写一个 Web 服务器/客户端。通过 localhost 进行通信时,一切正常。但是当使用我的公共 IP 地址进行通信时,会引发异常。这是一个最小的工作示例:
import java.io.*;
import java.text.*;
import java.util.*;
import java.net.*;
public class Server
{
public static void main(String[] args) throws IOException
{
int port = 80;
// server is listening on port
ServerSocket ss = new ServerSocket(port);
// running infinite loop for getting
// client request
while (true)
{
Socket s = null;
try
{
// socket object to receive incoming client requests
s = ss.accept();
System.out.println("A new client is connected : " + s);
// obtaining input and out streams
ObjectOutputStream odos = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream odis = new ObjectInputStream(s.getInputStream());
Info info = new Info();
info.color = 1;
odos.writeObject(info);
while(true){
info = (Info)odis.readObject();
if(info.exit){
break;
}
}
// closing resources
odis.close();
odos.close();
}
catch (Exception e){
s.close();
e.printStackTrace();
}
}
}
}
和客户:
import java.util.*;
import java.net.*;
import java.io.*;
import java.net.InetAddress;
public class Client
{
public static void main(String[] args) {
if(args.length>0){
ip_name = args[0];
}
if(args.length>1){
port = Integer.parseInt(args[1]);
}
network();
}
private static String ip_name = "localhost";
private static int port = 80;
private static void network(){
try
{
System.out.println("Connecting to network");
InetAddress ip = InetAddress.getByName(ip_name);
// establish the connection with server port
Socket s = new Socket(ip, port);
System.out.println("Connected");
// obtaining input and out streams
ObjectOutputStream odos = new ObjectOutputStream(s.getOutputStream());
InputStream i = s.getInputStream();
ObjectInputStream odis = new ObjectInputStream(i);
// get what color we are
int color = ((Info)odis.readObject()).color;
System.out.println(color);
//say we are done
Info info = new Info();
info.exit = true;
odos.writeObject(info);
System.out.println("Shutting down");
}catch(Exception e){
e.printStackTrace();
}
}
}
使用 localhost 时,客户端会按预期打印:
Connecting to network
Connected
1
Shutting down
但是当我用我的公共 IP 替换 localhost 时:
Connecting to network
Connected
java.io.StreamCorruptedException: invalid stream header: 48545450
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at Client.network(Client.java:36)
at Client.main(Client.java:14)
48545450 是“HTTP”的十六进制,但除此之外我不知道问题出在哪里。有任何想法吗?
解决方案
当我尝试运行您的代码时,出现错误“信息不可序列化”。我已按如下方式修改了您的 Info 类。
import java.io.Serializable;
public class Info implements Serializable {
public int color;
public boolean exit;
}
Serializable
如果要发送类数据,则需要实现。使用它,您可以通过网络保存对象信息。
推荐阅读
- python - 使用请求从网页获取端口时遇到问题
- php - DOMElement 绝对存在,但是 PHP 给出了致命错误,说它不存在,就在说它存在之后
- dll - REGSVR32 查找 dll 在注册表中的位置
- swiftui - 使用文本和滑块 swiftUI 创建 HStack 的垂直堆栈
- python - 仅对满足 Python 中特定条件的像素执行 2D 卷积
- c# - 如果输入参数不为空,MySql 更新
- javascript - 如何以角度向API服务异步添加数据(需要迭代两次)?
- android - Android 向上滑动以打开画廊,就像在 whatsapp 相机中一样
- python - Seaborn 中的自定义图表格式
- python - 拆分成str后加入列表元素