phttpd: pseudo hyper text transfer protocol daemon (pseudo HTTP processing)
"TCP / IP JAVA network programming understood from the basics" Compiled with Eclipse. Debug with Wireshark. https://qiita.com/drafts/14519536b827fdadb32e/
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);
}
}
}
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.
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
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.
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