title: Java Sockets: My server input stream will not read from the client output stream?
body: <p>EDIT: Ik it is long but does anyone know how to program sockets??</p>
  
  <p>My problem is confusing me a bit.  I have a server running on one computer and on another, I have a client connected to it.  When I type a message from the client into the console and send it, the server does not seem to receive it.  Anybody know why because I have been testing with printing to the console for the last 3 hours and cannot figure this out.  I am relatively new to sockets so don't be too harsh if I am just being an idiot.</p>
  
  <p>Heres my code for the client side:</p>
  
  <pre><code>import java.net.*;
  import java.util.Scanner;
  import java.io.*;
  
  public class SocketClient {
  
  public static void main(String [] args) {
      String host = "************";
      int port = 25565;
  
      StringBuffer instr = new StringBuffer();
      String TimeStamp;
      System.out.println("SocketClient initialized");
  
      try {
          InetAddress address = InetAddress.getByName(host);
          Socket connection = new Socket(address, port);
  
          BufferedOutputStream bos = new     BufferedOutputStream(connection.getOutputStream());
          OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");
  
          Scanner scan = new Scanner(System.in);
          String message = scan.nextLine();
  
          TimeStamp = new java.util.Date().toString();
          String process = "Server called on " + host + ":" + port + " at " + TimeStamp + ": " + message + (char) 13;
  
          osw.write(process);
          osw.flush();
  
          BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
  
          InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");
  
          int c;
          while ( (c = isr.read()) != 13)
              instr.append( (char) c);
  
          connection.close();
          System.out.println(instr);
  
      } catch (UnknownHostException e) {
          System.err.println("UnknownHostException: " + e);
      } catch (IOException e) {
          System.err.println("IOExcepion: " + e);
      }
  }
  }
  </code></pre>
  
  <p>Here is the code to connect the client to the server:</p>
  
  <pre><code>import java.io.IOException;
  import java.net.*;
  
  public class MultipleSocketServer {
  
  public static Socket connection;
  public static String name = "Tyler's Server";
  public static int limit = 2;
  public static Thread[] clients = new Thread[limit];
  public static int current = 0;
  public static int port = 25565;
  public static String[] connected = {"", ""};
  public static ServerSocket socket;
  
  public static void main(String[] args) {
      System.out.println("Server starting...");
      try {
          ServerSocket socket = new ServerSocket(port);
          while(true) {
              Socket connection = socket.accept();
              String ip = connection.getRemoteSocketAddress().toString().substring(1, 13);
              loop:
              for(int i = 0; i &lt; connected.length; i++) {
                  if(connected[0].equals(ip) || connected[1].equals(ip)) {
                      break loop;
                  }else if(!connected[i].equals(ip)) {
                      connected[i] = ip;
                      System.out.println(ip);
                      MultiServer_Client client = new     MultiServer_Client(connection, i);
                      Thread run = new Thread(client);
                      run.start();
                      break loop;
                  }
              }
          }
      } catch (IOException e1) {
          System.out.println("Could not bind server on: " + port);
          System.exit(-1);
      }
  }
  }
  </code></pre>
  
  <p>And here is my code to handle each client as connected:</p>
  
  <pre><code>import java.io.BufferedInputStream;
  import java.io.BufferedOutputStream;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  import java.net.Socket;
  
  public class MultiServer_Client implements Runnable {
  
  public String time;
  public Socket client;
  public StringBuffer process = new StringBuffer();
  
  public BufferedInputStream inputStream;
  public InputStreamReader reader;
  
  public BufferedOutputStream outputStream;
  public OutputStreamWriter writer;
  
  public boolean connected = true;
  
  public int ID;
  
  public MultiServer_Client(Socket connection, int i) {
      client = connection;
      ID = i;
      try {
          inputStream = new BufferedInputStream(client.getInputStream());
          reader = new InputStreamReader(inputStream);
  
          outputStream = new BufferedOutputStream(client.getOutputStream());
          writer = new OutputStreamWriter(outputStream, "US-ASCII");
      } catch (IOException e) {
          System.out.println("IOException: " + e);
      }
      System.out.println("Client connected...");
      write("Connected to " + MultipleSocketServer.name);
  }
  
  public void run() {
      while(connected) {
          write("hi");
      }
      System.out.println("Disconnecting client...");
  }
  
  public void write(String authen) {
      try {
          time = new java.util.Date().toString();
          String message = time + ": " + authen + (char) 13;
          writer.write(message);
          writer.flush();
      } catch (IOException e) {
          connected = false;
          MultipleSocketServer.connected[ID] = "";
      }
  }
  
  public void read() {
      //read from client
      int character;
      process = new StringBuffer();
      try {
          while ((character = reader.read()) != 13) {
              process.append((char) character);
          }
          System.out.println(process);
          process.delete(0, process.length());
      } catch (IOException e) {
          connected = false;
          MultipleSocketServer.connected[ID] = "";
      }
  }
  }
  </code></pre>
  
  <p>Srry if I cannot help very much.  As I said, I am new to sockets and no one else seems to have any problems with this... Thanks :)</p>