android 實現socket通訊

來源:互聯網
上載者:User

android 實現socket通訊

用戶端代碼:

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:

 

 

AndroidMenifest.xml檔案中添加網路存取權限:

 

 

 

伺服器:

不是web工程,直接run main函數就可以了。

 

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){}}}
 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.