Search code examples
javaservertcpclientbyte

Java Implementing a Client Server Model (Using TCP) and sending a IP address from server and Printing it out on Client


Server code:

package exp1;
import java.net.*;
import java.io.*;
public class MyServerSocket{
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
          ServerSocket ss=new ServerSocket(2050); 
          System.out.println("server is waiting..."); 
          Socket s=ss.accept(); 
          
          InetAddress ad= InetAddress.getByName("hostname");
          OutputStream os=s.getOutputStream(); 
          System.out.println("s"+ ad.getHostAddress());
          byte ins=Byte.parseByte(ad.getHostAddress());
          os.write(ins); 
          
          ss.close();
    }
    

}

Client code:

package exp1;
import java.io.*;
import java.net.*;
public class MyClientSocket {
    public static void main(String[] args) throws Exception{
    Socket s=new Socket(InetAddress.getLocalHost(),2050); 
    InputStream is=s.getInputStream(); 
    System.out.println("Client is ready to receive data"); 
    int d=0; 
    while(d!='#') 
    { 
    d=is.read(); 
    System.out.print((char)d); 
    }        
    }
}

Error:

Server side:

Exception in thread "main" java.lang.NumberFormatException: For input string: "ip"

at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)

at java.base/java.lang.Integer.parseInt(Integer.java:660)

at java.base/java.lang.Byte.parseByte(Byte.java:193)

at java.base/java.lang.Byte.parseByte(Byte.java:219)

at exp1/exp1.MyServerSocket.main([MyServerSocket.java:14](https://MyServerSocket.java:1

I'm trying to display the localhost's ip on client but I get an error.


Solution

  • getHostAddress returns a string representation of an address. So, this method returns something like 192.168.1.100 which can't be parsed to a byte. You can pass your string as an array of bytes, but this is not optimal solution, since IPv4 address is just 4 bytes, but string 192.168.1.100 is 13 bytes long!

    Also, I don't understand purpose of line while (d != '#') since you never send any # symbols.

    Here is the code that works for me

    class MyServerSocket {
      public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(2050);
        System.out.println("server is waiting...");
        Socket s = ss.accept();
    
        InetAddress ad= InetAddress.getByName("hostname");
        try (OutputStream os = s.getOutputStream()) {
          System.out.println("s"+ ad.getHostAddress());
          os.write(ad.getAddress());
        }
        ss.close();
      }
    }
    
    class MyClientSocket {
      public static void main(String[] args) throws Exception{
        Socket s=new Socket(InetAddress.getLocalHost(),2050);
        try (InputStream is = s.getInputStream()) {
          System.out.println("Client is ready to receive data");
          byte[] address = is.readAllBytes();
          InetAddress ad = InetAddress.getByAddress(address);
          System.out.println(ad.getHostAddress());
        }
      }
    }
    

    P.S. closing all resources you open is a good practice to avoid leaks. In the example, I've used try-with-resources construction.