0

I initially tried to connect to port 9090 but it throws the exception. I then tried another port number 61231 and it threw the exception again. (this how i init the port)

ServerSocket listener = new ServerSocket(61231);

I also know what ports are available on the server. netstat on linux

When I do Java DataServer in linux terminal

Exception in thread "main" java.net.BindException: Address already in use (Bind failed)
        at java.net.PlainSocketImpl.socketBind(Native Method)
        at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387)
        at java.net.ServerSocket.bind(ServerSocket.java:375)
        at java.net.ServerSocket.<init>(ServerSocket.java:237)
        at java.net.ServerSocket.<init>(ServerSocket.java:128)
        at DataServer.main(DataServer.java:15)

I am able to get the server to reply back to me enter image description here

DataServer.java

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

/*A TCP server that runs on port 9090.
When a client connects, it sends the
client the current date and time, then
closes the connection with that client.*/

public class DataServer {
    /*** Runs the server.*/
    public static void main(String[] args) throws IOException {
        ServerSocket listener = new ServerSocket(61231);
        try {
            while (true) {
                Socket socket = listener.accept();
                try {
                    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                    out.println(new Date().toString());
                } finally {
                    socket.close();
                }
            }
        } finally {
            listener.close();
        }
    }
}

DataClient.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import javax.swing.JOptionPane;


public class DataClient {
    /*** Runs the client as an application.
     * First it displays
     * a dialog box asking for the IP address or hostname
     * * of a host running the date server, then connects to
     * * it and displays the date that it serves.*/
    public static void main(String[] args) throws IOException {
        // String serverAddress = JOptionPane.showInputDialog(
        //     "Enter IP Address of a machine that is\n" +
        //     "running the date service on port 9090:");
        System.out.print("Please input the server address:");
        // Enter data using BufferReader
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        //Reading data using readLine
        String serverAddress = reader.readLine();

        Socket s = new Socket(serverAddress, 61231);
        BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String answer = input.readLine();
        System.out.println("Reply" + answer);
        //JOptionPane.showMessageDialog(null, answer);
        System.exit(0);
    }
}

AbouLkhair
  • 176
  • 2
  • 14
HappyHour
  • 49
  • 6

0 Answers0