简体   繁体   中英

String.join for integers?

How to have receive string array using join?

I'm trying to create server TCP socket that receives from client array of integers.

Having a problem with line:

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

in "ser" class. I know what I wrote is absolutely not possible but might help in describing what I want to do.

CLIENT:

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();
        }
    }
}

I'd suggest using an ArrayList and ObjectOutputStream for this purpose.

In the client:

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

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

In the server:

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

The list should contain the transfered numbers. I'd suggest using ArrayLists over primitive integers. It's easier to work with!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM