从java发送int到c [英] Sending int from java to c
问题描述
我正在尝试从java服务器向ac客户端发送5个int。
I'm trying to send 5 ints from a java server to a c client.
这是我的java代码:
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();
}
}
}
这是我的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);
c客户第一次提出请求时有点好处:
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
我的java服务器因连接重置错误而崩溃。
这两个程序在同一台计算机上的localhost上运行,端口8080。
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 ?
非常感谢!
推荐答案
在你的C程序中,请替换:
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");
}
使用:
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.
更新:添加代码以确认下面收到的数据。 Java程序以网络字节顺序发送整数,这需要转换为主机字节顺序。
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]));
}
这篇关于从java发送int到c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!