通信阶段和android入门总结
??????
import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.FileInputStream;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;public class server3 {public void startServer(int port, String path) {try {ServerSocket sc = new ServerSocket(port);System.out.println("服务器已启动,侦听客户端...");//while (true) {Socket client = sc.accept();System.out.println("ip:" + client.getInetAddress());System.out.println();InputStream in = client.getInputStream();OutputStream out = client.getOutputStream();DataInputStream dis = new DataInputStream(in);DataOutputStream dos = new DataOutputStream(out);FileInputStream file = new FileInputStream(path);DataInputStream image = new DataInputStream(file);int a = file.available();byte[] b = new byte[a+1];image.read(b);dos.writeInt(a);dos.write(b);System.out.println("b发送成功!");//}} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {server3 s = new server3();s.startServer(7777,"E:/01.jpg");}}import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.Socket;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.widget.ImageView;public class third extends Activity {// 定义各个streamprivate InputStream in;private DataInputStream dis;private DataOutputStream dos;private OutputStream out; private ImageView view;// 初始化public void init(String ip,int port) {try {Socket s = new Socket(ip, port);in = s.getInputStream();out = s.getOutputStream();dos = new DataOutputStream(out);dis = new DataInputStream(in);} catch (Exception e) {e.printStackTrace();}}public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);view =(ImageView)this.findViewById(R.id.view);this.init("192.168.1.101",7777);byte[] b = new byte[6185];try {int i = 0;dis.readInt();while(true){ byte a = dis.readByte(); b[i] = a; i++;}} catch (IOException e1) {e1.printStackTrace();}System.out.println(b.length+b[3]);Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);view.setImageBitmap(bitmap);//view.setImageDrawable(drawable);}}
?
?
<!--EndFragment-->