In order to understand network programming in Java, you need to understand couple of concepts first.
The first of which, is what form of network connection are you going to be making. Typically, many modern apps use a client server model, and you make a request from a URL Process where a client uses a REST API or something similar to that to connect to a server to get information either via XML, JSON, or a similar format.
However, that was not always the case. When Java was first introduced, that wasn’t really a thing at the time. Instead we often connected with Sockets. This is actually the most widely used, and allows for both client/server as well as peer to peer programming. It has been widely documented and is a well known entity.
There are two common protocols which we can use to send data over a network TCP and UDP.
TCP
TCP, or Transmission Control Protocol allows for reliable communication between two computers. It has built in error checking.
Large chunks of data are often broken up into smaller packets. These packets need to all come in, and come in the right order. TCP allows for this. If a packet is missing, the receiver will request the packet to be resent. If it is received out of order, it will hold the packets until all of the packets preceding it arrive and you can view the contents in order. Consider the issue of reading a web page out of order, like a Quentin Tarantino movie.
TCP is typically used of the IP (Internet Protocol) and it is why you often see them together as TCP/IP.
UDP
The User Datagram Protocol,or UDP, is a connectionless protocol. That means a connection between the two, or more, entities do not have to exist first, they can simply send packets of data. Data received out of order is ignored, and missing packets are not sent again.
While this may not seem to be “good” due to the potential errors, you have to realize the error rate might be low, and all things considered in a UDP environment, getting the next set of data is often more important. Let’s say you are reading a document, missing a key paragraph would be bad, and you wouldn’t want UDP. However, lets say your are watching a streaming show, and you miss a packet which is part of a frame of a live video. Your video might get “jumpy” but it’s not that big a deal because it doesn’t pause and wait for packets to be resent, and you keep with a live streaming set of information.
Socket Programming
In Java, we usually use Socket Programming to connect and transmit over TCP. In order to establish a connection, there are several things you will need to know.
The client and the server must both be willing to connect and have applications on both computers in order to establish a connection.
Java uses a Socket
class for the client to work with connecting to the server computer. When you go to establish a connection, you need to know both the address (usually IP) and the port which you will connect on.
Socket s=new Socket("localhost",1942); // attempts to establish a connection with the server
The server uses a ServerSocket
which will listen for requests from potential clients. It will use the accept method to do this, waiting until a request comes in, in which case it will return a Socket
object.
ServerSocket ss=new ServerSocket(1942);
Socket s=ss.accept();//establishes connection and waits for the client
At this point, you can establish a BufferedReader
for reading data from a DataInputStream
and/or DataOutStream
for reading and/or writing.
// as part of the client where the socket is connected
DataOutputStream out = new DataOutputStream(s.getOutputStream());
out.writeUTF("Hello: Evidence of Connection Established");
out.flush();
out.close();
s.close();
Notice how we get the stream from the socket, and then write it out.
On the server we’d have:
DataInputStream in = new DataInputStream(s.getInputStream());
String str = (String)in.readUTF();
System.out.println("message= " + str);
ss.close();
Of course, given the IO nature or sockets, and the chances of there being issues with communication between the server and client, etc, Exception handling will be necessary in the process of sending messages.
Intro to Network Programming with Java was originally found on Access 2 Learn