我试图让我的脚湿透网络编程。我创建了一个服务器,它接受来自 Web 客户端的连接。
在某个时候(我不记得是什么时候)它坏了,对于我的生活,我无法修复它。
至少有人可以为我提供一些指示吗?(没有双关语的意思)
我已经尝试了所有内容,阅读了整个网络,包括gnu c 库 文档(我在 linux 上),但仍然没有运气。
编辑:问题描述:我的客户端软件设法连接,但是服务器软件没有报告,并且似乎卡在了 accept() 函数中。
我的代码:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "lib/error.h"
#include <string.h>
#include <sys/types.h>
#include <time.h>
int main(int argc, char *argv[])
{
int socketfd = 0, connfd = 0; // socket, and connection file descriptors
struct sockaddr_in serv_addr; // struct that will get filled with host info
struct sockaddr_in cli_addr; // struct that will get filled with peer info
int status; // Used for various status controling and error messaging throughout the program
char sendBuff[1025]; // buffer to allow for storage of items to be sent
socklen_t cli_size = sizeof(cli_addr);
time_t ticks;
socketfd = socket(AF_INET, SOCK_STREAM, 0); // Creating a socket
if (socketfd == -1) error("Failed to create socket.", 4);
memset(&serv_addr, '0', sizeof(serv_addr)); // zeroing out the location of serv_addr
memset(&cli_addr, '0', sizeof(cli_addr)); // zeroing out the location of serv_addr
memset(sendBuff, '0', sizeof(sendBuff)); // zeroing out the locaiton of sendBuff
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
status = bind(socketfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
if (status == -1) error("Failed to bind socket.", 4);
listen(socketfd, 10);
while(1)
{
connfd = accept(socketfd, (struct sockaddr *) &cli_addr, (socklen_t *) &cli_size);
if (connfd == -1) error("Failed during accepting the peer connection at socket.", 3);
printf("Client connected: %d:%d", cli_addr.sin_addr.s_addr, cli_addr.sin_port);
close(connfd);
sleep(1);
}
return 0;
}