java Android socket 多线程 总结_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java Android socket 多线程 总结

java Android socket 多线程 总结

 2011/10/27 8:15:57  busuanzi  http://busuanzi.iteye.com  我要评论(0)
  • 摘要:今天做了一个多线程的例子,多个Android手机和电脑通信。首先要说的时候我的一个理解误区,也是我最大的收获。先说一下socket,比如while(true){Socketsc=ServerSocker.accept();}我一直以为这个阻塞是自己的while循环产生的,后来差资料发现,accept()方法自己产生阻塞,也就说进入while循环以后,代码会停在Socketsc=ServerSocker.accept();这一行,知道有链接请求,才会进入下一个循环,继续等待连接,在那里阻塞
  • 标签:总结 android 多线程 Java socket 线程
今天做了一个多线程例子,多个Android手机和电脑通信。

首先要说的时候 我的一个理解误区,也是我最大的收获。

先说一下socket,比如

while(true){
     Socket sc=ServerSocker.accept();
}


我一直以为这个阻塞是自己的while循环产生的,后来差资料发现,accept()方法自己产生阻塞,
也就说 进入while循环以后,代码会停在 Socket sc=ServerSocker.accept();这一行,知道有链接请求,才会进入下一个循环,继续等待连接,在那里阻塞。 这个while 循环是为了多个客户端链接,每一个链接都会得到一个Socket实例,用这个实例与客户端通讯,
这就需要自己开辟进程,进行处理了。语文学的不好,贴出代码来

java程序
package com.bh;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server{

	/**
	 * @param args
	 */
	int i=0;
	private static ServerSocket mServerSocker;
	private static Socket sc;
//	private static BufferedReader in;
//	private static PrintWriter out;
	public static void main(String[] args) throws IOException {
		
		mServerSocker=new ServerSocket(8888);
		while(true){
			sc=mServerSocker.accept();
			System.out.println("aaaaaaaa");
			//Server s=new Server();
			Thread r=new ReciveThreat(sc);
			r.start();
			Thread s=new SendThread(sc); 
			s.start();
			System.out.println("kaishi");
			//my.run();
			
			//new Server().run();
		}
		//out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(sc.getOutputStream())),true);
	}	
}


send类
package com.bh;

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

public class SendThread extends Thread{
	private Socket sc;
	int i=0;
	//private BufferedReader in;
	private PrintWriter out;
	public SendThread(Socket s) throws IOException {
		sc=s;
		//in=new BufferedReader(new InputStreamReader(sc.getInputStream()));
		out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(sc.getOutputStream())),true);
		//System.out.println("ok");
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			while(true){
				Thread.sleep(3000);
				System.out.println("aa");
				out.println(i++);
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}



recive类

package com.bh;

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

public class ReciveThreat extends Thread{
	private Socket sc;
	private BufferedReader in;
	//private PrintWriter out;
	public ReciveThreat(Socket s) throws IOException {
		sc=s;
		in=new BufferedReader(new InputStreamReader(sc.getInputStream()));
		//out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(sc.getOutputStream())),true);
	}
	@Override
	public void run() {
		while(true){
			try {
				String str;
				str = in.readLine();
				if(str!=null){
					System.out.println("read:"+str);
					System.out.println("run......");
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}


Android程序
package com.bh.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Main extends Activity implements Runnable{
    /** Called when the activity is first created. */
	EditText et;
	String recive;
	Socket s;
	TextView t;
	PrintWriter out;
	BufferedReader in;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        et=(EditText) findViewById(R.id.edittext);
        Button b=(Button) findViewById(R.id.send);
        t=(TextView) findViewById(R.id.showtext);
		try {
			s = new Socket("10.0.2.2",8888);
			out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())),true);
			in=new BufferedReader(new InputStreamReader(s.getInputStream()));
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
		
		new Thread(this).start();
        b.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
						String aa=et.getText().toString();
						out.println(aa);
						//s.close();
						Log.d("aaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
			}
		});
    }
    public Handler handler = new Handler(){
    	public void handleMessage(android.os.Message msg) {
    		switch (msg.what) {
			case 1:
				t.setText(recive);
				break;

			default:
				break;
			}
    		
    	};
	};
	@Override
	public void run() {
		while(true){
			try {
				//String str;
				recive = in.readLine();
				Log.d("aaaaa",recive);
				if(recive!=null){
					//t.setText(recive);
					handler.sendEmptyMessage(1);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}



错误 大家指出了 谢谢哈
发表评论
用户名: 匿名