Android Network Programming (i)---socket basics

Source: Internet
Author: User

Sockets are often called "" sockets. The socket literal means "socket" in Chinese. A server may provide a number of services, each of which corresponds to a socket, and the customer's "plug" is also a socket. A socket is an intermediate software abstraction layer that the application layer communicates with the TCP/IP protocol family, which is a set of interfaces. Socket to the complex TCP/IP protocol family hidden behind the socket interface, for the user, a simple set of interfaces is all, let the socket to organize data to meet the specified protocol. The socket is used to describe the IP address and port, which is a handle to a communication chain. Applications typically make requests to the network via sockets or answer network requests. The basic operation of the socket includes: (1) Connecting the remote machine (2) sending data (3) receiving data (4) Closing the connection (5) binding port (6) Listening for arrival data (7) Accepting connections from the remote machine on the bound port.

The server communicates with the client, both of which instantiate a socket. The server and client socket is not the same, the client can be connected to the remote machine, send data, receive data, close the connection, etc., the server also need to implement the binding port, listen to the data reached, accept the connection from the remote machine. Android provides two classes in the package java.net: ServerSocket and sockets, which are used to instantiate the server socket, which is used to instantiate the client socket. When the connection succeeds, a socket instance is generated at both ends of the application, manipulating the instance to complete the session required by the client to the server.

So how to construct a socket????

(1) Common methods of constructing client sockets are as follows:
Socket (): Creates a new, disconnected socket.
Socket: Creates a new, disconnected socket with the specified proxy type.

Socket (String dstname,int dstport): Creates a new socket using the IP address of the specified destination server and the port number of the destination server.

Socket (String dstname,int dstport,inetaddress localaddress,int localport): Creates a new socket using the specified target host, destination port, local address, and local port.

Socket (inetaddress dstaddress,int dstport): Creates a new socket using the specified local address and local port.

Socket (inetaddress dstaddress,int dstport,inetaddress localaddress,int localport): Using the specified target host, destination port, local address, and local port, Create a new socket.

Where proxy is the server address, dstaddress is the destination server IP address, dstport is the port number of the target server (each server service is bound to one port), DstName is the host name of the target server.

Note: The 0~1023 port is reserved for the system, and the user-defined port number should be greater than 1023.

Socket class Important methods:
(1) Public InputStream getInputStream () read out the data in the socket

(2) Public outputstream Getoutputstream () writes data to the socket

(3) public synchronized void Close () Close the socket

(2) There are several methods used to construct server-side serversocket:
ServerSocket (): Constructs a new unbound serversocket

ServerSocket (int port): Constructs a new serversocket instance and binds to the specified port. If the port parameter is 0, the ports will be automatically assigned by the operating system, at which point the number of incoming queues will be set to 50.

ServerSocket (int port,int backlog): Constructs a new serversocket instance and binds to the specified port and specifies the number of incoming queues. If the port parameter is 0, the ports will be assigned by the operating system

ServerSocket (int port,int backlog,inetaddress localaddress): Constructs a new serversocket instance and binds to the specified port and the specified address.                                                                                                                              If the localaddress parameter is NULL, you can make the With any address, if the port parameter is 0, the ports will be automatically assigned by the operating system.

There are generally two types of sockets: TCP Sockets and UDP sockets.

TCP and UDP are implemented in different ways in the transmission process. Both receive the transport protocol packet and forward its contents to the application layer. TCP breaks down messages into packets and assembles them in the correct order at the receiving end, and TCP also handles retransmission requests for lost packets, and the application layer on the upper level has fewer things to deal with. UDP does not provide the ability to assemble and retransmit requests, it simply forwards packets. The application layer on the upper level must ensure that the messages are complete and assembled in the correct order.

Reprint Please specify source: http://blog.csdn.net/android_jiangjun/article/details/27670355

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.