-------Android Training, Java training, look forward to communicating with you! ----------
TCP and UDP differences
UDP protocol: 1, data, source address, destination address are encapsulated into a packet, no need to establish a connection. 2, each packet limit size 64K
3, not rely on the agreement, fast speed
TCP protocol: 1, first establish a connection to form a data path. 2, after the connection established, carries on the massive data transmission, 3 reliable protocol, the speed is slightly slow
Socket
sockets are a mechanism that is provided for network services. Network traffic corresponds to the communication between sockets.
The socket must be established before communication can be communicated, the communication must be known, IP address, port number
UDP Transport in Java:
UDP socket set up with Datagramsocket class
Datagrampocket class for the establishment of UDP packets
UDP Transmission Code Ideas :
1. Two objects required: Datagramsocket and Datagrampacket
2, set up the sending side, the receiving end
3. Set up the data package
4, call the socket send, receive method
5. Close the socket.
Send side:
On the sending side, specify the destination IP and port number in the object in the packet
Receiving end:
The receiving end should explicitly specify the port to listen on
code example:
Import java.net.*;
/*
Requirement: Send a piece of text data through the UDP transmission mode. Defines a UDP send side.
Ideas:
1, establish Updsocket service.
2, provide data, and encapsulate the data in a packet.
3, through the socket service send function, the packet is sent out.
4, close the resource.
*/
Class udpsend{
public static void Main (string[] args) throws exception{
1, create the UDP service. Through the Datagramsocket object.
Datagramsocket ds = new Datagramsocket (8888);
2. Identify the data and encapsulate it into a packet. Datagrampacket (byte[] buf, int length, inetaddress address, int port)
byte[] buf = "udp ge men lai le". GetBytes ();
Datagrampacket DP =
New Datagrampacket (Buf,buf.length,inetaddress.getbyname ("192.168.1.254"), 10000);
3, through the socket service, the existing data packets sent out. Through the Send method.
Ds.send (DP);
4, close the resource.
Ds.close ();
}
}
/*
Demand:
Defines an application that receives and processes data transmitted by the UDP protocol.
Defines the receive side of UDP.
Ideas:
1, define the Udpsocket service. A port is typically monitored. is to define a digital ID for this receiving network application. It is easy to define what data comes up to the application to handle.
2, define a packet, because the received byte data is to be stored. Because there are more features in the packet object, you can extract different data information from the byte data.
3, the received data is stored in a defined packet via the Receive method of the socket service.
4, through the unique features of the packet object. Take these different data out. Print on the console.
5, close the resource.
*/
Class udprece{
public static void Main (string[] args) throws exception{
1, create the UDP socket and set up the endpoint.
Datagramsocket ds = new Datagramsocket (10000);
while (true) {
2. Define the packet. Used to store data.
byte[] buf = new byte[1024];
Datagrampacket DP = new Datagrampacket (buf,buf.length);
3, the received data is stored in the packet via the Receive method of the service.
Ds.receive (DP);//Blocking method.
4, the data is obtained through the method of the packet.
String IP = dp.getaddress (). gethostaddress ();
String data = new String (Dp.getdata (), 0,dp.getlength ());
int port = Dp.getport ();
System.out.println (ip+ "::" +data+ "::" +port);
}
5, close the resource, (optional)
Ds.close ();
}
}
Transmission of TCP
because it is connection-oriented and is divided into server and client
the server uses the ServerSocket class, the client uses the socket class to establish the socket object
After the establishment through the IO stream to transmit, facing the connection, establishes after directly transmits.
TCP Transport Communication ideas :
1. Required objects: Sockets and ServerSocket
2. Establish client and server side
3, after establishing the connection, through the IO stream in the socket for data transmission
4. Close socket
Client basic idea:
The client needs to specify the IP address and port of the server so that it can try to establish a connection, and if the connection fails, an exception occurs.
The connection is successful, which means that the client and the server have established a channel, so the data can be transmitted through IO stream.
The socket already provides the output stream and the input stream object, which can be obtained through the getInputStream () and the Getoutputstream () method.
When the data transfer is complete with the server, the socket can be closed.
Service-side Basic ideas:
The server needs to be clear on which port it is going to process the data.
When there is a client access, it needs to be clear which client is. The client object to access is obtained through the Accept () method, and the data is transmitted through the IO stream through the object and the client
When the client access is complete, close the client.
TCP code example: Requirement: Sends a text data to the server.
Client
Steps:
1, create the socket service. and specify the host and port to connect to.
Import java.io.*;
Import java.net.*;
Class tcpclient{
public static void Main (string[] args) throws Exception {
Create the client socket service. Specify destination host and port
Socket s = new socket ("192.168.1.254", 10003);
In order to send data, you should get the output stream in the socket stream.
OutputStream out = S.getoutputstream ();
Out.write ("tcp ge Men lai le". GetBytes ());
S.close ();
}
}
Requirements: Define the endpoint to receive data and print it on the console.
Service side:
1, set up the socket service on the server. ServerSocket (), and listen for a port.
2. Gets the client object that is connected. Through Serversokcet's Accept method. No connection will wait, so this method is blocking.
3, if the client sends the data, then the server will use the corresponding client object and fetch the read stream to the client object to read the data sent. and print it in the console.
4, close the server. (optional)
Class tcpserver{
public static void Main (string[] args) throws exception{
Set up a service-side socket service. And listen for a port. </span>
ServerSocket ss = new ServerSocket (10003);
The Accept method gets the client object that is connected. </span>
while (true) {
Socket s = ss.accept ();
String IP = s.getinetaddress (). gethostaddress ();
System.out.println (ip+ "... connected");
To get the data sent by the client, the read stream of the client object is used to read the data.
InputStream in = S.getinputstream ();
byte[] buf = new byte[1024];
int len = In.read (BUF);
System.out.println (New String (Buf,0,len));
S.close ();//Shut down the client.
}
}
This article is from the "Dot Drop" blog, please be sure to keep this source http://arctictern.blog.51cto.com/10120640/1660137
Dark Horse Programmer----Java Basics: Network programming