Results 1 to 2 of 2
  1. #1
    MichaelH is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default Client Server Communication

    Hello,

    I have a Server and a Client. The Client tries to receive a file from the Server. The Server sends the content of the File and a header. My Client works fine but I have a problem with my Server.

    I start the Server with
    > java Server <port>
    .
    And the Client with
    > java Client <name of Server> <port> <file>
    Every time I send a request with my Client to the Server I get always 200 ok as response, it doesn't matter if the file exists or not. The file is always in the same folder as the Server. So I don't know whats wrong with my Server-Code. If the file doesn't exists the response should be 404 Bad Request but it is always 200 ok, but why?

    That's my Server:

    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class Server {
    	
    	private int port;
    	private String type = "txt";
    	
    	public Server(int port) {
    	this.port = port;
    	}
    	
    	public void startServer() {
    		try {
    			ServerSocket server = new ServerSocket(port);
    			
    			InetAddress addr = InetAddress.getLocalHost();
    			System.out.println("HTTPServer " + addr.getHostName() + "/" + addr.getHostAddress() +
    					":" + port + " started ...");
    		
    		while (true) {
    			Socket client = server.accept();
    			File file = new File("data.txt");
    			byte[] bytearray = new byte [(int)file.length()];
    			BufferedOutputStream out = new BufferedOutputStream(client.getOutputStream());
    			FileInputStream inputStream = new FileInputStream(file);
    			BufferedInputStream bis = new BufferedInputStream(inputStream);
    			bis.read(bytearray,0, bytearray.length);
                OutputStream outputStream = client.getOutputStream();
    			
    			try  {
    				
    				if(file.exists()) {
    				
    				
    				String header = "HTTP/1.1 200 OK\r\n" + "Content-Type: " + type + "\r\n" + 
    				"Content-Length: " + file.length() + "\r\n\r\n";
    				
    				outputStream.write(header.getBytes());
    				 
    				outputStream.write(bytearray,0,bytearray.length);
    				 
    				 } else (!file.exists()) {
    					String header = "HTTP/1.1 404 Bad Request\r\n" + "Content-Type: " + type + "\r\n" + 
    							"Content-Length: " + file.length() + "\r\n\r\n";
    					outputStream.write(header.getBytes());
    				
    			}	
    		
    				out.flush();
    				out.close();
    			
    			}
    			catch(IOException e) {
    				System.err.println(e);
    			} 
    			finally {
    				try {
    					if(client != null)
    						client.close();
    					if(inputStream != null)
    						inputStream.close();
    				}
    				catch(IOException e) {}
    				
    			}
    			}
    		
    		}
    		catch(IOException e) {
    			System.err.println(e);
    		}			
    		
    		
    		}
    		public static void main(String[] args) {
    			if(args.length != 1) {
    				System.err.println("HTTPServer <port>");
    				System.exit(1);
    		}
    			int port = Integer.parseInt(args[0]);		
    			
    			new Server(port).startServer();
    			}
    			}
    And this is my Client:

    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class Client {
    	public static void main(String[] args) throws UnknownHostException, IOException {
    		
    		String serverName = new String(args[0]);
    		int port = Integer.parseInt(args[1]);
    		String file = args[2];
    
    	        Socket s = new Socket(serverName, port);
    	        OutputStream os = s.getOutputStream();
    	        PrintWriter pw = new PrintWriter(os, false);
    	        pw.println("GET " + "http://" + s.getInetAddress() + file + " HTTP/1.1" + "\n");
    	        pw.println();
    	        pw.flush();
    	        
    	        InputStream in = s.getInputStream();
    	        InputStreamReader isr = new InputStreamReader(in);
    	        BufferedReader br = new BufferedReader(isr);
    	        String line;
    	        while ((line = br.readLine()) != null) {
    	          System.out.println(line);
    	        }
    	}
    	  
    	  }


    Thank you for your help!
    Last edited by MichaelH; 11-04-2012 at 07:48 PM.

  2. #2
    bakstar is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: Client Server Communication

    Perhaps it is because this doesn't output a boolean.

    Java Code:
    !file.exists()
    Try it with

    Java Code:
    if(file.exists()) {
    
    //Enter stuff if there is a file
    
    } else {
    
    //Enter stuff if there isn't a file
    
    }
    Last edited by bakstar; 11-09-2012 at 12:08 PM.

Similar Threads

  1. Replies: 0
    Last Post: 07-16-2012, 08:44 PM
  2. Use Servlet for client to client communication?
    By ChexWithRaisins in forum Java Servlet
    Replies: 0
    Last Post: 08-03-2011, 06:29 AM
  3. Client to Client communication
    By ja107 in forum Networking
    Replies: 6
    Last Post: 10-09-2010, 03:52 AM
  4. Replies: 2
    Last Post: 09-24-2010, 12:31 PM
  5. client-server communication problem
    By revathi17 in forum New To Java
    Replies: 1
    Last Post: 08-09-2007, 03:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •