0

I am writing a web server/client. When communicating over localhost, things are fine. But when communicating using my public IP address, an exception is thrown. Here is a minimal working example:

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

And the Client:

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

When using localhost, the client prints out, as expected:

Connecting to network
Connected
1
Shutting down

but when I replace localhost with my public IP:

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 is hex for "HTTP", but beyond that I can't tell what the problem is. Any ideas?

Naman
  • 27,789
  • 26
  • 218
  • 353
chessprogrammer
  • 768
  • 4
  • 15

1 Answers1

0

When I tried running your code I got error "Info is non-serilizable". I have modified your Info class as follows.

import java.io.Serializable;

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

You need to implement Serializable If you are sending class data. Using this you can persist object info over a network.

js_248
  • 2,032
  • 4
  • 27
  • 38
  • Thank you for your answer! I did not post the code for info above, but it does indeed implement serializable, so that is not the issue :( – chessprogrammer Mar 24 '20 at 00:29
  • I think connecting to public ip is bit complicated https://www.whatismyip.com/questions/connecting-a-local-ip-through-a-public-ip/ – js_248 Mar 24 '20 at 00:48
  • @chessprogrammer Also, mine is working with private ip (ipconfig getifaddr en0) – js_248 Mar 24 '20 at 00:50