Socket is often referred to as the so-called “socket” used to describe the IP address and port, is a communications chain handle. Applications are usually through the “socket” to the network response to a request or the request of the network. Socket and ServerSocket class library is located in the java.net package. ServerSocket for server-side, Socket is to establish a network connection used. The connection is successful, the application will have two ends of a Socket instance, the operation of this example, the completion of the necessary conversation. For a network connection, the socket is equal, and there is no difference, not because the server side or on the client arising from different levels. Socket or ServerSocket whether their work is through the class and its subclasses SocketImpl completed.
The following is one of the most simple example of Socket Communications for beginners reference:
Server-side: ServerDemo.java
package com.lanber.socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo (
/ **
* Note: Socket is the need to send and receive simultaneously, that is, the client sends a message, the server must first receive this information,
* And before they can send a message to the client, otherwise there will be a run-time error.
* @ Param args
* /
public static void main (String [] args) (
ServerSocket ss = null;
try (
ss = new ServerSocket (8888);
/ / Server to the client receiving the data, create a dialogue with the Socket Client
Socket socket = ss.accept ();
/ / For sending data to the client’s output stream
DataOutputStream dos = new DataOutputStream (socket.getOutputStream ());
/ / For the client to receive data sent by the input stream
DataInputStream dis = new DataInputStream (socket.getInputStream ());
System.out.println ( “server to the client to receive connection requests:” + dis.readUTF ());
/ / Server to the client to send information to connect successfully identified
dos.writeUTF ( “accept connection requests, connection successful!”);
/ / Do not need to continue to use this connection, close connection
socket.close ();
ss.close ();
) Catch (IOException e) (
e.printStackTrace ();
)
)
)
Client: ClientDemo.java
package com.lanber.socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class ClientDemo (
/ **
* @ Param args
* /
public static void main (String [] args) (
Socket socket = null;
try (
socket = new Socket ( “localhost”, 8888);
/ / Obtain output stream for the client to send data to the server-side
DataOutputStream dos = new DataOutputStream (socket.getOutputStream ());
/ / Obtain the input stream, the server side for receiving the data sent
DataInputStream dis = new DataInputStream (socket.getInputStream ());
/ / Client sends data to the server-side
dos.writeUTF ( “I am a client, a request to connect!”);
/ / Print received from the server data termination
System.out.println (dis.readUTF ());
/ / Do not need to continue to use this connection, I recall that the closure of O
socket.close ();
) Catch (UnknownHostException e) (
e.printStackTrace ();
) Catch (IOException e) (
e.printStackTrace ();
)
)
)




No user commented in " One of the most simple example of Socket Communications "
Follow-up comment rss or Leave a TrackbackLeave A Reply