为什么Connect()返回负值?

问题描述 投票:-1回答:1
#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函数上,因此总是返回负值,或者至少返回一个不为零的值。我与此客户端一起使用的服务器可用于其他客户端,因此,我知道它没有任何问题。

非常感谢您的帮助。

c sockets client client-server
1个回答
0
投票
    self.sin_family = AF_INET;
    self.sin_port = htons(MY_PORT);
    self.sin_addr.s_addr = htonl(INADDR_ANY);

    memset(&self,'\0', sizeof(self));
© www.soinside.com 2019 - 2024. All rights reserved.