Java is very strong in communication world. To support, LAN programming, the java.net package comes with two classes Socket and ServerSocket. The meaning and purpose these classes and network related basics, you have studied in Networking – Introduction. Socket and ServerSocket are extensively I/O streams for data traversal and for this reason, sockets are called as stream sockets. These two classes are very confusing for a novice. Let us make it clear. Socket job is to request the connection with the server and ServerSocket job is to bind the connection on the port number requested by the client. When a port number is binded by the server, it is allotted to the client as long as client wants. When client gets disconnected, the port number on the server gets freed and server can allot the port number to some other client, may be waiting.
Four programs are given here under to illustrate Communication with TCP/IP Protocol Java.
|1st application||Client to server communication (one-way)|
|2nd application||Server to client communication (one-way)|
|3rd application||Server sends file contents to client (two-way, non-continuous)|
|4th application||Chat program (two-way, continuous)|
Socket plays very important role in communication. It can send data or receive data. For this, the class comes with two important methods getInputStream() and getOutputStream(). Infact, these are the starting and basic points where communication starts.
All the above client-serer applications deal with sockets, server sockets, binding the connection, using streams like DataInputStream, BufferedReader etc and clearly explained.
Application and program are very different. An application comprises of many programs. An application has got a task; to do the task, the programmer should integrate multiple programs and execute them in an order. Here, the communication application includes two programs – a client program and a server programs. The server means it need not be a server; it can be any system in the network (just for differentiation, it is called as client and server).
Refer all the examples and pass your comments and suggestions on this tutorial "Communication with TCP/IP Protocol Java".