c - 为什么 Connect() 返回负值?

标签 c sockets client client-server

#include<io.h>
#include<stdio.h>
#include<winsock2.h>
#include <ctype.h>
#include<string.h>
#include<strings.h>

#define MY_PORT     8989 //defining the port for the socket
#define MAXBUF      256



int main(int argc , char *argv[])
{
    //char str[MAXBUF];
    int a;
    WSADATA wsa;

    SOCKET sockfd , clientfd; //SOCKET is a data type. We initialize two variables of the data type Socket here
    struct sockaddr_in self; //structure for the family,port and IP address of the socket (Socket descriptors)
    char buffer[MAXBUF]; // this is a character array that will receive the message from the client and we will use this to manipulate
    //char message[MAXBUF];
    printf("\nInitialising Winsock...");


    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) //WSASTARUP is used to tell windows to get ready for a connection and if it returns a value 0, windows is ready
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }


    printf("Initialised.\n");

    /*---create streaming socket---*/
    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) //socket is created using a function called socket
                                                        //using AF_INET means that we are using TCP/IP family
                                                          //the if statement here checks whether or not the value returned by the socket is negative or not. If it is negative that means there is some sort of an error
    {
        perror("Socket");
        exit(errno);
    }
        printf("Socket created.\n");
        self.sin_family = AF_INET;
        self.sin_port = htons(MY_PORT);
        self.sin_addr.s_addr = htonl(INADDR_ANY);
        memset(&self,'\0', sizeof(self));
    /*The connect function below is used to establish a connection between the client and the server*/
    if (connect(sockfd, (struct sockaddr*)&self, sizeof(self)) <0)
        {
        printf("connection with the server failed...\n");
        exit(0);
        }
        else
        printf("connected to the server..\n");

        printf("Please enter message: ");
        memset(buffer, 0, sizeof (buffer));
        fgets(buffer, MAXBUF, stdin); //fgets is used here to get whatever is inside the buffer
        while (1)
        {
        /*struct sockaddr_in client_addr;
        int addrlen=sizeof(client_addr);*/

        /*---accept a connection (creating a data pipe)---*/

        a=  write(sockfd, buffer, sizeof(buffer));
        if(a<0){
        printf("Error");
            }
        // a= recv(clientfd, buffer, MAXBUF, 0);
        //accept(clientfd, (struct sockaddr*)&client_addr, &addrlen);

        a= read(sockfd, buffer, sizeof(buffer));
        if(a<0){
        printf("Error");
      }
        if (strncmp("QUIT", buffer, 4) == 0) {
            printf("Server Exit...\n");
            break;

        }
        }

    close(sockfd); //close the sockfd
    WSACleanup(); // windows socket is cleaned up
    return 0;
}


代码工作完全正常,但由于某种原因,我无法理解 connect 函数不断返回负值,或者至少是一个不为零的值。我与该客户端一起使用的服务器适用于其他客户端,因此我知道它没有任何问题。

我们将非常感谢您的帮助。

最佳答案

    self.sin_family = AF_INET;
    self.sin_port = htons(MY_PORT);
    self.sin_addr.s_addr = htonl(INADDR_ANY);

    memset(&self,'\0', sizeof(self));

在此代码中,您设置 self 的所有值然后你只需清除 selfmemset 。我很确定这没有任何意义,并且可能是您看到的错误的原因,即没有为 connect 给出有用的参数。 .

即使没有这个错误 memset该代码没有多大意义:您正在尝试连接到 INADDR_ANY但没有这样的IP地址可以连接。 INADDR_ANY意味着在服务器端监听机器的每个地址 - 在客户端不能使用它,而是必须使用真实的 IP 地址,例如 127.0.0.1对于本地主机。

关于c - 为什么 Connect() 返回负值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59225119/

相关文章:

java - 我的 JFrame 在运行 "while"循环后显示。我希望 JFrame 在运行时显示

javascript - 我可以将 CORS 任何地方的代码嵌入到我的 javascript/jquery 中吗?

java - 如何在java中向特定客户端发送响应?

c - 写入数据后向套接字发送错误响应

linux - 如何最有效地处理大量文件描述符?

security - 客户端密码哈希与纯文本

c - 如何编写Makefile(带有子Makfile)更简洁

c++ - 我可以在不使用标准 api 函数的情况下创建流程吗?

python - 哪种编程语言在其正式规范中具有非常短的上下文无关语法?

c - 为什么这段C代码能正确运行呢?