gpt4 book ai didi

c - 段错误 11 - 网络程序

转载 作者:太空宇宙 更新时间:2023-11-04 06:36:43 25 4
gpt4 key购买 nike

<分区>

这是我第一次尝试编写网络程序。它编译没有问题,但是当我运行它时,它给了我一个“segmentation fault 11”。我希望有人能告诉我代码中的哪些内容可能导致此问题。

我是 C 的新人。

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netdb.h>
#include <netinet/in.h>

int main()
{
//////////// GETADDRINFO \\\\\\\\\\\\\\\\

struct addrinfo hints, *res;
memset(&hints, 0 , sizeof hints);

hints.ai_family = AF_UNSPEC; //define the 'hints' strucure
hints.ai_socktype = SOCK_STREAM; //define the 'hints' strucure
hints.ai_protocol = AI_PASSIVE; //define the 'hints' strucure

char *ipaddr = "127.0.0.1";
getaddrinfo(ipaddr, "ftp", &hints, &res);

////////////// SOCKET \\\\\\\\\\\\\\\\

int sock = socket(AF_INET, SOCK_STREAM, 0);

struct sockaddr_in info;
int bnd = bind( sock , res->ai_addr, res->ai_addrlen);

info.sin_family = AF_INET;
info.sin_port = htons(10000);
info.sin_addr.s_addr = INADDR_ANY;
bzero(&(info.sin_zero),8);


/////////////// CONNECT \\\\\\\\\\\\\\\\\\\\\\

int conct = connect( sock, res->ai_addr, res->ai_addrlen);

////////////// LISTEN \\\\\\\\\\\\\\\\\\

int lstn = listen( sock , 4 );

////////////// ACCEPT \\\\\\\\\\\\\\\\\\\\\\\

int new_fd;
struct sockaddr_storage client_addr;
socklen_t addr_size;
addr_size = sizeof(client_addr);
new_fd = accept(sock, (struct sockaddr *)&client_addr, &addr_size);

///////////////// SEND \\\\\\\\\\\\\\\\\\\\\\

char *msg = "HI!";
int len, bytes_sent;
len = strlen(msg);
bytes_sent = send(sock, msg, len, 0);

//////////////// RECEIVE \\\\\\\\\\\\\\\\\

char burr[254];
int cat = sizeof(burr);

recv( sock, burr , cat , 0 );

printf("%s", burr);
return 0;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com