Java socket details

  • 2020-04-01 01:44:39
  • OfStack

// return local host name and IP address;
InetAddress I = InetAddress. GetLocalHost ();
I.g etHostAddress (); / / IP
I.g etAddress (); / /???

// get computer information by computer name;
InetAddress I = InetAddress. GetByName (" Livingstone - PC ");

// get host information by domain name
InetAddress ibaidu = InetAddress. GetByName (" ");

URL the URL = new URL (" http://localhost:8080/demo.html ");
Url. The getHost ();


When the object is established, you can connect to the specified host, because TCP is connection-oriented, so in the establishment of socket service, there must be a server exists, and the successful connection, connection
After that, carry on the data transmission in this channel;
// create client socket service, specify target host and port;
Socket s = new Socket(" cj-pc ", 10003);
// in order to send data, the output stream in the socket stream should be obtained;
OutputStream out = s.g etOutputStream ();
//PrintWriter out = new PrintWriter(s.prinoutputstream (),true); Out.println (" hello ");
Out. Write (" hello ". GetBytes ());

// receive the echo message
InputStream in = s.g etInputStream ();
Byte [] bufIn = new byte[1024];
Int num = in. Read (bufIn);
System. The out. Println (new String (bufIn, 0, num));

S.c lose (); // stream object in encapsulated in socket, automatically close the stream object;

Set up server-side socket service, ServerSocket, and listen to a port;
Gets the connected client object. If there is no connection, it will wait through the accept method of the object.
If the client sends the data, the server USES the reading stream of the corresponding client object to obtain the data sent by the client.
ServerSocket ss = new ServerSocket(10003);

/ / ServerSocket (int port, int backlog); Backlog is the maximum number of connections;
The Socket s = ss. The accept ();
InputStream in = s.g etInputStream ();

Byte [] buf = new byte[2014];
Int len = in. Read (buf);

String rec = new String(buf, 0, len);
System. The out. Println (rec);

// echo message
OutputStream out = s.g etOutputStream ();
Out. Write (" received ". GetBytes ());

S.c lose (); // the server will automatically close the client;
(1) client:
Set up socket service, specify to connect the host and port;
Get the output stream in the socket stream, write the data into the stream, and send it to the server through the network.
Obtain the input stream in the socket stream, obtain the data feedback from the server side, and close the client side resources;


DatagramSocket: send a segment of text data by UDP;
// create udp service, create a DatagramSocket object and give a commodity number;
DatagramSocket socket = new DatagramSocket(8888);

// determine the data and package it into a packet, DatagramPacket(port number of the destination machine to be specified);
Byte [] buf = "udp I'm coming". GetBytes ();
DatagramPacket dp = new DatagramPacket(buf, buf. Length,
InetAddress. GetByName (" the Machine - the Name "), 10086);
/ / send;
Socket. The send (dp);
/ / close;
Socket. The close ();
The Receive:
// define the udpsocket service. Typically, a port is listened on, essentially defining a digital identity for the receiving network application.
DatagramSocket socket = new DatagramSocket(10086);
While (true) {// the purpose is to keep listening
Byte [] buf = new byte[1024];
// define packets for storing data;
DatagramPacket dp = new DatagramPacket(buf, buf. Length);
// the received data is stored in packets through the receive method of the service;
Socket. The receive (dp); // blocking method, no data stuck here;
// get the data in a packet;
String IP = dp. GetAddress (). GetHostAddress ();
String data = new String(dp.getdata (), 0, dp.getlength ());
Int port = dp. GetPort ();

File upload:

Socket s = new Socket(" machine-name ", 10005);
OutputStream out = s.g etOutputStream ();
FileInputStream fis = new FileInputStream("awf.jpg");
Byte [] buf = new byte[1024];
Int len = 0;
While ((len =! = 1) {
Out. Write (buf, 0, len);
// stop sending data
S.s hutdownOutput ();

ServerSocket ss = new ServerSocket(10005); //ss.accept() method is blocking;
// a separate thread can be created for each Socket obtained by the accept() method.

Related articles: