安卓socket例子

客户端
 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>}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值