Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From the same tutorial:</p> <blockquote> <p>... The Socket constructor used here requires the name of the machine and the port number to which you want to connect. The example program uses the host name taranis. This is the name of a hypothetical machine on our local network. When you type in and run this program on your machine, change the host name to the name of a machine on your network. Make sure that the name you use is the fully qualified IP name of the machine to which you want to connect. The second argument is the port number. Port number 7 is the port on which the Echo server listens.`</p> </blockquote> <p>In any case, you will probably want to change taranis to <code>"localhost"</code> and make sure an echo service is running on your machine. If it's not, you could use something like the following code to simulate an echo server.</p> <pre><code>import java.net.Socket; import java.util.Formatter; import java.util.Scanner; import java.io.IOException; import java.net.ServerSocket; import java.util.ArrayList; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class EchoServer { public static void main(String[] args) { try { new EchoServer(INSERTPORT).execute(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } private ServerSocket serverSocket; private int port; private ArrayList&lt;Client&gt; clientList; private ExecutorService clientRunner; public EchoServer(int port) throws IOException { this.port = port; serverSocket = new ServerSocket(port); clientRunner = Executors.newCachedThreadPool(); clientList = new ArrayList&lt;&gt;(); } public void sendMessageToAll(String message) { for (Client c : clientList) { c.displayMessage(message); } } public void execute() throws IOException { while (true) { clientList.add(new Client(serverSocket.accept(), this)); clientRunner.execute(clientList.get(clientList.size()-1)); } } private class Client implements Runnable { private Socket clientSocket; private Scanner input; private Formatter output; public Client(Socket s) throws IOException { clientSocket = s; input = new Scanner(clientSocket.getInputStream()); output = new Formatter(clientSocket.getOutputStream()); } public void displayMessage(String s) { output.format(s + "\n"); output.flush(); } @Override public void run() { while(clientSocket.isConnected()) { if(input.hasNextLine()) { sendMessageToAll(input.nextLine()); } } } } } </code></pre> <p>Edit: Just for completeness, as you mentioned some problems running the code, you run the server (this code) and leave it running in the background, then run the client (the code you posted). I tested it, works fine.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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