Socket Communication for android

Source: Internet
Author: User

Socket Communication for android

Client code:

Activity:

package com.example.sockettest;import java.io.*;import java.net.Socket;import java.net.UnknownHostException;import android.app.Activity;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class main extends Activity implements OnClickListener {    /**     * Called when the activity is first created.     */    private EditText edtmsgcontent;    private Button btnSend;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //setContentView(R.layout.main);        InitView();    }    private void InitView() {        setContentView(R.layout.main);        edtmsgcontent = (EditText) findViewById(R.id.msgcontent);        btnSend = (Button) findViewById(R.id.btnsend);        btnSend.setOnClickListener(this);    }    public void onClick(View bt) {        try {            String msg = edtmsgcontent.getText().toString();            if (!TextUtils.isEmpty(msg))                SendMsg(msg);            else {                Toast.makeText(this, cccc, Toast.LENGTH_LONG);                edtmsgcontent.requestFocus();            }        } catch (Exception e) {            e.printStackTrace();        }    }    public void SendMsg(final String msg) throws UnknownHostException, IOException {        new Thread() {            @Override            public void run() {                try {                    String ip = 192.168.1.155;                    int port = 1818;                    Socket socket = null;                    socket = new Socket(ip, port);                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));                    writer.write(msg);                    writer.flush();                    writer.close();                    socket.close();                } catch (UnknownHostException e) {                    e.printStackTrace();                } catch (IOException e) {                    e.printStackTrace();                }            }        }.start();    }}
Main. xml:

 

 

Add the network access permission to the AndroidMenifest. xml file:

 

 

 

Server:

Instead of a web project, run the main function directly.

 

package test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;public class Server {public static void main(String[] args){BufferedReader in;PrintWriter out;try{ServerSocket ss = new ServerSocket(43211);System.out.println(1);Socket socket = ss.accept();System.out.println(2);in = new BufferedReader(new InputStreamReader(socket.getInputStream()));out = new PrintWriter(socket.getOutputStream(),true);while(true){String line = in.readLine();System.out.println(line);if(line.equals(88)){break;}out.println(hello:  + line);out.flush();}out.close();in.close();socket.close();}catch(IOException e){}}}
 

 

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.