Search


Rss feeds
Posts Comments Source Code
Rating
Image to SpectrogramNontransitive DicecheaTorrentDomain ColoringiMac G5 CPU Fan view all...
Recent
Pretty GraphHypernova EngineEmergent FeedbackSDL Euclid OrchardSingularity Viewer view all...
Tags

All source code released under the BSD License unless otherwise specified
© 2010, Gavin Black

Java Networking

Overview

Really 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

Web server running

Java Byte Signing Issues

Someone 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);
will spit out: Value: -45
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);
Now you get what you'd expect: Value: 211

Source Code

Source Tree: http://devrand.org:8080/cgi-bin/cgit/javaNetwork/tree/
Snapshots: http://devrand.org:8080/cgi-bin/cgit/javaNetwork/commit/
Git Access: git clone http://devrand.org:8080/git/javaNetwork

Conclusion

Nowadays 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.

Last Edited: 2010-10-16 14:38:29

+ Add a comment