Java Socket Detailed Introduction _java

Source: Internet
Author: User

Returns the local host name and IP address;
InetAddress i = Inetaddress.getlocalhost ();
I.gethostaddress ();//ip
I.getaddress ()//???

Acquiring computer related information through computer name;
InetAddress i = inetaddress.getbyname ("livingstone-pc");

Get host-related information by domain name
InetAddress Ibaidu = Inetaddress.getbyname ("www.baidu.com");

URL url = new URL ("http://localhost:8080/demo.html");
Url.gethost ();

Tcp:

Socket (client):
When an object is established, it is possible to connect to the specified host, because TCP is connection-oriented, so when the socket service is established, there must be a server presence and the connection is successful and connected
After the data transmission in the channel;
Create the socket service for the client, specifying the 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.getoutputstream ();
PrintWriter out = new PrintWriter (S.getoutputstream (), true); Out.println ("Hello");
Out.write ("Hello". GetBytes ());

Take back Send Message
InputStream in = S.getinputstream ();
byte[] Bufin = new byte[1024];
int num = In.read (bufin);
System.out.println (New String (bufin, 0, num));

S.close ();//Stream object in encapsulated in socket, automatically close stream object;

ServerSocket (service side):
Set up the socket service on the service side, ServerSocket and monitor a port;
Gets the client object that is connected, through the object's Accept method, waits (blocking) without a connection:
If the client sends data, the server will use the corresponding client object's read stream to obtain the data from the client;
ServerSocket ss = new ServerSocket (10003);

ServerSocket (int port,int backlog); Backlog is the maximum number of connections;
Socket s = ss.accept ();
InputStream in = S.getinputstream ();

byte[] buf = new byte[2014];
int len = In.read (BUF);

String rec = new String (buf, 0, Len);
SYSTEM.OUT.PRINTLN (REC);

Loopback message
OutputStream out = S.getoutputstream ();
Out.write ("Received". GetBytes ());

S.close ()//server will automatically shut down the client;
(1) Client:
Establish a socket service that specifies the host and port to be connected;
Gets the output stream in the socket stream, writes the data to the stream, and sends it to the server via the network;
Gets the input stream in the socket stream, obtains the data of the server feedback and closes the client resource;

Udp:

Datagramsocket: Send a piece of text data through UDP transmission mode;
Send:
Create a UDP service, create a Datagramsocket object and give a commodity number;
Datagramsocket socket = new Datagramsocket (8888);

Determine the data and encapsulate it into a packet, Datagrampacket (Specify the port number of the destination machine);
byte[] buf = "udp I ' m coming". GetBytes ();
Datagrampacket DP = new Datagrampacket (buf, Buf.length,
Inetaddress.getbyname ("Machine-name"), 10086);
Send
Socket.send (DP);
Close
Socket.close ();
Receive:
Define the Udpsocket service. Usually listens to a port, in fact, to the receiving network application to define the digital ID;
Datagramsocket socket = new Datagramsocket (10086);
while (true) {//purpose is to continuously monitor
byte[] buf = new byte[1024];
Defines a packet for storing data;
Datagrampacket DP = new Datagrampacket (buf, buf.length);
The received data is stored in the packet through the receive method of the service;
Socket.receive (DP),//blocking method, no data on the card die here;
Obtaining the data by means of 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.getoutputstream ();
FileInputStream fis = new FileInputStream ("awf.jpg");
byte[] buf = new byte[1024];
int len = 0;
while (len = Fis.read (buf))!=-1) {
Out.write (buf, 0, Len);
}
Stop sending data
S.shutdownoutput ();

ServerSocket ss = new ServerSocket (10005);//ss.accept () method has blocking effect;
A separate thread can be established for each socket obtained by the Accept () method;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.