简体   繁体   English

从Java发送int到C

[英]Sending int from java to c

I'm trying to send 5 ints from a java server to ac client. 我正在尝试从Java服务器向ac客户端发送5个整数。

Here is my java code : 这是我的Java代码:

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 : 这是我的C代码:

// 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 : c客户端第一次发出请求时,还可以:

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. 而且我的Java服务器因“连接重置”错误而崩溃。 The two program run in localhost on the same computer, port 8080. 这两个程序在同一台计算机的本地主机上运行,​​端口为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 ! 非常感谢 !

In your C program, please replace: 在您的C程序中,请替换为:

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. Java程序以网络字节顺序发送整数,这需要转换为主机字节顺序。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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