Experimenting with Java Sockets

Gooblah

Heh...
Joined
Jun 5, 2007
Messages
4,282
Hey guys,

Much as the title states, I'm working on a really really lame java chat client, which for now operates solely within the context of a console or command line. And before anyone says anything, it has to be written in Java, so please don't suggest writing the program in another language. Sorry.

I keep running into an issue with the Server side, where the Client claims it's connected to the specific port the Server is listening to, but the Serve refuses to acknowledge this. I'm running a bit of modified code from the Java Network Programming book, and I can't for the life of me figure out why it's going wrong.

Client -
Spoiler :
Code:
import java.io.*;
import java.net.*;

public class Client 
{
    public static void main(String[] args) throws IOException 
    {
        Socket clientSocket = null; //socket used to connect to port
        PrintWriter out = null; 
        BufferedReader in = null;

        try {
            clientSocket = new Socket(InetAddress.getLocalHost(), 139); //creates Socket bound on Port 139 of server
            System.out.println("Socket successfully bound to port 139.");
            System.out.println("Socket details: " + clientSocket.toString());
            
            /*
             * out is a PrintWriter; it takes output from the client (what I type into the console)
             * and writes it into the clientSocket's Output Stream, which is then read in by the server*/
            out = new PrintWriter(clientSocket.getOutputStream(), true); 
            
            /*
             * in is a BufferedReader; it takes input from the server (what the server types into the console)
             * and reads it into the client Socket's Input Stream (wrapped in an InputStreamReader)*/
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            
        }
        catch (UnknownHostException e) 
        {
            System.err.println("Don't know about host: " + InetAddress.getLocalHost().toString());
            System.exit(1);
        } 
        catch (IOException e) 
        {
            System.err.println("Couldn't get I/O for the connection to: " + InetAddress.getLocalHost().toString());
            System.exit(1);
        }

		BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
		String userInput;
	
		/*
		 * BufferedReader stdIn reads in the input typed into the console, and stores it into userInput; while
		 * userInput is not null...*/
		
		while ((userInput = stdIn.readLine()) != null) 
		{
		    out.println(userInput); //prints userInput into the PrintWriter
		    System.out.println("Server: " + in.readLine()); //prints output of the clientSocket
		}
	
		out.close();
		in.close();
		stdIn.close();
		clientSocket.close();
    }
}

Server/ClientTester -
Spoiler :
Code:
import java.net.*;
import java.io.*;
 
 
public class ClientTester {
 
  public static void main(String[] args) {
 
    int port;
    
    try {
      port = Integer.parseInt(args[0]);
    }  
    catch (Exception e) {
      port = 0;
    }
    
    port = 139;
    try {
      ServerSocket server = new ServerSocket(port, 1);
      System.out.println("Listening for connections on port " 
       + server.getLocalPort(  ));
 
      while (true) {
        Socket connection = server.accept(  );
        try {
          System.out.println("Connection established with " 
           + connection);
          Thread input = new InputThread(connection.getInputStream(  ));
          input.start(  );
          Thread output 
           = new OutputThread(connection.getOutputStream(  ));
          output.start(  );
          // wait for output and input to finish 
          try {
            input.join(  );
            output.join(  );
          }
          catch (InterruptedException e) {
          }
        }
        catch (IOException e) {
          System.err.println(e); 
        }
        finally {
          try {
            if (connection != null) connection.close(  );
          }
          catch (IOException e) {}
        }
      }
    }
    catch (IOException e) {
      e.printStackTrace(  );
    }
  
  }
 
}
 
class InputThread extends Thread {
  
  InputStream in;
  
   public InputThread(InputStream in) {
     this.in = in;
   }
 
   public void run(  )  {
   
     try {     
       while (true) {
         int i = in.read(  );
         if (i == -1) break;
         System.out.write(i);
       }
     }
     catch (SocketException e) {
       // output thread closed the socket
     }
     catch (IOException e) {
       System.err.println(e);
     }
     try {
       in.close(  );
     }
     catch (IOException e) { 
     } 
 
  }
 
}
 
class OutputThread extends Thread {
  
  Writer out;
    
  public OutputThread(OutputStream out) {
    this.out = new OutputStreamWriter(out);
  }
 
  public void run(  ) {
 
    String line;
    BufferedReader in 
     = new BufferedReader(new InputStreamReader(System.in));
    try {
      while (true) {
        line = in.readLine(  );
        if (line.equals(".")) break;
        out.write(line +"\r\n");
        out.flush(  );
      }   
    }
    catch (IOException e) { 
    } 
    try {
      out.close(  );
    }
    catch (IOException e) { 
    } 
    
   }
 
}

Thanks for any help!
 
Back
Top Bottom