码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-02-13 16:34:20      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:android   android应用   通信   线程   socket   

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客户端利用sokcet通信和向Java服务端发请求,Java服务端把文件发给android客户端

标签:android   android应用   通信   线程   socket   

原文地址:http://blog.csdn.net/u014600432/article/details/43795029

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!