我是Java新手,所以我需要帮助。我正在编写一个应用程序,将onClick发送一个字符串到服务器和服务器需要返回一个图像使用套接字。所以我的客户端是Android,服务器端是PC-java。 我认为我的服务器端是好的(因为他打印出所有的system.out.print命令),但我的客户端不好。请告诉我我的错误在哪里?谢谢!通过套接字发送图片(Java PC - Android)
这里是我的服务器(PC)端(插座交付直通功能参数)的代码:
try {
dataInputStream = new DataInputStream(socket.getInputStream());
poruka = "" + dataInputStream.readUTF();
System.out.print(poruka);
int bytecount = 2048;
byte[] buf = new byte[bytecount];
OutputStream OUT = socket.getOutputStream();
BufferedOutputStream BuffOUT = new BufferedOutputStream(OUT, bytecount);
FileInputStream in = new FileInputStream("screenShot.jpg");
int i = 0;
while ((i = in.read(buf, 0, bytecount)) != -1) {
BuffOUT.write(buf, 0, i);
System.out.print("check" + buf[0]);
BuffOUT.flush();
}
in.close();
BuffOUT.close();
System.out.print("over");
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
socket.close();
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
,这里是我的客户端(安卓)侧:
Socket socket = null;
DataOutputStream dataOutputStream = null;
try {
socket = new Socket(IPadresa, 8888);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
slanje = "hy string";
dataOutputStream.writeUTF(slanje);
FileOutputStream outToFile = new FileOutputStream("slika.jpg");
int bytecount = 2048;
byte[] buf = new byte[bytecount];
InputStream IN = socket.getInputStream();
BufferedInputStream BuffIN = new BufferedInputStream(IN, bytecount)
int i = 0;
int filelength = 0;
while((i = BuffIN.read(buf, 0, bytecount)) != -1) {
filelength += i;
outToFile.write(buf, 0, i);
outToFile.flush();
}
IN.close();
BuffIN.close();
dataOutputStream.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
更多的信息: 在服务器端,我可以看到从客户端发送的字符串。我有这个System.out.print(“over”);命令打印每次我发送字符串到服务器。另外我有System.out.print(“check”+ buf [0]);从服务器打印出来很多次。所以这就是为什么我认为客户端有问题。
而我的客户端不会抛出任何异常......但我注意到客户端从未通过while循环。它卡在那里。
请告诉我们是什么让你觉得客户端存在问题。怎么了?任何例外?客户端和服务器端的输出是什么? – 2013-02-16 23:17:59
我编辑了我的问题,以便您可以阅读是什么让我觉得客户端存在问题。感谢您的帮助! – jpetrucci 2013-02-17 00:00:13