开发者

Trouble writing to streams with sockets

开发者 https://www.devze.com 2023-02-28 03:40 出处:网络
I\'m quite new to C and am having trouble with writing to streams using sockets. I\'m using a network stream/socket to try to send messages in two directions (from host to client and client to host)

I'm quite new to C and am having trouble with writing to streams using sockets.

I'm using a network stream/socket to try to send messages in two directions (from host to client and client to host) however when I get the message back from the client it's scrambled (due to an encoding issue I guess?).

Here's the relevant parts of my code:

host.c

int client_sock_fd;

int main(int argc, const char* argv[]) {
    while (1) {
        char input[80];
        scanf("%s", input);
        // send to client, transmitted fine
        write(client_sock_fd, &input, sizeof(input));             

        char response[80];
        read(client_sock_fd, &response, sizeof(response));
        // Prints the missing character symbol, and 'random' letters
        printf("Response: %s\n", response);
    }
}

client.c

int host_sock_fd; // file descriptor fo开发者_如何学运维r host

void writeResponse(

int main(int argc, const char* argv[]) {
    while (1) {
        char message[80];
        // the message is transmitted fine
        read(host_sock_fd, &message, sizeof(message));

        writeResponse("Response message");
    }
}

void writeResponse(char response[80]) {
    write(host_sock_fd, &response, sizeof(response));
}

I'm not really sure how to continue. I know if I simply write back the received message in client it works fine, so is this a problem with the amount of memory for each char?


I showed a lack of knowledge below, and I'm not going to delete it, but this is what causeing your problem in writeResponse(char response[80]) since response is treated as char * and not char[80]. But in general, don't use the & when it is not needed, it causes mistakes just as the one you had. treat char[] as char * where it is about the level of referencing, there are differences between those two, but not in this case.

It's weird that it works at all, since you declared your buffers as char input[80]; etc., those variables are already char pointers and you should not pass their address as parameter to the read /write functions so it should be:

write(client_sock_fd, input, sizeof(input)); 

instead of:

write(client_sock_fd, &input, sizeof(input)); 

in all cases of read write. You may want to take a look at those examples


First of all I can see you are ignoring the return code of write(2) and read(2). Be aware that there is not guarantee related to the number of octets actually sent / received when using these calls.

If a function be advertised to return an error code in the event of difficulties, thou shalt check for that code, yea, even though the checks triple the size of thy code and produce aches in thy typing fingers, for if thou thinkest ``it cannot happen to me'', the gods shall surely punish thee for thy arrogance.

You should check the size_t returned and keep reading until EOF (return 0 says read(2)).


Apart from MByDs answer you are also passing a string of 17 characters (inluding \0) to the writeResponse function and then claiming it is actually 80 4.

Better:

void writeResponse(const char* response) {
    write(host_sock_fd, response, strlen(response) + 1);
}

Update: Fell into the array trap as well. Note that

void foo(char *bar)

and

void foo(char bar[80])

are equivalent and sizeof(bar) will return the size of a char pointer in both cases.

0

精彩评论

暂无评论...
验证码 换一张
取 消