Java Web客户端远程连接

时间:2020-03-23 23:48:09

标签: java http server stream client

我正在编写Web服务器/客户端。通过本地主机通信时,一切都很好。但是,当使用我的公共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”的十六进制,但除此之外,我无法确定问题所在。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

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

import java.io.Serializable;

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

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

相关问题