Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C bind()函数根本没有执行,测试时没有输出_C_Linux_Sockets - Fatal编程技术网

C bind()函数根本没有执行,测试时没有输出

C bind()函数根本没有执行,测试时没有输出,c,linux,sockets,C,Linux,Sockets,我正在学习C语言中的套接字编程。我已经让我的服务器创建了一个成功的套接字,但是当我尝试将套接字绑定到端口时,什么都没有发生。没有发生错误,也没有成功。这就好像bind()函数根本没有执行一样 我已经查看了关于bind()函数的文档,但是没有提到为什么它根本不会执行。我也尝试过在这个网站上搜索,但没有结果。 我也试着从头到尾地学习教程,但错误(或缺少错误)仍然存在 以下是导致问题的完整代码: #include <stdlib.h> #include <string.h> #

我正在学习C语言中的套接字编程。我已经让我的服务器创建了一个成功的套接字,但是当我尝试将套接字绑定到端口时,什么都没有发生。没有发生错误,也没有成功。这就好像
bind()
函数根本没有执行一样

我已经查看了关于
bind()
函数的文档,但是没有提到为什么它根本不会执行。我也尝试过在这个网站上搜索,但没有结果。 我也试着从头到尾地学习教程,但错误(或缺少错误)仍然存在

以下是导致问题的完整代码:

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

int main() {

    // Descriptors. Used to check the status of functions such as socket, listen, bind etc.
    // If a descriptor is equal to 0, then everything is okay. Else, if they are equal to -1, something went wrong.
    int socketDescriptor, newSocketDescriptor = 1;

    // The process ID of a child process (the client) when a new one is spawned (the client connects).
    pid_t childPID;

    // A string to hold the commands being sent a received.
    char* commandBuffer = calloc(BUFFER_SIZE, sizeof(char));

    // A structure to hold information on the server address.
    struct sockaddr_in serverAddress;
    memset(&serverAddress, '\0', sizeof(serverAddress));

    // Fill in the server address information.
    // Set the address family to AF_INET, which specifies we will be using IPv4.
    // htons() takes the given int and converts it to the appropriate format. Used for port numbers.
    // inet_addr() takes the given string and converts it to the appropriate format. Used for IP addresses.
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = htons(PORT);
    serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");

    // A structure to hold information a client when a new one connects to this server.
    struct sockaddr_in clientAddress;
    memset(&clientAddress, '\0', sizeof(clientAddress));

    // socklen_t defines the length of a socket structure. Need this for the accept() function.
    socklen_t addressSize;

    // Creating the socket.
    // AF_NET specifies that we will be using IPv4 addressing.
    // SOCK_STREAM specifies that we will be using TCP to communicate.
    socketDescriptor = socket(AF_INET, SOCK_STREAM, 0);

    if (socketDescriptor < 0) {
        perror("ERROR CREATING SOCKET");
        exit(1);
    }
    else
        printf("Socket created successfully.\n");

    // Binding to the specified port. 0 if everything is fine, -1 if there was an error.
    if (bind(socketDescriptor, (struct sockaddr*) & serverAddress, sizeof(struct sockaddr_in)) < 0) {
        perror("ERROR BINDNING");
        exit(1);
    }
    else
        printf("Socket bound to %s:%s.\n", serverAddress.sin_addr.s_addr, serverAddress.sin_port);
#包括
#包括
#包括
#包括
#包括
#包括
#包括


我不知所措。

服务器套接字不会显示在netstat列表中,除非绑定套接字后调用
listen

另外,在调用
serverAddress.sin\u addr.s\u addr
serverAddress.sin\u port
之后,您在
printf
中使用了
%s
格式说明符。这些不是字符串,而是整数。使用错误的格式说明符调用并可能导致程序崩溃。使用正确的格式说明符,如
%d
%x
将解决此问题

if (bind(socketDescriptor, (struct sockaddr*)&serverAddress, sizeof(struct sockaddr_in)) < 0) {
    perror("ERROR BINDNING");
    exit(1);
}
else
    // use %x to print instead
    printf("Socket bound to %x:%x.\n", serverAddress.sin_addr.s_addr, serverAddress.sin_port);

if (listen(socketDescriptor, 3) < 0) {
    perror("listen failed");
} else {
    printf("socket is listening\n");
}
if(绑定(socketDescriptor,(struct sockaddr*)&serverAddress,sizeof(struct sockaddr_in))<0){
perror(“错误绑定”);
出口(1);
}
其他的
//改用%x打印
printf(“绑定到%x:%x的套接字。\n”,serverAddress.sin\u addr.s\u addr,serverAddress.sin\u端口);
if(侦听(socketDescriptor,3)<0){
perror(“倾听失败”);
}否则{
printf(“套接字正在侦听\n”);
}

您的代码看起来不完整;您没有定义
端口
。您在
printf()
格式字符串中使用
%s
,但
s\u addr
sin\u端口
都不是字符串。你的程序可能会崩溃。@danadam我有我的代码来正确格式化这些值,这就解决了它。谢谢这就是答案,非常感谢!我将最后一行更改为
printf(“绑定到%s的套接字:%d.\n”,inet\u ntoa(serverAddress.sin\u addr,ntohs(serverAddress.sin\u port))
,它工作正常。我没有正确转换这些值