Results 1 to 2 of 2
  1. #1
    bayan.rafeh is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default SOLVED:Java Hangs when writing response to socket

    I'm writing a small server application, it just reads a browser request, prints it and returns a response. The problem is when I try to write to the output stream it hangs. No exception, no unusual exit codes, it just gets stuck on the write instruction(doesn't reach flush).

    Here's my script:

    Java Code:
    import java.io.*;
    import java.net.*;
    public class TestSocket{
    	public static void main(String[] args) throws IOException{
    		while(true){
    			// Listen for client connection
    			ServerSocket server = new ServerSocket(9999);
    			Socket client = server.accept();
    			System.out.println("Done");
    			// Get the IO Streams
    			BufferedReader clientIn = new BufferedReader(new InputStreamReader(client.getInputStream()));
    			BufferedWriter clientOut = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
    			String str;
    			// Counter for logging purposes
    			int cnt = 0;
    			
    			// Read HTTP Request
    			while((str=clientIn.readLine())!=null){
    				System.out.print(cnt);
    				System.out.println(str);
    				cnt++;
    			}
    			
    			System.out.println(cnt);
    			// Write response, script hangs here
    			clientOut.append("HTTP/1.0 200 OK\nDate: Sun, 04 Nov 2012 16:52:19 GMT\nContent-Type: text/html;\nConnection: keep-alive\n<html><body><h5>It worked</h5></body></html>");
    			clientOut.flush();
    			cnt++;
    			// Clean up
    			clientIn.close();
    			clientOut.flush();
    			clientOut.close();
    			client.close();
    		}
    	}
    }
    Any help greatly appreciated.
    Last edited by bayan.rafeh; 11-04-2012 at 08:27 PM. Reason: Resolved

  2. #2
    bayan.rafeh is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Java Hangs when writing response to socket

    Solved it, the problem isn't with write, it's with read. The line separator is never read, so it keeps waiting for it.

    I solved the problem by replacing readLine() in the while loop by read(), so I'm reading the response character by character instead of line by line.

    Java Code:
    import java.io.*;
    import java.net.*;
    public class TestSocket{
    	public static void main(String[] args) throws IOException{
    		ServerSocket server = new ServerSocket(9998);
    		while(true){
    			// Listen for client connection
    			Socket client = server.accept();
    			System.out.println("Done");
    			// Get the IO Streams
    			BufferedReader clientIn = new BufferedReader(new InputStreamReader(client.getInputStream()));
    			BufferedWriter clientOut = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
    			char c;
    			// Counter for logging purposes
    			
    			// Read HTTP Request CHARACTER BY CHARACTER instead of line by line
    			while((c=(char)clientIn.read())!=0 && c !='\n'){
    				System.out.print(c);
    			}
    			// Write response, script hangs here
    			clientOut.append("HTTP/1.0 200 OK\nDate: Sun, 04 Nov 2012 16:52:19 GMT\nContent-Type: text/html;\nConnection: keep-alive\n<html><body><h5>It worked</h5></body></html>");
    			clientOut.flush();
    			// Clean up
    			clientIn.close();
    			clientOut.flush();
    			clientOut.close();
    			client.close();
    		}
    	}
    }
    Last edited by bayan.rafeh; 11-04-2012 at 08:29 PM.

Similar Threads

  1. Replies: 0
    Last Post: 04-07-2011, 04:09 PM
  2. Writing to Socket outputStream w/o closing it
    By eyal in forum Networking
    Replies: 6
    Last Post: 05-17-2010, 05:26 PM
  3. append response to the request from Socket and write to another socket
    By vaibhav_singh_vs@yahoo.co in forum Networking
    Replies: 3
    Last Post: 04-17-2009, 08:02 PM
  4. java mail hangs
    By ashwinkumar18 in forum Advanced Java
    Replies: 6
    Last Post: 03-14-2009, 11:38 AM
  5. Replies: 4
    Last Post: 10-02-2008, 07:32 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
  •