我想做的是客户端循环截屏以后发送给服务端,服务端循环接收并且显示出来
这是服务端代码:
package com.cxj.SocketImageService;import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Enumeration;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;public class SocketImageService extends Activity {
private static final int PORT = 9999;
private Button bt;
private TextView tv_ip;
static ImageView im;
    static Bitmap bitmap;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt = (Button) findViewById(R.id.button1);
tv_ip = (TextView) findViewById(R.id.textIP);
im=(ImageView)findViewById(R.id.imageView1);
tv_ip.setText("本机IP:" + getLocalIpAddress());
new Thread(runnable).start();
bt.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
        try {
            
           new Thread() {
                public void run() {
                        try {
                         ServerSocket server = new ServerSocket(PORT);
                         Socket socket = server.accept();
                            receiveFile(socket);
                        } catch (Exception e) {
                        
                    }
                }             }.start();
 //启动线程运行
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});
}
    public static void receiveFile(Socket socket) {
    
        byte[] inputByte = null;
        int length = 0;
        InputStream dis = null;
        while(true){
        try {
            try {
                dis = new DataInputStream(socket.getInputStream());
                ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
                inputByte=new byte[1024];   
                while ((length = dis.read(inputByte,0,inputByte.length))>0) {
                 bytestream.write(inputByte, 0, length);
                }
                byte[] imageData=bytestream.toByteArray();
                bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
                im.setImageBitmap(bitmap);
                bytestream.flush();
                bytestream.close();
            } finally {
                if (dis != null)
                    dis.close();
            }
        } catch (Exception e) {        }
    }
    }
public String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf
.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e("WifiPreference IpAddress", ex.toString());
}
return null;
}
private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        public void run() {
            this.update();
            handler.postDelayed(this, 1000);// 间隔1秒
        }
        void update() {
         im.invalidate();
        }
    }; 
}这是客户端代码:
package com.cxj.socketimage;import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;public class SocketImageClient extends Activity implements Runnable { private EditText ed_ip = null;
private Button bt_ip;
private Button start_button;
private Button end_button;
private static String HOST = "";
private static final int PORT = 9999;
private Socket socket = null;
private BufferedReader in = null;
private String content = "";
Handler handler = new Handler();
int length = 0;
byte[] sendBytes = null;
OutputStream dos = null;
InputStream fis = null; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed_ip = (EditText) findViewById(R.id.EditIP);
start_button = (Button) findViewById(R.id.start_bt);
end_button = (Button) findViewById(R.id.end_bt);
bt_ip = (Button) findViewById(R.id.IPbutton);
bt_ip.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
HOST = ed_ip.getText().toString();
try {
socket = new Socket(HOST, PORT);
} catch (IOException ex) {
ex.printStackTrace();
ShowDialog("login exception" + ex.getMessage());
}
}
});
start_button.setOnClickListener(new Button.OnClickListener() { public void onClick(View arg0) {
new Thread(runnable).start();
Toast.makeText(SocketImageClient.this, "start Shot",
Toast.LENGTH_SHORT).show();
}
});
end_button.setOnClickListener(new Button.OnClickListener() { public void onClick(View arg0) {
handler.removeCallbacks(runnable);
}
});
} public void ShowDialog(String msg) {
new AlertDialog.Builder(this).setTitle("notification").setMessage(msg)
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).show();
} public void run() {
try {
while (true) {
if (socket.isConnected()) {
if (!socket.isInputShutdown()) {
if ((content = in.readLine()) != null) {
content += "\n";
} else {
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
} Runnable runnable = new Runnable() {
public void run() {
try {
dos = new DataOutputStream(socket.getOutputStream());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
takeScreenShot(end_button).compress(
Bitmap.CompressFormat.PNG, 90, baos);
fis = new ByteArrayInputStream(baos.toByteArray());
sendBytes = new byte[1024];
while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {
dos.write(sendBytes, 0, length);
}
new Thread(SocketImageClient.this).start();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dos != null)
dos.close();
if (fis != null)
fis.close();
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
} }
handler.postDelayed(runnable, 2000);
}
}; private static Bitmap takeScreenShot(View arg0) {
View view = arg0.getRootView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
return bitmap;
}
}
现在的问题是服务端只能收到第一张图片。

解决方案 »

  1.   

    初步看了一下
    好像是在服务器代码中,
    receiveFile(socket);
    你没有使用while循环接收数据,只是接收了一次数据不知道是不是这样。
      

  2.   

    那while(true){
     receiveFile(socket);
    }就可以了吗?
    在服务端现在用runnable.run不会出错,但是一旦用了new Thread(runnable).start();
    开启就出错了,不知道为什么,信新人,麻烦多多指教
      

  3.   

    自己顶自己- -,那个runnable的问题解决了,可是服务端还是只能接收一张图片,客户端是在不断发送的,这个是明确的了。
    求高手大大帮忙。
      

  4.   

    不知道是不是server没有close的关系。
    你每个server都没有关闭。