客户端
new Thread(){
@Override
public void run(){
try {
Socket server=new Socket("10.0.2.2", 9999);
server.setSoTimeout(90000);
Log.e("连接信息", "连接服务器成功");
PrintWriter pout=new PrintWriter(server.getOutputStream());
BufferedReader pin=new BufferedReader(new InputStreamReader(server.getInputStream()));
while(true){
pout.println(text_name.getText().toString()+"#"+text_pw.getText().toString());
pout.flush();
Thread.sleep(1000);
String info= pin.readLine();
if(info!=null)
Log.e("接收信息", info);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
服务端
//获取cpu核数
<span style="white-space:pre"> </span>int cpunum=Runtime.getRuntime().availableProcessors();
<span style="white-space:pre"> </span>//创建线程池
<span style="white-space:pre"> </span>ExecutorService executorService=Executors.newFixedThreadPool(cpunum);
<span style="white-space:pre"> </span>ServerSocket socket = new ServerSocket(9999);
<span style="white-space:pre"> </span>int count=0;
<span style="white-space:pre"> </span>System.out.println("服务已启动");
<span style="white-space:pre"> </span>while(true){
<span style="white-space:pre"> </span>Socket lisener=socket.accept();
<span style="white-space:pre"> </span>count++;
<span style="white-space:pre"> </span>userlist.add(lisener);
<span style="white-space:pre"> </span>executorService.execute(new myThread(lisener,"liser"+count));
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
mythread
package com.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class myThread implements Runnable {
<span style="white-space:pre"> </span>public String threadName;
<span style="white-space:pre"> </span>public Socket socket;
<span style="white-space:pre"> </span>public myThread(String threadName){
<span style="white-space:pre"> </span>this.threadName=threadName;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public myThread(Socket lisener,String threadName){
<span style="white-space:pre"> </span>this.socket=lisener;
<span style="white-space:pre"> </span>this.threadName=threadName;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void run() {
<span style="white-space:pre"> </span>Date date=new Date();
<span style="white-space:pre"> </span>DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
<span style="white-space:pre"> </span>String time=format.format(date);
<span style="white-space:pre"> </span>System.out.println(time+" 客户端"+threadName+"请求连接");
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>//获取输入流
<span style="white-space:pre"> </span>BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
<span style="white-space:pre"> </span>PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
<span style="white-space:pre"> </span>while(true){
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>String info=in.readLine();
<span style="white-space:pre"> </span>if(info!=null)
<span style="white-space:pre"> </span>System.out.println("客户端发出信息为"+info);
<span style="white-space:pre"> </span>out.println("sever info");
<span style="white-space:pre"> </span>out.flush();
<span style="white-space:pre"> </span>Thread.sleep(1000);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>} catch (IOException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>} catch (InterruptedException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
}