Note that there are some explanatory texts on larger screens.

plurals
  1. POJava server client (I/O connectionException)
    text
    copied!<p>I have written a program where the client should be able to establish connection, and write to som message to the server. And the server should print this message.</p> <p>The problem is that once the client connect to the server, I get an exception with the following message:</p> <pre><code>java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:529) at java.net.Socket.connect(Socket.java:478) at java.net.Socket.&lt;init&gt;(Socket.java:375) at java.net.Socket.&lt;init&gt;(Socket.java:189) at Client.&lt;init&gt;(Client.java:21) at Server.startServer(Server.java:42) at Server.main(Server.java:62) </code></pre> <p>My code for the server is as shown below:</p> <pre><code>import java.io.*; import java.net.*; public class Server { private int port = 5555; public void printServerAddress() { InetAddress i = null; String host = null; try { i = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } host = i.getHostAddress(); System.out.println("Contact this server at address: " + host + " and port: " + port); } public void startServer() { ServerSocket serverSocket = null; BufferedReader reader = null; Socket socket = null; String fromClient = null; try { serverSocket = new ServerSocket(port); while(true) { socket = serverSocket.accept(); reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); if(socket != null) { System.out.println("Connection from: " + socket); } Thread thread = new Thread(new Client(socket)); thread.start(); while((fromClient = reader.readLine()) != null) { System.out.println("From client: " + fromClient); } reader.close(); socket.close(); } } catch (IOException e) { } } public static void main(String[] args) { Server server = new Server(); server.printServerAddress(); server.startServer(); } } </code></pre> <p>And the code for the client is:</p> <pre><code>import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; public class Client implements Runnable{ private int port; private String serverAddress; private PrintWriter writer; private BufferedReader reader; public Client(Socket socket) { try { socket = new Socket(serverAddress, port); writer = new PrintWriter(socket.getOutputStream(), true); reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { System.out.println("Write message to server:"); Scanner scanner = new Scanner(System.in); while(scanner.next() != "quit") { writeToServer(scanner.next()); } writer.close(); } public void writeToServer(String message) { writer.write(message); } public void readFromServer() { String msg = null; try { while((msg = reader.readLine()) != null) { System.out.println("From server: " + msg); } reader.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws UnknownHostException, IOException { Socket s = new Socket("localhost", 5555); new Client(s); } } </code></pre> <p>Any idea on what I did wrong, and how to fix my problem?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload