Chapter 2 Network programming with JAVA phttpd Exception collection in 3 places

phttpd "TCP / IP JAVA network programming understood from the basics" by Tomohiro Odaka, Ohmsha

基礎.png

Exception collection in three places of phttpd.java in "Chapter 2 Network Programming with JAVA"

phttpd: pseudo hyper text transfer protocol daemon (pseudo HTTP processing)

See below for installation, compilation and debugging methods

"TCP / IP JAVA network programming understood from the basics" Compiled with Eclipse. Debug with Wireshark. https://qiita.com/drafts/14519536b827fdadb32e/

The following items have been modified. For modified lines, write /// at the end of the line.

phttpd.java


import java.io.*;
import java.net.*;
/// remove java.util.*

public class phppd {
 public static void main(String args[]) {
    ServerSocket servsock = null; 
    Socket sock;  
    OutputStream out;
    BufferedReader in;
    FileInputStream infile=null;
    int buff_size = 1024;///
    byte buff[] =new byte[buff_size];
    boolean cont =true;
    int i; // loop counter
    int backlog=300; int htpp=8080;///
    try{
      servsock = new ServerSocket(htpp, backlog);///
      while(true){
        sock = servsock.accept();  
        System.out.println("Connection Request"+(sock.getInetAddress()).getHostName());
        try {
         infile=new FileInputStream(args[0]);
        } catch(Exception e){
         System.out.println("1");///
         System.out.println(e);///
         System.exit(1);
        }
        in=new BufferedReader(new InputStreamReader(sock.getInputStream()));;
        out=sock.getOutputStream();
        for(i=0;i<2;++i) {///
          in.readLine();
        }///
        cont=true;
        while(cont) {
         try {
          int n=infile.read(buff);
          System.out.println("4");///
          System.out.println(buff);///
          out.write(buff,0,n);
         } catch(Exception e){
          System.out.println("2:");///
          System.out.println(e);///
          cont=false;
         }
        }
        sock.close();
        infile.close();
      }
    } catch(IOException e){
     System.out.println("3");///
     System.out.println(e);///
     System.exit(1);
    }
  }
}

Exceptions could be collected at all three locations.

error 1

Connection Request localhost
1
java.io.FileNotFoundException: index.html (No such file or directory)

I didn't create an index.html file.

error 2

2
Connection Request localhost
2
java.net.SocketException: Broken pipe (Write failed)

error 2-1

Connection Request localhost
2
java.lang.ArrayIndexOutOfBoundsException: len == -1 off == 0 buffer length == 1024

I started Wireshark before running. http.png

Looking at Wireshark, it sends and receives up to </ html>. The display of the browser (Safari version 11.0.1 (12604.3.5.1.1)) is page.png

Oh, it's http / 0.9, so it's no good. I'm looking for a way to send it at http / 1.1.

error 3

3
java.net.BindException: Address already in use (Bind failed)

It seems that he started the next one without finishing the previously started phttpd.

$ ps -ax | grep phppd
10427 ??         0:08.61 /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/bin/java -Dfile.encoding=UTF-8 -classpath /Users/administrator/eclipse-workspace/phppd/bin phppd index.html
10714 ??         0:07.68 /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/bin/java -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:51095 -Dfile.encoding=UTF-8 -classpath /Users/administrator/eclipse-workspace/phppd/bin phppd index.html
13190 ttys000    0:00.00 grep phppd

$ su Administrator
Password:
$ kill -9 10427
$ kill -9 10714

Why are both of them working? At other times, I couldn't start next time, so there is only one.

"TCP / IP JAVA network programming understood from the basics" Compiled with Eclipse. Debug with Wireshark. https://qiita.com/drafts/14519536b827fdadb32e/ "Recommended for those who have never touched JAVA. It is still valid after 20 years." Since I wrote that, I have to fix it so that it works.

Passed the Information-Technology Engineers Examination Network Specialist https://qiita.com/kaizen_nagoya/items/407857392ca5c5677ee4

Communication emulator port https://qiita.com/drafts/ce505bbea4229b83e93b

Macintosh compatible "TCP / IP analyzer creation and packet analysis understood from the basics Linux / FreeBSD compatible" Tomohiro Odaka Ohmsha https://qiita.com/kaizen_nagoya/items/517411b42fc5ceabd581

Recommended Posts

Chapter 2 Network programming with JAVA phttpd Exception collection in 3 places
Programming with direct sum types in Java (Neta)
Constraint programming in Java
Getting Started with Java Collection
Exception handling techniques in Java
Deep copy collection in Java
[Java] Basic terms in programming
Access the network interface in Java
Morphological analysis in Java with Kuromoji
Play with Markdown in Java flexmark-java
Java Performance Chapter 5 Garbage Collection Basics
[Java] Write the programming quiz SET INTERSECTION in one line with StreamAPI
Use OpenCV_Contrib (ArUco) in Java! (Part 2-Programming)
Concurrency Method in Java with basic example
Read xlsx file in Java with Selenium
Java (exception handling, threading, collection, file IO)
Split a string with ". (Dot)" in Java
Working with huge JSON in Java Lambda