JAVA TCP多张图片传输(视频)

wlyqiqi 2014-05-30 01:55:00
为何这个客户端只能接受单张图片啊,连续多张就不行,应该如何循环??

package socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;

import org.apache.commons.codec.binary.Base64;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import simplestream.Compressor;
import simplestream.Instruction;
import simplestream.Simplestream;
import simplestream.Viewer;
import sun.misc.BASE64Decoder;//base64 to byte[]
import sun.misc.BASE64Encoder;//byte[] to base64
public class TCPClient implements Runnable {



@Override
public void run() {
Socket s = null;
try {
Instruction ins = new Instruction();
int serverPort = Simplestream.rport;
InetAddress addr = InetAddress.getByName(Simplestream.remote);//remote is IP
//System.out.println(serverPort+" and "+addr.toString());
s = new Socket(addr, serverPort);
//System.out.println("Connection Established");
DataInputStream in = new DataInputStream(s.getInputStream());
DataOutputStream out = new DataOutputStream(s.getOutputStream());

//Received start **************************************************************************
String startjson = in.readUTF();
JSONObject obj=null;
JSONParser parser=null;
try {
parser = new JSONParser();
obj = (JSONObject) parser.parse(startjson);
} catch (org.json.simple.parser.ParseException e) {
e.printStackTrace();
System.exit(-1);
}
ins.response = (String) obj.get("response");
System.out.println("client Received response:"+ ins.response);


//Send startstream***************************************************************************
JSONObject jsondata=new JSONObject();
jsondata.put("request", "startstream");
jsondata.put("ratelimit", Simplestream.rate);
out.writeUTF(jsondata.toJSONString()); // startstream

//receive starting
String startingjson = in.readUTF();
try {
parser = new JSONParser();
obj = (JSONObject) parser.parse(startingjson);
} catch (org.json.simple.parser.ParseException e) {
e.printStackTrace();
System.exit(-1);
}
ins.response = (String) obj.get("response");
System.out.println("client Received response: " + ins.response);

//receive image**************************************************************************************************
//do-while
do{
@SuppressWarnings("deprecation")
String imagejson = in.readLine();
obj=null;

try {
parser = new JSONParser();
obj = (JSONObject) parser.parse(imagejson);
} catch (org.json.simple.parser.ParseException e) {
e.printStackTrace();
System.exit(-1);
}
ins.response = (String) obj.get("response");
ins.data = ((String) obj.get("data")).getBytes();
System.out.println("client Received response: " + ins.response);
//open viewer
Viewer myViewer = new Viewer();
JFrame frame = new JFrame("Simple Stream Viewer");
frame.setVisible(true);
frame.setSize(320, 240);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(myViewer);

//byte[] nobase64_image = Base64.decodeBase64((new BASE64Decoder()).decodeBuffer((String) obj.get("data")));
byte[] nobase64_image = Base64.decodeBase64(ins.data);
/* Decompress the image */
byte[] decompressed_image = Compressor.decompress(nobase64_image);
/* Give the raw image bytes to the viewer. */
myViewer.ViewerInput(decompressed_image);
frame.repaint();

//end do-while
}while(true);

} catch (UnknownHostException e) {
System.out.println("Socket:" + e.getMessage());
} catch (EOFException e) {
System.out.println("EOF:" + e.getMessage());
} catch (IOException e) {
System.out.println("readline:" + e.getMessage());
} finally {
if (s != null)
try {
s.close();
} catch (IOException e) {
System.out.println("close:" + e.getMessage());
}
}
}


}
...全文
234 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
丘小鱼 2015-12-29
  • 打赏
  • 举报
回复

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧