import java.util.*; import java.net.*; import java.io.*; public class lab6FullDuplex{ public static void main (String args[]) throws Exception { System.out.println ("Is this Server or Client? [S/c]"); Scanner scn = new Scanner (System.in); String input = scn.nextLine(); Socket s; int port = Integer.parseInt(args[0]); char ch = input.charAt(0); if (ch =='C' || ch == 'c'){ s = new Socket("localhost",port); } else { ServerSocket ss = new ServerSocket(port); s = ss.accept(); } new Thread(new lab6FullDuplex.reader(s)).start(); new Thread(new lab6FullDuplex.writer(s)).start(); } public static class reader implements Runnable { public Socket s; public reader(Socket s){ this.s = s; } @Override public void run () { try { InputStreamReader r = new InputStreamReader (s.getInputStream()); while (true) { char dataLine [] = new char [8*1024]; if (r.read(dataLine)!=-1) { System.out.println("Receiver: "+new String(dataLine)); } } } catch (Exception e) { System.out.print(e); } } } public static class writer implements Runnable { public Socket s; public writer(Socket s){ this.s = s; } @Override public void run () { try { OutputStreamWriter w = new OutputStreamWriter (s.getOutputStream()); Scanner scn = new Scanner (System.in); while (true) { //System.out.print("Sender: "); String message = scn.nextLine(); w.write(message); w.flush(); } } catch (Exception e) { System.out.println(e); } } } }