rt

An obligatory Java Project
Log | Files | Refs | LICENSE

commit 7d8cbd61d27d2bcc9b385f3796f59b8362540cea
parent 5d6d77dccdb964df40e1290e90f96f1262a2d84d
Author: zerous Naveen Narayanan <zerous@nocebo.space>
Date:   Sun, 24 Nov 2019 17:59:35 +0100

Use separate threads for reading and writing from/to socket

Diffstat:
MMakefile | 3+++
Mclient.java | 41+++++++++++++++++++++--------------------
Aclienttask.java | 36++++++++++++++++++++++++++++++++++++
3 files changed, 60 insertions(+), 20 deletions(-)

diff --git a/Makefile b/Makefile @@ -9,6 +9,9 @@ servertask: client: javac client.java +clienttask: + javac clienttask.java + util: javac util.java diff --git a/client.java b/client.java @@ -2,45 +2,46 @@ package space.nocebo; import java.net.*; import java.io.*; +import java.util.concurrent.*; public class client { + protected static int cexit = 0; public static void main(String[] args) { int port = 1234; String ip = "127.0.0.1"; + Socket connection; + + ExecutorService exec = Executors.newFixedThreadPool(5); + try { - Socket connection = new Socket(ip, port); + connection = new Socket(ip, port); System.out.println("Connected! " + connection); + exec.execute(new clienttask(connection)); + DataInputStream inputStr = new DataInputStream( new BufferedInputStream(connection.getInputStream())); - DataOutputStream outputStr = new DataOutputStream( - new BufferedOutputStream(connection.getOutputStream())); String serv_mes = ""; String s = ""; - while (s.compareTo("q") != 0) { - serv_mes = inputStr.readUTF(); - System.out.print(serv_mes + "> "); - - BufferedReader stdin = new BufferedReader( - new InputStreamReader(System.in)); - if ((s = stdin.readLine()) != null && s.length() != 0) { - outputStr.writeUTF(s); - outputStr.flush(); - } + while (cexit == 0) { + if (inputStr.available() > 0) { + serv_mes = inputStr.readUTF(); + System.out.print(serv_mes + "> "); + } else + Thread.sleep(1000); } - // kill the server - outputStr.writeUTF(s); - outputStr.flush(); - - // kill the client - outputStr.close(); inputStr.close(); // close the connection connection.close(); + exec.shutdown(); + } catch (IOException ex) { - System.out.println("Couldn't establish connection: " + ex); + System.out.println("Client couldn't establish connection: " + ex); + } catch (InterruptedException ex) { + System.out.println("Sleep interrupted: " + ex); } + } } diff --git a/clienttask.java b/clienttask.java @@ -0,0 +1,36 @@ +package space.nocebo; + +import java.net.*; +import java.io.*; + +public class clienttask implements Runnable{ + private Socket con; + + public clienttask(Socket s) { + con = s; + } + @Override + public void run() { + try { + DataOutputStream outputStr = new DataOutputStream( + new BufferedOutputStream(con.getOutputStream())); + String s = ""; + while (s.compareTo("q") != 0) { + BufferedReader stdin = new BufferedReader( + new InputStreamReader(System.in)); + if ((s = stdin.readLine()) != null && s.length() != 0) { + outputStr.writeUTF(s); + outputStr.flush(); + } + } + + outputStr.writeUTF(s); + outputStr.flush(); + + outputStr.close(); + client.cexit = 1; + } catch (IOException ex) { + System.out.println("IOException: " + ex); + } + } +}