Network Socket
Network Socket
Create Network Connection via Socket
In network programming, a server
is a program that keeps listening to messages from clients. And a client
is a program that uses a server.
If two programs want to create a network connection, one of them should act as the server, and the other should be the client. The server creates a server socket, attaches it to a particular port on its own computer(i.e., attach the socket to an IP address and port number), and then listens for incoming connections:
1 | ServerSocket serverSocket = new ServerSocket(9527); // In Java, the server creates a ServerSocket, listening to the 9527 port. |
As for the client, it creates an ordinary socket, and uses it to make a connection request:
1 | Socket socket = new Socket("localhost", 9527); // In Java, the client creates a Socket, using the IP address and port number to connect the ServerSocket. Note that "localhost" is the server's IP address. |
If everything goes well, the server will accept the connection. In Java, the ServerSocket
uses the accept
method to accept a connection request from a client. Then it creates a dedicated ordinary Socket
for talking to this client:
1 | Socket newClientSocket = serverSocket.accept(); |
After the connection, the server and the client can communicate via socket
(owned by the client) and newClientSocket
(owned by the server).
Communication via Socket
In Java,java.net.Socket
has the following methods:
1 | Socket(String hostname, int port) // a constrcutor that creates a new Socket to talk to hostname at port |
And java.net.ServerSocket
has the following methods:
1 | ServerSocket(int port) // a constructor that creates a new ServerSocket that listens at port |
Therefore, in the view of a client, the communication via socket would like this:
- Create a Socket to talk to a server.
- Use the Socket’s OutputStream to send messages to the server.
- Use the Socket’s InputStream to receive messages from the server.
- Keep doing this(sending&receiving message) as long as the protocol requires.
- Close the connection.
In the view of a single-thread server, the communication would like this:
- Create a ServerSocket on a port.
- Accept connection request from a client, creating a dedicated Socket.
- Use the dedicated Socket’s InputStream to receive messages.
- Use the Socket’s OutputStream to send messages.
- Keep doing this as long as the protocol requires.
- Close the dedicated Socket.
- Go back to receive connection requests.
You might have noticed that a single-thread server can only service one client at a time. For this reason, we normally use multi-threaded server, which creates a separate thread to service each client. And a multi-thread server can be available at all times.
Network Input/Output
For network I/O, we can use DataInputStream/DataOutputStream
and ObjectInputStream/ObjectOutputStream
.
DataInputStream/DataOutputStream
are sequences of basic data values, such as integers, floats, characters, etc. We can use them to send/recieve data like this:
1 | Socket socket = new Socket("localhost", 9527); |
ObjectInputStream/ObjectOutputStream
are sequences of objects, and these objects are so-called serialized objects
, which are represented as sequences of bytes. Suppose we have a class Person
which implements the interface Serializable
:
1 | public class People implements Serializable {...} |
We can use ObjectInputStream/ObjectOutputStream
to send/recieve Person
object:
1 | Person person = new Person(); |
Simple Server&Client Demo
In Java, a simple server is like this:
1 | import java.io.IOException; |
And a simple client is like this:
1 | import java.io.IOException; |
We can use terminal to run the above server and client seperately, and use them to communicate with each other: