简体   繁体   English

C套接字(Linux)无法连接

[英]C sockets (Linux) won't connect

recently I've been trying to do a lot with C sockets as it is a language I need for future use. 最近,我一直在尝试使用C套接字做很多事情,因为这是我将来需要使用的语言。 However I've encountered this problem I can't manage to solve. 但是,我遇到了无法解决的问题。 I made both client and server - unfortunately the two binaries refuse to connect in between each other. 我同时创建了客户端和服务器-不幸的是,两个二进制文件拒绝彼此连接。 What I mean is that I do not get any error whatsoever when I run the server binary and client binary in a same machine, but no connection either. 我的意思是,当我在同一台计算机上运行服务器二进制文件和客户端二进制文件时,无论如何我都不会收到任何错误,但是也没有连接。 Any ideas? 有任何想法吗? Here is the code! 这是代码!

(Ubuntu, both C codes compiled with gcc, server and client code ran within the same machine in 2 different terminals.) (Ubuntu,使用gcc编译的C代码,服务器代码和客户端代码都在同一台计算机上的2个不同终端中运行。)

Server.c: Server.c:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h> 
    #include <string.h>
    #include <sys/socket.h>
    #include <arpa/inet.h> //inet_aton(), inet_ntoa() etc
    #include <sys/types.h>
    #include <netinet/in.h>

    #define PORT 8000
    #define ERROR perror("Something went wrong! => ");
    #define BUFFERSIZE 256
    int main()
    {
        int sockfd, client_socketfd; 
        int bytes_sent; 
        socklen_t sin_size;
        struct sockaddr_in server_addr;
        struct sockaddr_in connectedClient_addr;
        char message[BUFFERSIZE] = "Welcome to the server!";

        if((sockfd = socket(PF_INET, SOCK_STREAM ,0)) == -1) {
            ERROR;
            exit(-1);
        }

        server_addr.sin_family = AF_INET;
        server_addr.sin_port   = htons(PORT);
        server_addr.sin_addr.s_addr = INADDR_ANY; //Subject to change


        if((bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr))) == -1) {
            ERROR;
            exit(-2);
        }


        if((listen(sockfd, 5)) == -1) {
            ERROR;
            exit(-3);
        }   

        int addrlen = sizeof(connectedClient_addr);
        if((client_socketfd = accept(sockfd, (struct sockaddr *)&connectedClient_addr, (socklen_t*)&addrlen)) == -1){
            ERROR;
            exit(-4);
        }   

        printf("Got a connection from: %s at port: %d" , inet_ntoa(connectedClient_addr.sin_addr), PORT);

        if((send(sockfd, &message, BUFFERSIZE, 0)) == -1) {
             ERROR;
             exit(-5);
        }

         close(sockfd);
         close(client_socketfd);

        return 0;
    }

-- -

Client.c: Client.c:

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


#define PORT 8000
#define ERROR perror("Something went wrong! =>");
#define BUFFERSIZE 256
int main()
{
    int sockfd;
    struct sockaddr_in client_addr;
    char message[BUFFERSIZE] = "Successfully connected!";
    int bytes_received, bytes_sent;
    char buffer[BUFFERSIZE];


    if((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
        ERROR;
        exit(-1);
    }


    client_addr.sin_family = AF_INET;
    client_addr.sin_port   = htons(PORT);
    client_addr.sin_addr.s_addr = INADDR_ANY;


    if((connect(sockfd, (struct sockaddr*)&client_addr, sizeof(client_addr))) == -1) {
        ERROR;
        exit(-2);
    }

    if((bytes_received = recv(sockfd, &buffer, BUFFERSIZE, 0)) == -1) {
        ERROR;
        exit(-3);
    }

    printf("Received %d bytes:\n" , bytes_received);
    printf("%s", buffer);

    close(sockfd);

    return 0;
}

ANSWER: The issue was that I provided server socket inside send() function in server.c instead of the client socket. 解答:问题是我在server.c的 send()函数中提供了服务器套接字,而不是客户端套接字。 Therefore the code was working without any errors but the behavior was incorrect. 因此,代码可以正常工作,没有任何错误,但是行为不正确。 Credit goes to RemyLebeau in the comments who pointed this out and saved me countless hours of more struggling. RemyLebeau 在评论中指出了这一点,他指出了这一点,并为我节省了无数小时的艰苦奋斗。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM