Category Archives: Java

A Simple Java TCP Server and TCP Client 43

Following up on my previous post, we also had to demonstrate a sample Java TCP Server and TCP Client. The code footprint pretty small and it gives you a good idea about how a TDP Server opens up a port, and then the TCP Client sends or receives data from that port. This is a good page on the differences between TCP and UDP. To compile these, install Java JDK to your system. Then compile the program with javac TCPClient.java – this will create a TCPClient.class. Execute the file with java TCPClient – leave off the .class, or you will get the error: “Exception in thread “main” java.lang.NoClassDefFoundError”. Here is sample code for a simple Java TCP Server/Client, originally from the excellent Computer Networking: A Top Down Approach, by Kurose and Ross: TCPServer.java 123456789101112131415161718192021222324import java.io.*; import java.net.*; class TCPServer {    public static void main(String argv[]) throws Exception    ….

A Simple Java UDP Server and UDP Client 44

For a class I am taking, we are testing out a simple UDP Server and UDP Client to demonstrate what each one does and how sockets work. The code size is very small and give you a good idea about how a UDP Server opens up a port, and then the UDP Client sends or receives data from that port. To compile these, install Java JDK to your system. Then compile the program with javac UDPClient.java – this will create a UDPClient.class. Execute the file with java UDPClass – leave off the .class, or you will get the error: “Exception in thread “main” java.lang.NoClassDefFoundError”. Here is sample code for a simple Java UCP Server and Client, originally from Computer Networking: A Top Down Approach, by Kurose and Ross: UDPServer.java: 1234567891011121314151617181920212223242526import java.io.*; import java.net.*; class UDPServer {    public static void main(String args[]) throws Exception       {        ….