Programming with TCP/IP – Best Practices Matt Muggeridge TCP/IP for OpenVMS Engineering 'Be liberal in what you accept, and conservative in what you send'.
In the late nineties I was working for an online video game company where I spent my days writing Unix Berkley Sockets and Windows WinSock code. Twinhead durabook r15d drivers. My task was to enable video game clients to communicate with a game server. When I had the opportunity to write some Java socket code, I was amazed by Java's streamlined and straightforward approach to network programming. This is no surprise because Java was originally designed to enable smart devices to communicate with one another, which translated very well to desktop and server applications.
In 1996, JavaWorld published '.' Written by Qusay H.
Java Tcp Example
Mahmoud, the article presents an overview of the Java model for socket programming. While a few things have changed in the 18 years since, Mahmoud's article is still a classic introduction to Java socket programming in networked systems.
How To Program In Java
I'll build on that work here by first presenting a simple client-server example that demonstrates the humble beginnings of Java I/O. The example will demonstrate features from both the original java.io package and NIO, the new, non-blocking I/O ( java.nio) APIs introduced in Java 1.4. My final example will leverage some of the NIO.2 features introduced in Java 7. Socket programming with Java: TCP and UDP Socket programming boils down to two systems communicating with one another.
Simple Java Tcp Server
Network communication comes in two flavors: Transport Control Protocol (TCP) and User Datagram Protocol (UDP). TCP and UDP are used for different purposes and both have unique constraints:. TCP is relatively simple and reliable protocol that enables a client to make a connection to a server and the two systems to communicate. In TCP, each entity knows that its communication payloads have been received. UDP is a connectionless protocol and is good for scenarios where you do not necessarily need every packet to arrive at its destination, such as media streaming. To appreciate the difference between TCP and UDP, consider what would happen if you were streaming video from your favorite website and it dropped frames.
Would you prefer that the client slow down your movie to receive the missing frames or would you prefer that the video continue playing? Video streaming protocols typically leverage UDP. Because TCP guarantees delivery, it is the protocol of choice for HTTP, FTP, SMTP, POP3, and so forth. Old-school Java sockets In implementations prior to NIO, Java TCP client socket code is handled by the java.net.Socket class. The following code opens a connection to a server: Socket socket = new Socket( server, port ); Once our socket instance is connected to the server we can start obtaining input and output streams to the sever. Input streams are used to read data from the server while output streams are used to write data to the server.
We can execute the following methods to obtain input and output streams: InputStream in = socket.getInputStream; OutputStream out = socket.getOutputStream; Because these are ordinary streams, the same streams that we would use to read from and write to a file, we can convert them to the form that best serves our use case. For example, we could wrap the OutputStream with a PrintStream so that we can easily write text with methods like println. For another example, we could wrap the InputStream with a BufferedReader, via an InputStreamReader, in order to easily read text with methods like readLine.