String.join是否为整数?

时间:2017-05-20 22:03:56

标签: java arrays string sockets client-server

如何使用join接收字符串数组?

我试图创建从客户端整数数组接收的服务器TCP套接字。

遇到问题:

        int joined = Integer.valueOf(String.join(",", list));

in" ser"类。我知道我所写的内容绝对不可能,但可能有助于描述我想做的事情。

客户端:

import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class cli {

    private static Socket socket;
    private static final int PORT = 8000;
    private static DataInputStream input;
    private static ObjectOutputStream output;

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        System.out.println("Enter number of the elements in array: ");
        int broj = in.nextInt();
        List<Integer> listica = new ArrayList<>();

        System.out.println("Enter numbers for your array : ");
        for (int i = 0; i < broj; i++) {
            listica.add(in.nextInt());
        }

        try {
            socket = new Socket("localhost", PORT);
            output = new ObjectOutputStream(socket.getOutputStream());
            input = new DataInputStream(socket.getInputStream());

            output.writeObject(listica);
            String str = input.readUTF();
            System.out.println("New array of numbers is : ");
            System.out.println(str);

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            in.close();
        }
    }
}

SERVER

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;



public class ser {

    private static ServerSocket server;
    private static Socket socket;
    private static final int PORT = 8000;
    private static ObjectInputStream input;
    private static DataOutputStream output;

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();

        try {
            try {
                server = new ServerSocket(PORT);

                socket = server.accept();
                input = new ObjectInputStream(socket.getInputStream());
                Object ob = input.readObject();
                System.out.println("Data that is sent from client :");
                System.out.println(ob);
                list = (List<Integer>) ob;

                int joined = Integer.valueOf(String.join(",", list));

                output = new DataOutputStream(socket.getOutputStream());
                output.writeByte(joined);

            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我建议使用ArrayList和ObjectOutputStream来实现此目的。

在客户端:

int arr = {1, 2, 3, 4};
ArrayList list = // copy the array

ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(list);

在服务器中:

ObjectInputStream ois = ObjectInputStream(socket.getInputStream());
ArrayList list = (ArrayList) ois.readObject();

列表应包含转移的数字。我建议使用ArrayLists而不是原始整数。它更容易使用!