Socket实现pc端与手机Android端图片传输

socket编程的一般步骤:
1>创建Socket
2>打开连接到Socket的输入/出流
3>按照一定的协议对Socket进行读/写操作
4>关闭Socket

具体代码如下:
服务端(pc端):
package com.text8;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Server {
	//采用List来保存所客户端的信息
	private static List sockets=new ArrayList();	
	public static void main(String [] args){
		new Server().startServrer();
	}
	private void startServrer(){
		ServerSocket ss=null;
		try {
			//与服务端建立连接,端口号10000
			ss=new ServerSocket(9998);
			while(true){
				Socket s=ss.accept();
				sockets.add(s);
				new Server().new Start(s).start();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	class Start extends Thread{
		Socket s=null;  //客户端
		InputStream in=null;    //输入流,读
	
		public Start(Socket s){
			this.s=s;
		}
		public void run(){
			try {
				DataOutputStream dos = new DataOutputStream(s.getOutputStream()); 
				System.out.println("success");
	            FileInputStream fis = new FileInputStream("F:/server/server.png"); 
	            System.out.println("client");
	            int size = fis.available();  
	            byte[] data = new byte[size];  
	            fis.read(data);  
	            dos.writeInt(size);  
	            dos.write(data);  
	            dos.flush();  
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				try {
					
					s.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
	}

}
客户端(Android端):

package com.example.fdt.client;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
 import java.io.IOException;
import java.net.Socket;

 import android.app.Activity;
        import android.graphics.Bitmap;
        import android.graphics.Bitmap.CompressFormat;
        import android.graphics.BitmapFactory;
        import android.os.Bundle;
        import android.os.Handler;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Button;
        import android.widget.ImageView;

public class MainActivity extends Activity {

    private ImageView imageView = null;
    private Bitmap bmp = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.image01);
        Button btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                Thread t = new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        Socket s = null;
                        try {
                            s= new Socket("192.168.1.100", 9998);
                            DataInputStream dataInput = new DataInputStream(
                                    socket.getInputStream());
                            int size = dataInput.readInt();
                            byte[] data = new byte[size];
                            int len = 0;
                            while (len < size) {
                                len += dataInput.read(data, len, size - len);
                            }

                            ByteArrayOutputStream out = new ByteArrayOutputStream();
                            bmp = BitmapFactory.decodeByteArray(data, 0,
                                    data.length);
                            bmp.compress(CompressFormat.PNG, 100, out);
                            myHandler.obtainMessage().sendToTarget();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                socket.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                };
                t.start();
            }
        });
    }

    private Handler myHandler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            imageView.setImageBitmap(bmp);
        };
    };

}



你可能感兴趣的:(android,java)