我从客户端程序发送到服务器程序的消息打印在客户端程序 C Linux

Message which I sent from a client program to server program prints in client program C Linux

我了解应用程序如何通过 Internet 自行运行。我编写了一个向服务器程序发送消息的客户端程序,然后该消息应该在服务器程序中打印,但此消息打印到客户端程序中。这里编码服务端程序和客户端程序,还有头文件重新定义一些类型。

编译时没有任何错误和警告,所以我没有发送编译过程。

server.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/un.h>

#include "../main_values.h"

int main()
{
    int socket_server = socket(AF_INET, SOCK_STREAM, 0);                            //create a socket server
    int opt = 1;            
    setsockopt(socket_server, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));         //set an option to socket to
                                                                                    //if we ends working with 
    sockaddr_in addr_server;                                                        //socket connected with 
    addr_server.sin_family = AF_INET;                                               //ip address, free ip
    addr_server.sin_port = htons(2251);
    addr_server.sin_addr.s_addr = inet_addr("127.0.0.222");

    int stat = bind(socket_server, (sockaddr *)&addr_server, sizeof(addr_server));  //connecting ip addr and socket
    if (stat == -1)
    {
        perror("ERROR");
    }

    sockaddr_in addr_client;                                                        //create the future client ip stucture 
    socklen_t len = 0;

    listen(socket_server, 15);                                                      //set server socket into listening
    int socket_client = accept(socket_server, (sockaddr *)&addr_client, &len);      //connecting client ip with our socket
    if (socket_client == -1)
    {
        perror("ERROR");
    }
    char msg[100];
    int res = read(socket_server, msg, sizeof(msg));                                //getting message from client
    printf("MESSAGE FROM CLIENT: %s\n", msg);                                       //printing message from client

    shutdown(socket_server, SHUT_RDWR);                                             //closing server socket

    return 0;
}

client.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/un.h>

#include "../main_values.h"

int main()
{
    int socket_client = socket(AF_INET, SOCK_STREAM, 0);                                    //create a client socket
    int opt = 1;                                                                            //set an option like in a server.c
    setsockopt(socket_client, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));               

    sockaddr_in addr_client;                                                                //making client ip structure
    addr_client.sin_family = AF_INET;
    addr_client.sin_port = htons(2250);
    addr_client.sin_addr.s_addr = htons(INADDR_ANY);

    int stat = bind(socket_client, (sockaddr *)&addr_client, sizeof(addr_client));          //connecting ip and client socket
    if (stat == -1)
    {
        perror("ERROR");
    }

    sockaddr_in addr_server;                                                                //creating a server ip structure
    addr_server.sin_family = AF_INET;
    addr_server.sin_port = htons(2251);
    addr_server.sin_addr.s_addr = inet_addr("127.0.0.222");

    int socket_server = connect(socket_client, (sockaddr *)&addr_server, sizeof(addr_server));
    if (socket_server == -1)                                                                //connecting to server
    {
        perror("ERROR");
    }
    char msg[] = "HELLO WORLD!!!!!";                                                        //excample message
    int res = write(socket_server, msg, sizeof(msg));                                       //sending a message to server

    shutdown(socket_client, SHUT_RDWR);

    return 0;
}

main_values.h

#pragma once

typedef struct sockaddr sockaddr;
typedef struct sockaddr_in sockaddr_in;

terminal_1

ch@ch:~/Рабочий стол/mainc.4/my_server$ ./server.out
MESSAGE FROM CLIENT: 
ch@ch:~/Рабочий стол/mainc.4/my_server$ 

terminal_2

ch@ch:~/Рабочий стол/mainc.4/my_server$ ./client.out
HELLO WORLD!!!!!ch@ch:~/Рабочий стол/mainc.4/my_server$  
int socket_server = connect(socket_client, (sockaddr *)&addr_server, 
...
int res = write(socket_server, msg, sizeof(msg));

如果连接成功,connect的return代码为0。因此 socket_server 为 0。这意味着对文件描述符 0 的 write 已完成,这相当于 stdin,因此导致写入终端。

正确的代码是写入 socket_client,而不是 socket_server。命名表明 connect return 存在一些混淆,即它 return 是状态而不是新套接字。详情请见the documentation for connect