我正试图通过一个套接字将一个字符串从谷歌玻璃设备发送到桌面PC --它一直在等待,并且有一次告诉我字符串是空的。我也试图在字符串之后发送一张图片,但是如果我能让字符串显示出来,这将是一个巨大的帮助目前。
Client:
private void createConnection()
{
//Create the java socket and send the file to the android device
//Need to send out the voice command to the Android phone and the picture
//for now, send the voice data
try {
try {
try {
String ip = "192.168.1.149";
Socket skt = new Socket();
skt = new Socket(ip, 40404);
///////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////
//send the string.
OutputStream outstream = skt.getOutputStream();
PrintWriter out = new PrintWriter(outstream);
out.print(voiceglobal);
Log.d("sending", voiceglobal);
long length = pictureToSend.length();
//Create a new socket for sending the picture.
Socket pic_socket = new Socket(ip, 50505);
byte bytes[];
ObjectInputStream ois = new ObjectInputStream(pic_socket.getInputStream());
FileOutputStream fos = null;
try {
bytes = (byte[])ois.readObject();
fos = new FileOutputStream(pictureToSend);
fos.write(bytes);
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} finally {
if(fos!= null)
{
fos.close();
}
}
} catch (UnknownHostException u)
{
System.err.print(u);
}
} catch (UnknownHostException e) {
//Handle this.
System.err.print(e);
}
} catch (IOException v) {
// handle this.
System.err.print(v);
}
} Server:
//server
void createConnection()
{
try {
ServerSocket serv = new ServerSocket(40404);
ServerSocket servimage = new ServerSocket(50505);
while(true)
{
System.out.println("Listening ... \n");
Socket client = serv.accept();
BufferedReader enter = new BufferedReader(new InputStreamReader(client.getInputStream()));
enter.readLine();
String data = enter.readLine();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(client.getOutputStream()), true);
pw.println(data);
System.out.println("String was " + data);
Globals.voicedata = data;
//Now recieve the image from the secondary serversocket.
System.out.println("Recieving image .. \n");
Socket clientimage = servimage.accept();
System.out.println("image srv connected to: " + clientimage.getInetAddress());
//Bufferedimage should go to an imageview on the GUI.
BufferedImage img=ImageIO.read(ImageIO.createImageInputStream(clientimage.getInputStream()));
//Convert the bufferedimage into an image.
//reason is that bufferedimages can't be displayed in a javafx imageview.
WritableImage wr = null;
if (img != null) {
wr = new WritableImage(img.getWidth(), img.getHeight());
PixelWriter pw2 = wr.getPixelWriter();
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
pw2.setArgb(x, y, img.getRGB(x, y));
}
}
Globals.img = wr;
}
else
{
System.out.println("Image was null. \n");
}
} //while true
} catch (IOException e)
{
System.out.println("Something went wrong. \n");
System.out.println(e.fillInStackTrace());
}
}提前谢谢你!
发布于 2015-12-09 00:29:08
您的客户端代码正在用readObject()接收图像,而不是用writeObject()发送图像。而且您的服务器还试图从客户端读取映像,而不是使用readObject(),因此您无论如何都不会使用兼容的协议。
但是..。这是行不通的。您不能假设客户端将神奇地按顺序连接到两个套接字,而且客户端之间的交织永远不会发生。对文件名和图片都使用单个套接字。使用DataInput/OutputStreams,对文件名使用read/writeUTF(),对图片数据只使用普通的read()和write()方法。
https://stackoverflow.com/questions/34163887
复制相似问题