Mastering Android Web Development-using sockets for data communication

Source: Internet
Author: User

NO1:

Network transport applications typically use three protocols, TCP, IP, or UDP, for data transfer. In the process of transmitting data, it is necessary to realize the interaction of data through a two-way communication connection. In this transfer process, the end of this bidirectional link is usually called the socket, and a socket is usually determined by an IP address and a port number.

No2:

There are two main problems in network programming, one is how to accurately locate one or more hosts on the network, and the other is how to reliably and efficiently transmit data after locating the host.

In the TCP/IP protocol, it is mainly responsible for the location of the network host, the routing of data transmission, and the IP address can uniquely determine a host on the Internet. The TCP layer provides an application-oriented, reliable (TCP) or unreliable (UDP) data transfer mechanism.

No3:

The basic steps of the socket work process:

1) Create socket

2) Open the input/output stream connected to the socket

3) Read/write the socket according to a certain protocol

4) Close Socket

No4:

The chat room program contains multiple threads on the server, each of which corresponds to a thread that reads the data from the socket corresponding to the input stream (the data sent from the client) and sends the read data to each socket output stream once ("broadcast" the data sent by the moth client to other clients), so you need to use list on the server side to save all sockets.

 Public classiserver{//define the ArrayList that holds all sockets     Public StaticArraylist<socket> socketlist =NewArraylist<socket>();  Public Static voidMain (string[] args)throwsioexception{ServerSocket SS=NewServerSocket (3000);  while(true){            //This line of code will block and will wait for someone else's connectionSocket s =ss.accept ();            Socketlist.add (s); //each time a client connects, a Serverthread thread is started to service the client            NewThread (NewServerxian (s)). Satart (); }    }}
//thread class responsible for handling each thread's communication Public classServerxianImplementsrunnable{//defines the socket that the current thread handlesSocket s =NULL; //The input stream corresponding to the socket that the thread is processingBufferedReader br =NULL;  PublicServerxian (Socket s)throwsioexception{ This. S =s; //initialize the input stream corresponding to the socketBR =NewBufferedReader (NewInputStreamReader (S.getinputstream ())); }         Public voidrun () {Try{String content=NULL; //using loops to continuously read the data sent by the client from the socket .             while(content = Readfromclient ())! =NULL){                //traverse each socket in the Socketlist//send read content to each socket once                 for(Socket s:iserver.socketlist) {printstream PS=NewPrintStream (S.getoutputstream ());                PS.PRINTLN (content); }            }        }Catch(IOException e) {//e.printstacktrace ();        }    }    //define methods for reading client data    PrivateString readfromclient () {Try{            returnBr.readline (); }Catch(IOException e) {//If an exception is caught, the client that corresponds to the socket is closed//Delete the socketIServer.socketList.remove (s); }        return NULL; }}

Mastering Android Web Development-using sockets for data communication

Related Article

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.