android客户端利用sokcet通信和向Java服务端发请求,Java服务端把文件发给android客户端

Java服务端

package com.yqq.socketTest;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketTest {
	static ServerSocket mServerSocket;
	static Socket socket;
	public static void main(String[] args) throws IOException {
		mServerSocket=new ServerSocket(20000);
		while(true){
			 socket=mServerSocket.accept();
			(new Thread(new ReadThread(socket))).start();
			
			
		}
	}
	/**
	 * 读取客户端指令
	 * @author yqq_coder
	 *
	 */
	static class ReadThread implements Runnable{
		private Socket mSocket;
		
		public ReadThread(Socket socket){
			mSocket=socket;
		}
		@Override
		public void run() {
			System.out.println("读线程开启");
			InputStream  in=null;
			OutputStream os=null;
				ByteArrayOutputStream mByteArrayOutputStream=new ByteArrayOutputStream();
			try {
			 in=mSocket.getInputStream();
			 int count=0;
			 byte[] buffer=null;
			 while((count=in.available())==0){
				 count=in.available();
				 try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				 
			 }
			 buffer=new byte[count];
			 in.read(buffer);
			 mByteArrayOutputStream.write(buffer);
			 
			// askForFile
			 System.out.println("读线程开启"+mByteArrayOutputStream.toString());
			 if(mByteArrayOutputStream.toString().equals("askForFile")){
				 File file=new File("F://测试.xls");
					FileInputStream is=null;
	
				
						 is=new FileInputStream(file);
						 os=mSocket.getOutputStream();
						 ByteArrayOutputStream arrayOutputStream=new ByteArrayOutputStream();
						 byte[] buffer2=new byte[1024];
						 while(is.read(buffer)!=-1){
							 arrayOutputStream.write(buffer2);
							 System.out.println("写线程开启socket测试");
						 }
						 os.write(arrayOutputStream.toByteArray());
						 os.flush();
						 socket.shutdownOutput();
				
				 System.out.println("读线程开启socket"+socket.isConnected());
				/* //启动写线程
				 (new Thread(new WriteThread(socket))).start();*/
			 }
			} catch (IOException e) {
				
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				
				if(in!=null){
					try {
						in.close();
						in=null;
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
				
				if(os!=null){
					try {
						os.close();
						os=null;
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
			}
			
		}
		
		
	
	
	/**
	 * 向客户端写数据(文件)
	 * @author yqq_coder
	 *
	 *//*
	static class WriteThread implements Runnable{
		
		private Socket mSocket;
		public WriteThread(Socket socket){
			mSocket=socket;
		}
		@Override
		public void run() {
			System.out.println("写线程开启");
			File file=new File("F://测试.xls");
			FileInputStream is=null;
			OutputStream os=null;
			try {
				 is=new FileInputStream(file);
				 os=mSocket.getOutputStream();
				 ByteArrayOutputStream arrayOutputStream=new ByteArrayOutputStream();
				 byte[] buffer=new byte[1024];
				 while(is.read(buffer)!=-1){
					 arrayOutputStream.write(buffer);
				 }
				 os.write(arrayOutputStream.toByteArray());
				 os.flush();
				 mSocket.shutdownOutput();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				
				if(os!=null){
					try {
						os.close();
						os=null;
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			}
			
		}
		
		*/
		}
	}

}



android客户端:

package com.yqq.SocketClient;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

public class SocketClient extends Activity {
	private Socket socket;
	InputStream inputStream;
	OutputStream outputStream;
	Handler mHandler=new Handler(){
		
		public void handleMessage(Message msg) {
			
			if(msg.what==0x123){
				Toast.makeText(SocketClient.this,"文件下载完毕", 0).show();
			}
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(com.example.socketclient.R.layout.main);
	}

	public void downFile(View v){
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					socket=new Socket(InetAddress.getByName("172.20.113.1"), 20000);
					inputStream=socket.getInputStream();
					outputStream=socket.getOutputStream();
					outputStream.write("askForFile".getBytes());
					outputStream.flush();
					//socket.shutdownOutput();
					Thread.sleep(3000);
					byte[] buffer=new byte[1024];
					File file=new File(getCacheDir(),"测试");
					FileOutputStream fileOutputStream=new FileOutputStream(file);
					while(inputStream.read(buffer)!=-1){
						fileOutputStream.write(buffer);
						
					}
					
					Looper.prepare();
					Message message=Message.obtain();
					message.what=0x123;
					mHandler.sendMessage(message);
					Looper.loop();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}).start();
		
	}
}

下载地址:http://download.csdn.net/detail/u014600432/8445151

你可能感兴趣的:(android,线程,通信,socket,android应用)