Java NetworkingjavaNetwork
OverviewReally two separate thoughts, but small enough they don't deserve individual posts. One is a basic HTTP Server written in Java, the sample provided will take in a word and see if it can find an image to serve for that word. The other is a basic file transfer tool, akin to FTP (Although you can't list directories or anything). Web Server Picture![]() Java Byte Signing IssuesSomeone e-mailed and pointed out a bug with certain files being transferred(The server was closing before the client). Turns out that Java signs byte values, so any bytes being converted with the MSB as 1 were actually subtracting from the total value, making the server expect a smaller file size. Since Java has no concept of signed vs. unsigned, you have to mask the value and convert to type with more bits. For example: byte b = 211; // Binary is 11010011
int i = (int)(b);
System.out.println("Value: " + i);
So you have to first do a mask each time you are converting a byte: byte b = 211;
int i = (int)( b & 0xFF );
System.out.println("Value: " + i);
Source Code
ConclusionNowadays I use a Java Application Server for any webserver needs in Java. They are a little heavyweight though and there may be some applications were that's overkill. Also the network transfer tool was originally intended for a P2P program, I've since found Freenet, and believe it's better to develop on top of that instead of trying to reinvent the wheel.
|
Posts
Source Code