从Java套接字发送时添加在整数数组中的空字符串
问题描述:
我想从客户端使用ObjectOutputStream和WriteObject方法发送整数数组,并从服务器接收数组。下面分别是客户端和服务器代码:从Java套接字发送时添加在整数数组中的空字符串
客户:
int numberArray [] = new int[]{3,6,1,5,8};
ObjectOutputStream os =new ObjectOutputStream(socket.getOutputStream());
os.writeObject(numberArray);
服务器:
ObjectInputStream is = new ObjectInputStream(socket.getInputStream());
int[] Array = (int[])is.readObject();
for (int e : Array) {
System.out.print(e + " ");
}
客户端可以在阵列成功发送。然而,当我想读会出现以下错误和退出程序数组:
3 6 1 5 8
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at testserver.TestServer.main(TestServer.java:73)
Java Result: 1
请有谁能够告诉我在哪里,我做了错误的?
答
我不知道我的android工作室或Netbeans发生了什么奇怪的事情。我已经写了一个新的项目中的代码,现在它正在工作! 这是我的完整代码。对于上次简单的麻烦我没有发布整个代码。
但是,在客户端代码中,如果我只使用打印方法,控制台中没有输出,但是如果使用println方法,则数组的内容将在控制台中打印。有人可以解释为什么会发生这种情况吗?
服务器
package testserver;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TestServer {
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
ServerSocket listener;
InetAddress addr = InetAddress.getLocalHost();
listener = new ServerSocket(9090,50,addr);
try {
System.out.println("Waiting for Client...!!!");
while (true) {
Socket socket = listener.accept();
try {
ObjectInputStream is = new ObjectInputStream(socket.getInputStream());
int[] Array = (int[])is.readObject();
for (int e : Array) {
System.out.print(e + " ");
}
ObjectOutputStream os =new ObjectOutputStream(socket.getOutputStream());
os.writeObject(Array);
} finally {
socket.close();
}
}
}
finally {
listener.close();
}
}
}
客户
package com.example.asus.clientwitharray;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client extends AsyncTask<Void, Void, Void> {
String dstAddress;
int dstPort;
String response ="";
TextView textResponse;
Client(String addr, int port, TextView textResponse) {
dstAddress = addr;
dstPort = port;
this.textResponse = textResponse;
}
@Override
protected Void doInBackground(Void... arg0) {
Socket socket = null;
try {
socket = new Socket(dstAddress, dstPort);
int numberArray [] = new int[]{2,4,6,1,9};
ObjectOutputStream os =new ObjectOutputStream(socket.getOutputStream());
os.writeObject(numberArray);
ObjectInputStream is = new ObjectInputStream(socket.getInputStream());
int[] Arr = (int[])is.readObject();
for (int e : Arr) {
System.out.println(e);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "IOException: " + e.toString();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
textResponse.setText(response);
super.onPostExecute(result);
}
}
是否有可能为您发布更多的代码?我测试了你到目前为止,并没有得到任何例外。 – tima
请提供[mcve],以便我们重现此内容。它看起来像你从你的代码中调用'parseInt',但是你没有显示出...(我不太清楚你从当前的问题中得到5个upvotes ...) –