We use software simulation on the server side, which is a very small software. Download the software NetAssist: http://xiazai.VeVB.COm/201403/tools/NetAssist(VeVB.COm).rar
The second step is to paste our client code:
Copy the code code as follows:
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
public class Client {
public static final String IP_ADDR = "192.168.3.65";//Server address
public static final int PORT = 8080; //Server port number
static String text = null;
public static void main(String[] args) throws IOException {
System.out.println("Client starts...");
Socket socket = null;
socket = new Socket(IP_ADDR, PORT);
while (true) {
try {
//Create a stream socket and connect it to the specified port number on the specified host
//Read server side data
DataInputStream input = new DataInputStream(socket.getInputStream());
byte[] buffer;
buffer = new byte[input.available()];
if(buffer.length != 0){
System.out.println("length="+buffer.length);
//Read buffer
input.read(buffer);
//Convert string
String three = new String(buffer);
System.out.println("content=" + three);
}
} catch (Exception e) {
System.out.println("Client exception:" + e.getMessage());
}
}
}
}
You can see the print results as follows:
length represents the length of the message
Copy the code code as follows:
Client starts...
length=27
Content=//www.VeVB.COm
Okay, we have completed a socket client. This can always receive messages from the server.