The network application is divided into two parts: the client and the service side, while the socket class is the Java class responsible for handling client communication. This class enables you to connect to a server with a specified IP or domain name, and can send and receive data to and from the server.
For the socket communication brief, the service end to the socket output stream inside write something, the client can through the socket input stream to read the corresponding content. Socket and socket is two-way connectivity, so the client can also to the corresponding socket output stream inside write things, and then the corresponding socket of the service side of the input stream can read the corresponding content.
Example 1: Shorthand for Client (a).
Socket client = null;
try{
client = new Socket (ip,port);
String msg= "The content of the data sent!" ";
Gets the socket read-write stream and sends the data
Client.getoutputstream () to the server program. Write (Msg.getbytes ());
byte[] datas = new byte[2048];
Receive data from a server-side program
Client.getinputstream (). read (datas);
System.out.println (New String (datas));
catch (Exception e) {
e.printstacktrace ();
} Finally {
if (client!= null) {
try {
client.close ();
} catch (IOException e) {
System.out.println ("Systemerr:" +e);}}}
Example 2: Client Shorthand (ii).
try{
client = new Socket ();
SocketAddress socketaddress = new inetsocketaddress (ip,port);
Client.connect (socketaddress, 3000);
String msg= "Accessed server returns content!" ";
Gets the socket read-write stream and sends the data
Client.getoutputstream () to the server program. Write (Msg.getbytes ());
byte[] datas = new byte[2048];
Receive data from a server-side program
Client.getinputstream (). read (datas);
System.out.println (New String (datas));
catch (Exception e) {
e.printstacktrace ();
} Finally {
if (client!= null) {
try {
client.close ();
} catch (IOException e) {
System.out.println ("Systemerr:" +e);}}}
Example 3: The complete writing of the client.
try {
//1. Establish a client socket connection, specify the server location and port
socket sockets =new socket (ip,port);
2. Get socket read-write stream
outputstream os=socket.getoutputstream ();
PrintWriter pw=new printwriter (OS);
Input stream
InputStream is=socket.getinputstream ();
BufferedReader br=new BufferedReader (New InputStreamReader (IS));
3. Use the flow according to certain operation, the socket for read
and write operation String sendinfo= "Send data information to the server!" ";
Pw.write (sendinfo);
Pw.flush ();
Socket.shutdownoutput ();
The corresponding String replyinfo=null of the receiving server
;
while (!) ( (Replyinfo=br.readline ()) ==null)) {
System.out.println ("Data information for the receiving server:" +replyinfo);
}
4. Close Resource
br.close ();
Is.close ();
Pw.close ();
Os.close ();
Socket.close ();
} catch (Unknownhostexception e) {
e.printstacktrace ();
} catch (IOException e) {
e.printstacktrace ();
}
About Java Socket Communication (i) of the client program to send and receive data related knowledge, small series to introduce to you here, more information please visit the Cloud Habitat Community website to learn more!