I've been struggling to get my java client to communicate with with my c++ server. I'm using tcp sockets.

Here's how I setup my server.

    //descriptor for listening for new connections
    int listenDesc;
    //socket information
    struct sockaddr_in myAddr;
    //client connection object
    clientConnection newClient;
    //connectionDesc
    int connectionDesc;
    //set iterator to the start of the vector
    it = clients.begin();
    //initialize the connection and player id tracker
    currentSize = 0;
 
    //create socket
    if ((listenDesc = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("open error on socket");
        exit(1);
    }
 
    //set socket information
    myAddr.sin_family = AF_INET;
    myAddr.sin_addr.s_addr = INADDR_ANY;
    myAddr.sin_port = htons(portNum);
    //bind, exit on error
    if (bind(listenDesc, (struct sockaddr *) &myAddr, sizeof(myAddr)) < 0) {
        perror("bind error");
        exit(1);
    }
 
    //listen for new connections, up to 20 can be queued at a time
    listen(listenDesc,20);


here's an excerpt of how I'm writing to the socket in the server

if(write(cfd->connection,"?\0",2)<0){
        perror("Server: write error");
        cfd->~clientConnection();
        close(cfd->connection);
        delete cfd;
        return(0);
    }

I've done some testing with a dummy client in c and I can communicate fine with the server.

what I'm trying to send over the socket is essentially a char array
for example "3 2 string 4 "

here's how I'm setting up the java client
 DataInputStream Sinput;                // to read the socker
 
        DataOutputStream Soutput;        // towrite on the socket
 
        Socket socket;

 
                       socket = new Socket(InetAddress.getByName(ip), port);
                       socket.setKeepAlive(true);

 
                        Sinput  = new DataInputStream(socket.getInputStream());
 
                        Soutput = new DataOutputStream(socket.getOutputStream());


example of how I'm reading from socket in client
String response = new String();
                    while(Sinput.available()>0)
                        response+=Sinput.readChar();
                        //String response = bArray.toString();
                        System.out.print("message recieved\n");
                        System.out.print(response);

essentially I need to take what the c server sends and get it into a string on the java client what am I doing wrong? I currently print the response variable to screen and it looks like random characters. Also I get a string index out of range error.