使用C语言的套接字发送和接收消息?

问题描述 投票:-1回答:1

我正在处理一个与Socket有关的学校作业,在那里我有一个客户端和一个服务器。我的客户端出现问题,我没有收到我想要服务器发送的内容。现在,我不确定是否是因为我需要一个int send(int sockfd,const void * buff,int len,int flag);在服务器端,以便我的客户端接收消息,也对客户端执行相同操作并进行接收。

我不确定这是否是我要让我的客户接收到的东西,但这就是我的代码所需要的,我正在func(sockfd)方法上完成所有这些工作。

/**
 * Build a struct to send the base pair information from the server to the client. 
 * Print the counts on the server side, then convert to network (htons). 
 * The client should receive the information (htonl) and simply print the values to the screen. 
 * Verify the counts match between the client and the server. 
 */

服务器:

#define MAX 256 
#define PORT 9064 
#define SA struct sockaddr 

typedef struct results
{
        unsigned short as;
        unsigned short cs;
        unsigned short gs;
        unsigned short ts;
} results;

// Function designed for chat between client and server. 
void func(int sockfd)
{
    char buff[MAX];
    int n, i, j;
        struct results result;
        result.as = 0;
        result.cs = 0;
        result.gs = 0;
        result.ts = 0;
    // infinite loop for chat 
    for (j = 0; j < 1; j++) {
        bzero(buff, MAX);

        // read the message from client and copy it in buffer 
        read(sockfd, buff, sizeof(buff));
        for(i = 0; i < strlen(buff); i++)
        {
                if(buff[i] == 'A')
                {
                        result.as++;
                }
                if(buff[i] == 'C')
                {
                        result.cs++;
                }
                if(buff[i] == 'G')
                {
                        result.gs++;
                }
                if(buff[i] == 'T')
                {
                        result.ts++;
                }
        }
printf("From client: %s\t To client : \nOutput: A: %d, C: %d, G: %d, T: %d ", buff, result.as, result.cs, result.gs, result.ts);
result.as = htons(result.as);
result.cs = htonl(result.cs);
result.ts = htons(result.ts);
result.gs = htons(result.gs);
// print buffer which contains the client contents 
bzero(buff, MAX);
    n = 0;
    // copy server message in the buffer 
    while ((buff[n++] = getchar()) != '\n')
        ;
    // and send that buffer to client 
    write(sockfd, buff, sizeof(buff));

    // if msg contains "Exit" then server exit and chat ended. 
    if (strncmp("exit", buff, 4) == 0) {
        printf("Server Exit...\n");
        break;
    }
 }
}

在客户端:

#define MAX 256 
#define PORT 9064 
#define SA struct sockaddr 
typedef struct results
{
        unsigned short as;
        unsigned short cs;
        unsigned short gs;
        unsigned short ts;
} results;
void func(int sockfd)
{
    char buff[MAX];
    int n, j;
    struct results result;
    for (j = 0; j < 1; j++)
    {
        bzero(buff, sizeof(buff));
        printf("Enter the string : ");
        n = 0;
        while ((buff[n++] = getchar()) != '\n')
                ;
        write(sockfd, buff, sizeof(buff));
        bzero(buff, sizeof(buff));
        read(sockfd, &buff, sizeof(buff));

        result.as = htons(result.as);
        result.cs = htonl(result.cs);
        result.gs = htonl(result.gs);
        result.ts = htonl(result.ts);
        bzero(buff, MAX);
        if ((strncmp(buff, "exit", 4)) == 0) {
            printf("Client Exit...\n");
            break;
        }
    }
}

让我知道。

c sockets server client send
1个回答
0
投票

我们正在使用unsigned short数据。因此,当服务器将数据从主机写入网络时,我们使用htons()转换整数。

result.as = htons(result.as);
result.cs = htons(result.cs);
result.ts = htons(result.ts);
result.gs = htons(result.gs);

write(sockfd, &result, sizeof(result));

[当客户端从网络读取数据到主机时,我们使用ntohs()转换整数。

read(sockfd, &result, sizeof(result));

result.as = ntohs(result.as);
result.cs = ntohs(result.cs);
result.gs = ntohs(result.gs);
result.ts = ntohs(result.ts);
© www.soinside.com 2019 - 2024. All rights reserved.