TCP/IP 在 C 中不工作

TCP/IP in C not working

我无法使客户端和服务器之间的 TCP/IP 连接正常工作。

这是服务器代码 -

int main() {
    int servSock; /* Socket descriptor for server */
    int clntSock; /* Socket descriptor for client */
    unsigned short echoServPort; /* Server port */
    struct sockaddr_in echoServAddr; /* Local address */
    struct sockaddr_in echoClntAddr; /* Local address */
    pid_t processID; /* Process ID from fork()*/
    unsigned int childProcCount = 0; /* Number of child processes */
    unsigned int clntLen;
    unsigned int recvMsgSize;

        echoServPort = 22;
        if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
            printf("Socket failed");
        }

        echoServAddr.sin_family = AF_INET;  //Internet address family
        echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY);  //Any incoming interface
        echoServAddr.sin_port = htons(echoServPort); // Local port

        if (bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0){
            printf("bind failed");
        }

        if (listen(servSock, MAXPENDING) < 0){
            printf("listen() failed");
        }

        clntLen = sizeof(echoClntAddr);

        if ((clntSock=accept(servSock,(struct sockaddr *)&echoClntAddr,&clntLen))<0){
            printf("accept() failed");
        }

        if ((recvMsgSize = recv(clntSock, buf, 1024, 0)) < 0){
            printf("recv() failed");
        }

        /* Send received string and receive again until end of transmission */
        while (recvMsgSize > 0) { /* zero indicates end of transmission */


            printf("%d", recvMsgSize);

        if (send(clntSock, buf, recvMsgSize, 0) != recvMsgSize){
            //printf(“send() failed”);
        }

        if ((recvMsgSize = recv(clntSock, buf, 1024, 0)) < 0){
            //printf(“recv() failed”);
        }

        }

        sleep(60);

    }
}
return EXIT_SUCCESS;
}

和客户端代码,这是一个 CGI 应用程序。

int main(void) {

int servSock; /* Socket descriptor for server */
int clntSock; /* Socket descriptor for client */
unsigned short echoServPort; /* Server port */
struct sockaddr_in echoServAddr; /* Local address */
struct sockaddr_in echoClntAddr; /* Local address */

struct sockaddr_in {
        __uint8_t       sin_len;
        sa_family_t     sin_family;
        in_port_t       sin_port;
        struct  in_addr sin_addr;
        char            sin_zero[8];
};
/*pid_t processID;  Process ID from fork()
unsigned int childProcCount = 0;  Number of child processes
unsigned int clntLen;*/
//char echoservIP = "10.0.0.2";
printf("Content-type: text/html\n\n");
puts("<HTML>");
puts("<BODY>");
echoServPort = 22;
servSock = 22;
clntSock = 22;

puts("<br>");

if ((clntSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
    printf("socket() failed");
}

echoServAddr.sin_family = AF_INET;  //Internet address family
echoServAddr.sin_addr.s_addr = inet_addr("10.0.0.2");  //Server IP address
echoServAddr.sin_port = htons(echoServPort);  //Server port

echoClntAddr.sin_addr.s_addr = inet_addr("10.0.0.1");


if (connect(clntSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0){
    printf("connect() failed\n");

}

int clntLen;

clntLen = sizeof(echoClntAddr);

if ((clntSock=accept(servSock,(struct sockaddr *)&echoClntAddr,&clntLen))<0){
    printf("accept() failed\n");
}

char echoString[4] = "helo";

int echoStringLen;

echoStringLen = strlen(echoString);  //Determine input length
 //Send the string to the server
if (send(clntSock, echoString, echoStringLen, 0) != echoStringLen){
    printf("send() sent a different number of bytes than expected");
}

puts("<p>Hello <b>CGI</b</p>");
puts("</BODY>");
puts("</HTML>");


return EXIT_SUCCESS;
}

调试时,问题出现在客户端

if ((clntSock=accept(servSock,(struct sockaddr  
  *)&echoClntAddr,&clntLen))<0){
    printf("accept() failed\n");
 }

然后在

if(send(clntSock, echoString, echoStringLen, 0) != echoStringLen){
    printf("send() sent a different number of bytes than expected");
}

我得到输出

<HTML>
<BODY>
<br>
accept() failed
send() sent a different number of bytes than expected<p>Hello      
<b>CGI</b</p>

需要帮助解决这个问题!谢谢。

如果 accept 失败,您不应继续对套接字进行其他操作,例如 recv。 TCP 连接不适合您在服务器和客户端之间进行数据交换。您需要处理错误情况而不会失败。

在客户端,您在 send 上有一条自定义错误消息。那没有帮助。通常,部分发送并不常见。因此,您的打印“发送的字节数与预期的不同”可能会产生误导。你需要找到真正的原因。

不需要在客户端代码中调用 accept。它是接受连接的服务器和连接

的客户端

在您的所有系统调用上使用 errnoperror 之类的 perror("Accept Failed") 以便于调试并删除自定义打印

您无需在客户端上调用 accept() - 您只需连接即可。

char echoString[4] = "helo";

错了。它不是以 NUL 结尾的。就这样

char echoString[] = "helo";