从Java套接字发送时,在整数数组中添加空字符串

时间:2017-05-05 23:14:24

标签: java arrays sockets objectoutputstream

我正在尝试从客户端使用ObjectOutputStream和WriteObject方法发送整数数组,并从服务器接收数组。这里分别是客户端和服务器代码:

客户端:

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

任何人都可以告诉我,我在做错了吗?

1 个答案:

答案 0 :(得分:0)

我不知道我的android工作室或Netbeans发生了什么奇怪的事。我已经在一个全新的项目中编写了代码,现在它正在运行!!!! 这是我的完整代码。为了简单起见,上次我没有发布整个代码。

但是,在客户端代码中,如果我只使用print方法,则控制台中没有输出,但如果我使用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);
    }
}