Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, UDP is quite reliable (in fact, as reliable as the IP protocol itself). TCP simply ensures that the data was received which involved quite a lot of magic in the back end. Unless you absolutely need to be sure that other machines got the message, you could do it with UDP. Mind that I'm not saying “Don't use TCP”, I just want to make it straight that you should take UDP into consideration as well.</p> <p>Anyway, yes, you can create a simple listening program. Here is an example of a client in Java that reads messages from the server. It overrides the run method of a Thread class:</p> <pre><code>public void run() { try { String messageFromServer = reader.readLine(); while (messageFromServer != null) { // Do things with messageFromServer here // processor.processFromServer(messageFromServer); messageFromServer = reader.readLine(); // Blocks the loop, waits for message } } catch (IOException e) { // Handle your exception } } </code></pre> <p>Amongst other things, my thread was set up as such:</p> <pre><code>public CommunicationThread(String hostname, int port, int timeout) throws IOException, SocketTimeoutException { InetSocketAddress address = new InetSocketAddress(hostname, port); socket = new Socket(); socket.connect(address, 2000); // 2000ms time out // You can use the writer to write messages back out to the server writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); } </code></pre> <p>Now, regards to server-side you can do something as follows:</p> <ol> <li>Write a program to allow clients to contact, given that they know your address.</li> <li>Accept the connections, and store the sockets in a list.</li> <li>When you need to send out a message, traverse the list and send the data to everyone on it.</li> </ol> <p>You can start listening on your server with</p> <pre><code>this.socket = new ServerSocket(port); </code></pre> <p>You could (or even should(?)) make it threaded so that you can accept clients while serving others. You can accept new clients with:</p> <pre><code>socket.accept(); // Blocks, waiting for someone to connect, returns open socket </code></pre> <p>Feel free to pass that to a whole new class which can deal with BufferedWriter (and maybe even BufferedReader if you want to read from clients as well). That class is where you would implement things such as writeToClient(message) Consider the situation where you have a ClientConnection class that has writeToClient(String s) method and (Server server, Socket socket) and initialized ArrayList conList. Here is how you would follow: In a separate thread in Server, accept connections with</p> <pre><code>ClientConnection con = new ClientConnection(this, socket.accept()); conList.add(con); </code></pre> <p>Then, when you want to write to clients:</p> <pre><code>for (ClientConnection c : conList) { c.writeToClient("I'm sending you a message!"); } </code></pre> <p>I hope you get a vague idea of what you need to do. Read the Socket documentation, it's very useful. Also, as always with threaded applications, make sure you aren't doing things such as modifying a list while traversing it and avoid race conditions.</p> <p>Good luck!</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.
 

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