Empty string added to Integer array when sending from java socket
I am trying to send an integer array using ObjectOutputStream and WriteObject method from client and receiving array from server. Here is the client and server code respectively:
Customer:
int numberArray [] = new int[]{3,6,1,5,8};
ObjectOutputStream os =new ObjectOutputStream(socket.getOutputStream());
os.writeObject(numberArray);
Server:
ObjectInputStream is = new ObjectInputStream(socket.getInputStream());
int[] Array = (int[])is.readObject();
for (int e : Array) {
System.out.print(e + " ");
}
The client can send the array successfully. However, when I try to read the array, the following error occurs and the program exits:
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
Please, can someone tell me where I am doing wrong?
source to share
I don't know what weird happened in my android studio or Netbeans. I wrote the code in a new new project and now it works !!!! Here is my complete code. For the bag of simplicity, I didn't post all the code last time.
However, in client code, if I only use the print method, there is no output to the console, but if I use the println method, the contents of the array will be printed to the console. Can someone explain why this is happening?
Server
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();
}
}
}
Client
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);
}
}
source to share