andorid socket 异步方式

服务器端:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;


public  class androidsocketserver extends Thread {
 public BufferedWriter writer;
 public BufferedReader reader;
 public ServerSocket server ;
 
 public androidsocketserver(){
  server=creatserversocket();
  System.out.println("creat serversocket..");
 }
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  androidsocketserver threadsocket=new androidsocketserver();
  //Thread socketthread=new Thread();
  if(threadsocket!=null){
   
   threadsocket.start();
  }
 }
 @Override
 public void run() {
  // TODO Auto-generated method stub
  
  while(true){
  
   Socket socket=null;
   try {
    socket = acceptsocket(server);
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   //接收
   try {
    recmsg(socket);
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
   //发送
   try {
    sendmsg("server received", socket);
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   //关闭socket
   try {
    closesocket(socket);
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
   
  }
 }
//创建服务器socket
 public ServerSocket creatserversocket(){
  try {
   server=new ServerSocket(55555);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  
  
  return server;
 }
 
//创建链接socket
 public Socket acceptsocket(ServerSocket sersocket) throws IOException{
  System.out.println("listening...");
  Socket socket=sersocket.accept();
  System.out.println("server accept...");
  return socket;
 }

 //发送消息
 public void sendmsg(String msg,Socket socket) throws IOException{
  
  writer=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
  writer.write(msg);
  writer.flush();
 }
 //接收消息
 public void recmsg(Socket socket) throws IOException{
  
  reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
  String str=reader.readLine();
  System.out.println("client said:"+str);
 }
 //关闭socket
 public void closesocket(Socket socket) throws IOException{
  System.out.println("server.closed");
  System.out.println();
  reader.close();
  writer.close();
  socket.close();
 }
}

 

android 手机端:

package com.android.androidsocketclient;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
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.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidsocketclientActivity extends Activity {
 

 private EditText mrectext;
 private EditText msendtext;
 private Button msendbtn;
 

 //添加句柄 是为可以在异步方式下 更新ui界面
 private Handler h;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initialview();
       
        h = new Handler(){ 
            //@override 
            public void handleMessage(Message msg){ 
                // call update gui method.
             updateview(msg);
             
           
        }; 

    }
    //初始化view控件
    public void initialview(){
     
     mrectext=(EditText)findViewById(R.id.editText1);
     msendtext=(EditText)findViewById(R.id.editText2);
     msendbtn=(Button)findViewById(R.id.button1);
     
    }
   
    //单机发送的响应函数
   public void clicksend(View v) throws IOException{
     
    Socket socket=connectserver();
    String str=msendtext.getText().toString();
    sendmsg(str, socket);
   
    Thread readthread=startreadthread(socket);
    endreadthread(readthread);
    }
  
   //异步方式的更新ui界面函数
   public void updateview(Message msg){
    mrectext.setText(msg.obj.toString());
   }
   //结束线程
   public void endreadthread(Thread thread){
   
    thread.stop();
   }
   //开启线程
   public Thread startreadthread(Socket socket){
   
    readservermsgthread readmsg=new readservermsgthread(socket, h);
  
    return readmsg;
   }
   //链接服务器
    public Socket  connectserver(){
     
     Socket socket = null;
     try {
   socket=new Socket("10.0.2.2",55555);
  } catch (UnknownHostException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
     
     return socket;
     
     
    }
    //发送消息
    public void sendmsg(String msg,Socket socket) throws IOException{
       BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        writer.write(msg.replace("\n", " ")+"\n");
        writer.flush();
     
    }

   
  
}

 

手机接收线程端:

package com.android.androidsocketclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;


import android.os.Handler;
import android.os.Message;

 

public class readservermsgthread extends Thread {

 public static Socket socket;
 public BufferedReader reader;
 public String readdata;
 public Handler hd;
 public readservermsgthread(Socket sock,Handler h){
  socket=sock;
  hd=h;
  
  this.start();
 }
 
// public static void main(String[] args) {
//  // TODO Auto-generated method stub
//
//  readservermsgthread readmsg=new readservermsgthread(socket);
//  if(readmsg!=null){
//   
//   readmsg.start();
//  }
// }
 
 
 public void run(){
 
   try {
    reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   String str = null;
   try {
    str=reader.readLine();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
   readdata=str;
   
   Message msg=new Message();
   //msg.arg1=readdata;
   msg.obj=readdata;
   msg.obj=readdata;
   hd.sendMessage(msg);
   
  
   
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值