首页 > 解决方案 > 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”的十六进制,但除此之外我不知道问题出在哪里。有任何想法吗?

标签: javahttpserverstreamclient

解决方案


当我尝试运行您的代码时,出现错误“信息不可序列化”。我已按如下方式修改了您的 Info 类。

import java.io.Serializable;

public class Info implements Serializable {
    public  int color;
    public boolean exit;
}

Serializable如果要发送类数据,则需要实现。使用它,您可以通过网络保存对象信息。


推荐阅读