简体   繁体   English

C 套接字中的 recv() 为某些请求返回 0

[英]recv() in C socket returns 0 for some requests

I have an issue with recv() and read() methods as well, both return 0 byte for some of my requests.我对 recv() 和 read() 方法也有问题,对于我的一些请求,它们都返回 0 字节。 On the other hand, I made a java application to do the same, and it always get me the response from the server, I mean the issue only happens with my C application, but not happen on Java.另一方面,我做了一个 java 应用程序来做同样的事情,它总是让我得到服务器的响应,我的意思是这个问题只发生在我的 C 应用程序上,而不会发生在 Java 上。 So please revise my below main method for that.所以请修改我下面的主要方法。

#include <stdio.h>
#include <string.h>
#include <sys/socket.h> 
#include <stdlib.h> 
#include <netinet/in.h> 
#include <string.h> 
#include <limits.h>
#define PORT 7142 


typedef unsigned char BYTE;

//function to convert string to byte array
void string2ByteArray(char* input, BYTE* output)
{

    int loop;
    int i;

    loop = 0;
    i = 0;

    while (input[loop] != '\0')
    {
        output[i++] = (unsigned char)input[loop++];

    }
}


int toDigit(char ch, int index) {
    int digit;
    if (ch >= '0' && ch <= '9')
        digit = ch - '0';
    if (ch >= 'A' && ch <= 'F')
        digit = ch - 'A' + 10;
    if (ch >= 'a' && ch <= 'f')
        digit = ch - 'a' + 10;

    if (digit == -1) {
        // throw new Exception("Illegal hexadecimal character " + ch + " at index " + index);
        return -1;
    }
    return digit;
}


void ToHex(BYTE data[], BYTE out[])
{
    int len = strlen(data);
    int i, j, f;
    if ((len & 0x01) != 0) {
        //throw new Exception("Odd number of characters.");
    }

    // two characters form the hex value.
    for (i = 0, j = 0; j < len; i++) {
        f = toDigit(data[j], j) << 4;
        j++;
        f = f | toDigit(data[j], j);
        j++;

        out[i] = (f & 0xFF);


    }

}


void encodeHex(int data[], char output[1024], int len) {
    int l = len, i = 0, j = 0;
    char out[l << 1];
    char DIGITS_LOWER[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    // two characters form the hex value.

    for (i = 0, j = 0; i < l; i++) {
        out[j++] =  DIGITS_LOWER[(unsigned int)((0xF0 & data[i]) >> 4)];
        out[j++] =  DIGITS_LOWER[(unsigned int)(0x0F & data[i])];

    }

    out[j] = '\0';
    strcpy(output, out);
}

int main(){
    char ascii_str[] = "Hello world!";
    int i;
    struct sockaddr_in address;
    int sock = 0, sRet, rdret = 0;
    struct sockaddr_in serv_addr;
    char sbuffer[] = "005a600003000001006038048020C090001663917561001880293100001701091820291201002100346391756100188029D2301521107398300f32323230303737373232323030303037202020202020203933385a4f1aceb81b09d7";
    int len = strlen(sbuffer);
    BYTE arr[len], arr1[len], bufferbyte[1024];
    char buffer[1024] ="";
    int tempBuffer[1024] = { 0 };
    char Recv[1024] = "";
    int* ptr;

    string2ByteArray(sbuffer, arr);

    ToHex(arr, arr1);

    if ((sock =  socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("\n Socket creation error \n");
        return -1;
    }

    memset(&serv_addr, '0', sizeof(serv_addr));

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);

    if (inet_pton(AF_INET, "10.16.4.212", &serv_addr.sin_addr) <= 0)  
    {
        printf("\nInvalid address/ Address not supported \n");
        return -1;
    }

    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
        printf("\nConnection Failed \n");
        return -1;
    }

    sRet = send(sock, arr1, strlen(sbuffer), 0);
    printf("Hello message sent %d\n", sRet);

    rdret = recv(sock, buffer, 100,0);

    printf("Recieved Size1: %d\n", rdret);

    {

        int i = 0;
        int j = 0;

        for (i = 0; i <= rdret; i++)
        {
            if (buffer[i] == 0)
            {
                buffer[i] = buffer[i] + 256;

            }
            tempBuffer[j] = buffer[i];

            ++j;

        }
                encodeHex(tempBuffer, Recv, j - 1);

        printf("\nRecieved Data: %s\n", Recv);


    }

    return 0;
}

When recv or read returns 0 it means that the other end have nicely closed its connection, and you should do the same.recvread返回0 ,表示另一端已经很好地关闭了它的连接,您也应该这样做。

This is well-documented behavior and any socket book or tutorial should have mentioned it.这是有据可查的行为,任何套接字书籍或教程都应该提到它。

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

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