Cassifiore
Cassifiore

Reputation: 89

Sending int from java to c

I'm trying to send 5 ints from a java server to a c client.

Here is my java code :

class Server {
public static void main(String args[]) throws Exception {
 ServerSocket welcomeSocket = new ServerSocket(8080);

 while(true)
 {
    Socket connectionSocket = welcomeSocket.accept();

    System.out.println("welcomeSocket.accept() called");
    DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());

    outToClient.writeInt(1);
    outToClient.writeInt(2);
    outToClient.writeInt(3);
    outToClient.writeInt(4);
    outToClient.writeInt(5);
    outToClient.close();
    connectionSocket.close();
 }
  }
}

Here is my c code :

// the function below is made by made, it creates and return a socket
// AF_INET witch a tcp protocol
int sock = socketClient("localhost",8080);
if (sock < 0) { 
    printf("client : erreur socketClient\n");
    exit(2);
}

char intBufferCoupReq[20];

int data = recv(sock, intBufferCoupReq, 80, 0);
printf("data recieved : %d\n",data);
if( data == -1){
    printf("Error while receiving Integer\n");
}

char intBufferCoupReq2[5][4];

int cpt;
int j;
int i = j = 0;

// in this loop I divide my big array of 5 ints into 5 differents
// array to use with ntohl
for(cpt = 0; cpt < 20 ; cpt++){
    if(cpt%5==0) i=0;
    if(j%4==0) j=0;

    intBufferCoupReq2[i][j] = intBufferCoupReq[cpt];
    i++;
    j++;


}

int receivedInt[5];
for(cpt=0;cpt<5;cpt++){
    printf("int n°%d = %d\n",cpt+1,ntohl(*((int *) &intBufferCoupReq2[cpt])));  
}


close(sock);

The first time the c client make a request it is kinda fine :

data recieved : 20
int n°1 = 4
int n°2 = 3
int n°3 = 2
int n°4 = 1
int n°5 = 5

But the second time ( without shutting the server down ) I get this :

data recieved : 8
int n°1 = 16384
int n°2 = -1610612736
int n°3 = 11010050
int n°4 = -1342118655
int n°5 = 524519

And my java server crash with a " connection reset " error. The two program run in localhost on the same computer, port 8080.

I've been trying to figure this out for a few days but i am really clueless. Do any of you guys got a piece of advice ?

Thanks a lot !

Upvotes: 1

Views: 1512

Answers (1)

tivn
tivn

Reputation: 1923

In your C program, please replace:

char intBufferCoupReq[20];

int data = recv(sock, intBufferCoupReq, 80, 0);
printf("data recieved : %d\n",data);
if( data == -1){
    printf("Error while receiving Integer\n");
}

With:

char intBufferCoupReq[1024];
memset(intBufferCoupReq, '\0', sizeof(intBufferCoupReq));

int k = 0;
while ( 1 ) { 
    int nbytes = recv(sockfd, &intBufferCoupReq[k], 1, 0); 
    if ( nbytes == -1 ) { printf("recv error\n"); break; }
    if ( nbytes ==  0 ) { printf("recv done\n"); break; }
    k++;
}   

This is done to make sure that all packets sent by server are received properly.

Update: added code to confirm data received below. Java program send the integers in network-byte-order, this need to be converted to host-byte-order.

int *myints = (int*) intBufferCoupReq;
int i = 0;
for ( i=0; i<(k/4); i++ ) {
    printf("myints[%d]=%d\n", i, ntohl(myints[i]));
}

Upvotes: 2

Related Questions