The basics of socket programming in Android

Source: Internet
Author: User
Tags socket port number

Socket Programming Basics:

Primary server-side programming and client programming.

Server-side programming steps:

1: Create server-side sockets and bind to a port (0-1023 is System reserved, preferably about 1024)

2: Socket set listening mode waiting for connection request

3: Communication After accepting the connection request

4: Return, wait to win a connection request

Client Programming Steps:

1: Create a client socket (specify server-side IP address and port number)

2: Connect (automatically connect when Android creates socket)

3: Communication with the server side

4: Close Socket

Android Socket Communication Principle Note:

1: The middle pipe connection is achieved through Inputstream/outputstream flow.

2: Once the pipeline is established to communicate

3: Closing the pipe means closing the socket

4: When duplicate pipes are created on the same socket, they are abnormal

5: The order is important in the communication process: The server side first gets the input stream and then outputs the input stream information to its individual clients

After the client establishes the connection, it writes the output stream before obtaining the input stream. Otherwise, there are eofexception anomalies.

Don't forget to add Internet permissions, don't forget to add Internet permissions, and don't forget to add Internet permissions

The writing of server-side code:

Import Java.io.DataInputStream;  
Import Java.io.DataOutputStream;  
Import java.io.IOException;  
Import Java.net.ServerSocket;  
Import Java.net.Socket;  
      
Import java.util.ArrayList;  
public class Server {static ServerSocket aserversocket = NULL;//server Socet. DataInputStream adatainput = null;  
Server input Stream that to//Receive MSG from client. DataOutputStream adataoutput = null;  
      
      
Server output Stream that to static ArrayList list = new ArrayList ();  
public static void Main (string[] args) {try {aserversocket = new ServerSocket (50003);//Listen 8888 port.  
System.out.println ("Already listen 50003 port.");  
catch (Exception e) {e.printstacktrace ();  
int num = 0;  
while (Num <) {Socket asessionsoket = null;  
try {asessionsoket = aserversocket.accept ();  
Mythread thread = new Server (). New Mythread (Asessionsoket);  
Thread.Start ();  
num = List.size (); catch (IOException E1) {//TODO auto-generated Catch block E1.printstacktrace ();  
Class Mythread extends Thread {Socket asessionsoket = null;  
Public mythread (socket socket) {asessionsoket = socket;  
public void Run () {try {adatainput = new DataInputStream (Asessionsoket.getinputstream ());  
Adataoutput = new DataOutputStream (Asessionsoket.getoutputstream ());  
      
List.add (Adataoutput);  
while (true) {String msg = ADATAINPUT.READUTF ();//Read MSG. if (!msg.equals ("Connect ...")) {System.out.println ("IP:" + asessionsoket.getinetaddress ());//IP.  
SYSTEM.OUT.PRINTLN ("Receive msg:" + msg);  
for (int i = 0; i < list.size (); i++) {DataOutputStream output = (dataoutputstream) list.get (i);  
Output.writeutf (msg + "----" + list.size ());  
if (Msg.equals ("End")) the break;  
} adataoutput.writeutf ("");  
The catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();  
Finally {try {adatainput.close (); if (adataoutput!= null) adataOutput.close ();  
List.remove (Adataoutput);  
Asessionsoket.close (); catch (Exception E2) {

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.