Note that there are some explanatory texts on larger screens.

plurals
  1. POTCP stream being read by Java
    primarykey
    data
    text
    <p>I would like to create a Java server socket application that receives a TCP packet and reads the content of it. Based on the contents of the packet it will perform several actions. I managed to get to the point where it reads some content and prints a string System.out.println(sb.toString()); But (a) not all the content is printed and (b) I am not sure how to process the content as they arrive in network order. An example would be to receive an HTTP packet and from the header to report the "Content-Length" or the "User-Agent". Any example would be appreciated.</p> <pre><code>public static void main(String[ ] args){ PrintWriter out = null; BufferedReader in = null; int bufferSize = 0; try{ String message = args[0]; int count = 0; ServerSocket connectionSocket = null; try { connectionSocket = new ServerSocket(4444); System.out.println("Server started"); } catch (IOException e) { System.err.println("Could not listen on port: 4444."); System.exit(1); } Socket clientSocket = null; try { while(true){ count++; clientSocket = connectionSocket.accept(); System.out.println("TCP packet received… " + count); InputStream is = clientSocket.getInputStream(); out = new PrintWriter(clientSocket.getOutputStream()); in = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; while ((line = in.readLine()) != null) { sb.append(line + "\n"); } System.out.println(sb.toString()); clientSocket.close(); } } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } } catch(Exception e){ e.printStackTrace(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload