What I want to do is have the server send back what the client send it, but it does not do anything, what did I do wrong with the code here?
Here is the client code
and here is the server code:import java.io.*; import java.net.*; import java.util.Scanner; /** * * @author Kenshin */ public class Client { public static void main(String[] args) throws IOException { final int port = 8189; String s = ""; try{ Socket client = new Socket(s, port); DataOutputStream socketOut = new DataOutputStream(client.getOutputStream()); InputStream inStream = client.getInputStream(); Scanner in = new Scanner(inStream); while (in.hasNextLine()) { String line = in.nextLine(); System.out.println(line); } client.close(); }catch(UnknownHostException e) { System.err.println(": unknown host."); } } }
Thanks for any helpimport java.io.*; import java.net.*; import java.util.*; /** This program implements a multithreaded server that listens to port 8189 and echoes back all client input. @author Cay Horstmann @version 1.20 2004-08-03 */ public class ThreadedEchoServer { public static void main(String[] args ) { try { int i = 1; ServerSocket s = new ServerSocket(8189); while (true) { Socket incoming = s.accept(); System.out.println("Spawning " + i); Runnable r = new ThreadedEchoHandler(incoming); Thread t = new Thread(r); t.start(); i++; } } catch (IOException e) { e.printStackTrace(); } } } /** This class handles the client input for one server socket connection. */ class ThreadedEchoHandler implements Runnable { /** Constructs a handler. @param i the incoming socket @param c the counter for the handlers (used in prompts) */ public ThreadedEchoHandler(Socket i) { incoming = i; } public void run() { try { try { InputStream inStream = incoming.getInputStream(); OutputStream outStream = incoming.getOutputStream(); Scanner in = new Scanner(inStream); PrintWriter out = new PrintWriter(outStream, true /* autoFlush */); out.println( "Hello! Enter BYE to exit." ); // echo client input boolean done = false; while (!done && in.hasNextLine()) { String line = in.nextLine(); out.println("Echo: " + line); if (line.trim().equals("BYE")) done = true; } } finally { incoming.close(); } } catch (IOException e) { e.printStackTrace(); } } private Socket incoming; }