找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4588|回復: 0
打印 上一主題 下一主題
收起左側(cè)

Android Client

[復制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:104287 發(fā)表于 2016-1-31 02:36 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
package com.socket.client;



import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Xml.Encoding;
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. */
        //定義聲明需要用到的UI元素
        private EditText edtmsgcontent;
        private Button btnSend;
        private String ip="192.168.137.1";//服務器ip地址
        private int port=9898;
       
        private Socket socket=null;//socket
        private BufferedWriter writer=null;
       
        Handler handler=new Handler()
        {

                @Override
                public void handleMessage(Message msg) {
                        // TODO Auto-generated method stub
                       
                        byte[] data=(byte[])msg.obj;//取書據(jù)                       
                        String str = new String(data);                
                 Toast.makeText(main.this,str,1).show();
                        super.handleMessage(msg);
                }
               
        };
       
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        InitView();
       try
           {
        socket=new Socket(ip,port);        //客戶端分配socket套接字
            writer=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
           }
       catch(UnknownHostException e)
           {
                   //e.printStackTrace();
           } catch (IOException e)
           {
              // e.printStackTrace();
       }


    }
    private void InitView()
    {
            //顯示主界面
            setContentView(R.layout.main);
           
            //通過id獲取ui元素對象
            edtmsgcontent=(EditText)findViewById(R.id.msgcontent);
            btnSend=(Button)findViewById(R.id.btnsend);
           
            //為btnsend設置點擊事件
            btnSend.setOnClickListener(this);
    }

    public void onClick(View bt)
    {
            new Thread(new ReceivThunder()).start();      //step4:開線程
            try
                {
                   
                        String msg=edtmsgcontent.getText().toString();
                        if(!TextUtils.isEmpty(msg))
                                SendMsg(ip,port,msg);
                        else
                        {
                                Toast.makeText(this,"請先輸入要發(fā)送的內(nèi)容", Toast.LENGTH_LONG);
                                edtmsgcontent.requestFocus();
                        }
                }
         catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                 }
         }
    public void SendMsg(String ip,int port,String msg) throws UnknownHostException, IOException
    {          
            writer.write(msg);
            writer.flush();
    }

    public class ReceivThunder implements Runnable//step1,多線程函數(shù)
    {
                @Override
                public void run() {
                        // TODO Auto-generated method stub

                        char []ch=new char[10];
                       
                        try {
                                BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                       
                                byte[] buffer = new byte[10];   
                            reader.read(ch);
                            for(int i=0;i<10;i++)
                            {
                                    buffer[i]=(byte)ch[i];
                            }
                           
                            Message message=Message.obtain();//攜帶數(shù)據(jù)
                                message.obj=buffer;
                                handler.sendMessage(message);//發(fā)送到handler
                               
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                   
                }
    }   
}


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表