A, UDP / TCP differences:
( 1 ) Connect: to establish a connection, data transmission channel is formed;
( 2 ) large data transfers: a large amount of data transmission connection;
( 3 ) Reliable: by three-way handshake to complete the connection, a reliable coordination;
( 4 ) slow: must establish a connection, the efficiency will be lower;
2, UDP no connection:
( 1 ) No connection: the data encapsulated into packets and the source and destination, without establishing a connection;
( 2 ) small data transmission: the size of each packet within the 64K limit;
( 3 ) not reliable: because there is no connection, the protocol is not reliable;
( 4 ) speed: without establishing a connection speed;
二、UDP/DatagramSocket/DatagramPacket:
1, the transmission train of thought:
1) Send:
(1) establish udpsocket services;
(2) providing data, the data encapsulated in the packet;
(3) send by the sending socket and services of the data packets sent out;
(4) close the resource;
2) receiving:
(1) establish udpsocket service, listening on a port ;
(2) define the data packet, the received data bytes for storage;
(3) by receiving the socket receive functions and services, it is stored defined data packet;
(4) the data packet unique features, to retrieve data;
(5) close the resource;
2, DatagramSocket class: used to create udpsocket services;
( 1 ) Constructor:
1)DatagramSocket();
2) DatagramSocket (int port); specify a port number;
( 2 ) Method:
. 1 ) void Send (of DatagramPacket P); transmitting a data packet;
2 ) void the receive (of DatagramPacket P); receiving a data packet; // This method blocks until receiving the data packet;
. 3 ) void Close (); Close resources;
. 3, of DatagramPacket categories: used to create the data packet;
( 1 ) Constructor:
1 ) send packets: of DatagramPacket (byte [] buf, int length, InetAddress address, int Port);
// Parameters: buf data; longitudinal length; address addresses; port port;
2 ) receiving a data packet: of DatagramPacket (byte [] buf, int length);
( 2 ) Method:
. 1 ) InetAddress getAddress (); return the IP address;
2) byte [] getData (); return packet; // 1024 is created, it is full no later default value;
3) int getLength (); return data length is received, the array is not;
. 4 ) int the getPort (); return port number;
4, an example:
1) the sending end:
public class the Send { public static void main (String [] args) throws Exception { // . 1, service establishment udpsocket DatagramSocket Client = new new DatagramSocket ( ); // 2, to provide data, and data encapsulated into packets String msg = "Hello" ; byte [] = data Msg.getBytes (); of DatagramPacket packet = new new of DatagramPacket (data, data.length, new new the InetSocketAddress ( "127.0.0.1", 8848 )); // . 3, the transmission packet client.send (Packet); //4. Close the resource client.close (); } }
2) the receiving end:
public class the Receive { public static void main (String [] args) throws Exception { // . 1, udpsocket establish service, a listening port; DatagramSocket Server = new new DatagramSocket (8848 ); // 2, defined packet, for storing received to data bytes; byte [] = Container new new byte [1024 ]; of DatagramPacket packet = new new of DatagramPacket (Container, container.length); // . 3, functions by receiving the socket receive services, stored defined data packet in; server.receive (Packet); // . 4, data analysis InetAddress address = packet.getAddress(); byte[] data = packet.getData(); int length = packet.getLength(); int port = packet.getPort(); System.out.println(address); System.out.println(new String(data, 0, length)); System.out.println(length); System.out.println(port); // 5、关闭资源 server.close(); } }
三、TCP/Socket/ServerSocket:
1, the transmission train of thought:
1) Client :
( 1 ) establish a socket service, and specify the host and port to connect to;
( 2 ) obtaining stream data is written;
( 3 ) close the resource;
2) server :
( 1 ) establish socket services, and a monitor port; // ServerSocket ;
( 2 ) pass over the acquired customer object; accpet method;
( 3 ) the object acquired by the socket process to process the data into the stream;
( 4 ) close the resource;
2, the Socket client socket:
1) construction method:
( . 1 ) the Socket (InetAddress address, int Port); Create a socket connected to the specified IP and port;
(2)Socket(String host, int port);
2) core methods:
(1)void close();
( 2 ) the InputStream the getInputStream (); Return jack input stream;
( . 3 ) the OutputStream the getOutputStream (); Return jack output stream;
3, ServerSocket server socket:
1) constructor: the ServerSocket (int Port); bind-specific server socket port;
2) core methods:
( 1 ) the Socket the Accept (); listen for connections; // This method blocks until an incoming connection;
(2)void close();
5, example :
1) server:
public class TcpService { public static void main (String [] args) throws Exception { // . 1, establishing the socket service, and a monitor port; // the ServerSocket; the ServerSocket serverSocket = new new the ServerSocket (10086 ); // 2, the acquired customer pass over the object; accpet method; the socket socket = serverSocket.accept (); System.out.println (socket.getInetAddress () getHostAddress ().); // . 3, the method of obtaining the objects into the socket stream data processing; the InputStream in = Socket.getInputStream (); byte [] = data new new byte[1024 ]; int len = in.read (Data); System.out.println ( new new String (Data, 0 , len)); // . 5, the data returns the OutputStream OUT = Socket.getOutputStream (); out.write ( "received thank" .getBytes ()); // . 4, close the resource; the Socket.close (); serverSocket.close (); } }
2) Client:
public class the TcpClient { public static void main (String [] args) throws Exception { // . 1, establishing the socket services, and specified host and port to be connected; the Socket socket = new new the Socket ( "127.0.0.1", 10086 ); / / 2, the acquisition stream, and write data; the OutputStream OUT = Socket.getOutputStream (); out.write ( "server Hello!" .getBytes ()); // . 3, the acquisition stream, the read data; the InputStream in = Socket.getInputStream (); byte [] = Data new new byte [1024 ]; int= len in.read (Data); System.out.println ( new new String (Data, 0 , len)); // . 4, close the resource the Socket.close (); } }