java - 从java发送int到c

标签 java c sockets tcp int

我正在尝试从 Java 服务器向 C 客户端发送 5 个整数。

这是我的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();
 }
  }
}

这是我的 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 客户端第一次发出请求还不错:

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

但是第二次(没有关闭服务器)我得到了这个:

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端口。

这几天我一直在努力解决这个问题,但我真的一无所知。 你们有什么建议吗?

非常感谢!

最佳答案

在您的 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");
}

与:

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++;
}   

这样做是为了确保正确接收服务器发送的所有数据包。

更新:添加代码以确认下方收到的数据。 Java 程序以网络字节顺序发送整数,这需要转换为主机字节顺序。

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

关于java - 从java发送int到c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30003777/

相关文章:

java - Android 上的错误 java.lang.NoClassDefFoundError : org. firebirdsql.jdbc.FBDataSource

使用逻辑运算符的c程序

c - 查找数组中的元素

c# - 如何检查远程端口是否打开

c - 线程 C 客户端-服务器聊天应用程序中 SIGSEGV 的未知原因

ios - 使用 CFStreamCreatePairWithSocketToHost 建立 IPV6 套接字连接

Java - 打印分割字符串的几个不同部分

java - 当我可以通过引用子类访问所有方法时,为什么还要引用基类?

java - 我们可以使用反射获取本地类吗?

c - pthread条件变量只能使用一次吗?