writing simple java server/client. need help with PrintWriter/Scanner

Discussion in 'Mac Programming' started by seventeen, Nov 29, 2009.

  1. seventeen macrumors member

    Joined:
    Apr 9, 2009
    Location:
    Denton, Tx
    #1
    I'm writing a simple server and client in java for an assignment and I can't even seem to get the server to receive anything from the client.

    I'm using a PrintWriter to send data from the client and a Scanner to receive it on the server.

    In all blatant honesty... I don't really know what I'm doing... at all. So it's incredibly likely that I've overlooked something stupid. I went ahead and copied both files in full below for review. Thanks for any help!

    Client Code:
    Code:
    
    import java.io.*;
    import java.util.*;
    import java.net.*;
    
    public class myClient {
    	
    	protected static Socket sock;
    	
    	public static void main(String args[]) {
    		try {
    			sock = new Socket("127.0.0.1",55555);
    			DataInputStream response = new DataInputStream(sock.getInputStream());
    			PrintWriter request = new PrintWriter(sock.getOutputStream());
    			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    			String txt = "";
    						
    			while(!txt.equals("EXIT")) {
    				
    				System.out.print("prompt:");
    				txt = in.readLine();
    				
    				request.print(txt);
    				request.flush();
    				
    			}
    			response.close();
    			request.close();
    			in.close();
    			sock.close();
    
    			
    		} catch(IOException e) {
    			System.out.println(e.toString());
    		}
    	}
    	
    }
    Server:
    Code:
    
    import java.io.*;
    import java.util.*;
    import java.net.*;
    
    public class myServer {
    	
    	protected static final int PORT_NUMBER = 55555;
    	
    	public static void main( String args[]) {
    				
    		try {
    		
    			ServerSocket servsock = new ServerSocket(PORT_NUMBER);
    						
    			String instr;
    		
    			while(true) {
    			
    				Socket sock = servsock.accept();
    				Scanner in = new Scanner(sock.getInputStream());
    				PrintWriter out = new PrintWriter(sock.getOutputStream());
    																
    				System.out.println(in.next());
    				
    				in.close();
    				out.close();
    				sock.close();
    							
    			}
    			
    		} catch(Exception e) {
    			System.out.println(e.toString());
    		}
    		
    	}
    	
    }
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    The changes I made:
    On the client side, changed the request.print to request.println so a newline goes across.
    On the server side, did the same... change the in.read to in.readLine.
    On the server side, moved all of the socket opening, scanner setup, printerwriter setup outside of the while, as well as the close of these. This keeps the socket open, and reading a line at a time (forever).

    If you want a new connection for each line entered, then you'd need to be reconnecting the client each time, as well as the cycling the connection on the server. This seems silly, but if that's what's needing, the client and server need to match. Right now, your client thinks it's keeping its connection, but the server is disconnecting and restarting the socket each time it gets a message.

    This seems to work in a way that seems reasonable to me, but i have no idea if it meets the requirements for your project.

    -Lee
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    What are you working from, a book, an online tutorial, something else?

    Maybe you should look at an example. There's a knock-knock server example in Sun's Java Tutorial:
    http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html

    The book "Java Examples in a Nutshell" is also worthwhile. The code is available here (see examples 7-4 and 7-5 for an "echo reversed" example):
    http://oreilly.com/catalog/javanut/examples/

    You can get the book lots of places, including Amazon.com.

    If you don't know anything at all about TCP/IP networking (which is what sockets use), you could do worse than reading about it on Wikipedia.


    There's something important your code hasn't considered: text encoding.

    If the client has a different default text encoding than the server, then the Reader/Writer classes will malfunction. It may work sometimes (even most times), or it may garble a few characters, or it may fail with an IOException due to undecodable data being received. You should make sure both sides use the same encoding, and in general it should be UTF-8, unless there is a good reason to use something else.

    Text encoding is something you can worry about later, but it's something you need to understand and deal with, or it will lead to surprising and unexpected bugs.
     

Share This Page