This article records the pits encountered using socket communication and is constantly updated.
Three issues currently recorded: establishing a socket connection using input and output streams to communicate using a heartbeat to maintain the socket connection to establish a socket connection
The socket connection requires a serversocket to act as a server, and many sockets are used as clients to connect ServerSocket. In Eclipse, the code is as follows:
Server side:
Socket socket;
System.out.println ("Wait for a client.");
Socket = Serversocket.accept ();
System.out.println ("accept a socket.");
writer = new ObjectOutputStream (Socket.getoutputstream ());
System.out.println ("Get writer.");
ObjectInputStream is waiting for a header, we'll be stuck until we completely receive it,
//we should call Flush () On the other side.
reader = new ObjectInputStream (Socket.getinputstream ());
System.out.println ("Get reader.");
Socket = Serversocket.accept (); This sentence is a blocking statement, blocking means that when the socket is not used to connect, the program will always stay in this sentence, know that a socket to connect, the statement can continue to execute.
On the client:
public class Socketclient extends Thread {
private Scanner Scanner;
private socket socket;
Private ObjectInputStream reader;
Private ObjectOutputStream writer;
Public Socketclient () throws ioexception{
scanner = new Scanner (system.in);
Socket = new Socket ("10.30.131.230", 5555);
SYSTEM.OUT.PRINTLN ("A client has start.");
reader = new ObjectInputStream (Socket.getinputstream ());
writer = new ObjectOutputStream (Socket.getoutputstream ());
Writer.flush ();
System.out.println ("reader writer.");
The client only needs to declare a socket object, and the IP and port of the incoming server will be available, and the connection will be established. using input and output streams for communication
The server communicates with the client, requiring the serversocket and socket to communicate using the same stream. Java API has been encapsulated, we need to simply invoke the method can:
First, the client socket gets its own input stream and output stream:
public class Socketclient extends Thread {
private Scanner Scanner;
private socket socket;
Private ObjectInputStream reader;
Private ObjectOutputStream writer;
Public Socketclient () throws ioexception{
scanner = new Scanner (system.in);
Socket = new Socket ("10.30.131.230", 5555);
SYSTEM.OUT.PRINTLN ("A client has start.");
reader = new ObjectInputStream (Socket.getinputstream ());
writer = new ObjectOutputStream (Socket.getoutputstream ());
Writer.flush ();
System.out.println ("reader writer.");
Code the same, here we see through
Socket.getinputstream ();
Socket.getoutputstream ();
You can get the client socket's own input and output stream, and encapsulation with Objectstream, then the client can use the reader and writer objects here, directly and ServerSocket use the custom object to communicate. Of course, custom objects for communication need to be serialized.
The code at the server side is as follows:
Message message = NULL;
Socket socket;
System.out.println ("Wait for a client.");
Socket = Serversocket.accept ();
System.out.println ("accept a socket.");
writer = new ObjectOutputStream (Socket.getoutputstream ());
System.out.println ("Get writer.");
ObjectInputStream is waiting for a header, we'll be stuck until we completely receive it,
//we should call Flush () On the other side.
reader = new ObjectInputStream (Socket.getinputstream ());
System.out.println ("Get reader.");
We see the service side is also through the first socket, and then call the socket method to get the input and output stream, but here we also note that on the client, we should call the flush () method on the resulting output stream , because when the connection is first established, The corresponding input stream on the service side will wait for a header until it is fully received, and flush () will write all the contents of the cache. use heartbeat to keep the socket connected
If you set up a socket connection, you can also communicate, do not be happy too early, if a period of time does not communicate, the socket will automatically disconnect, and the solution is to send a regular heartbeat, is to send a message to the server, to stay connected.
The usual approach is to write a class, inherit thread, to send a heartbeat, and set a maximum wait time, if the maximum wait time, not to the server to send a message or accept the message to the server, then send a heartbeat to the service. Of course, if you get feedback during this period, reset the wait time.