gpt4 book ai didi

c - 为什么我的accept()套接字函数返回 "Invalid argument"errno?

转载 作者:行者123 更新时间:2023-11-30 17:34:34 34 4
gpt4 key购买 nike

当我运行此源代码时,一切正常,直到它收到客户端对无效文件名的请求,之后所有连接尝试都因此代码而失败 - 当我尝试接受时,“无效参数”。

这是用 C 语言编写的源代码。

enter code here

#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "pthread.h"
#include <errno.h>

#define BACKLOG 10

void *handle_client(void *); //Function for processing threads


int main ()
{
int sockfd, portno, tempsockfd, optval;
struct sockaddr_in serv_addr, cli_addr;
socklen_t *clilen;
pthread_t thread_id;
FILE* fp = NULL;
char buff[100];
memset(buff,0,sizeof(buff));

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("ERROR with server socket()");
exit (1);
}

puts("Socket creation was successful\n");

srand (time (NULL));

portno = (rand() % 10000) + 1;
while (portno < 1000)
{
portno = (rand() % 10000) +1;
}

printf ( "%d\n", portno);

/* Initialize socket structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);

optval = 1;

setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&optval, sizeof(optval));

puts("Attempting to bind socket...\n");

/* Binding the host ID to the socket.*/
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
perror("ERROR with server bind()\n");
exit(1);
}

puts ("Socket Bound\n");

/* Listen for clients until "BACKLOG" is reached. */
if ((listen(sockfd,BACKLOG)) != 0)
{
perror("Failure to listen, bad socket\n");
exit(1);
}

*clilen = sizeof(cli_addr);

puts ("Now listening...\n");

tempsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, (socklen_t *)&clilen);

if (tempsockfd <0)
{

printf ("BADCONNECTIOn...Error: %s\n", strerror(errno));
exit(1);
}

puts("Connection accepted");


//Get the socket descriptor
int newsockfd = tempsockfd;
char buffer[256];
int n;
int c;
int x = 0;


/* If successful connection, begin reading from file. */
bzero(buffer,256);
n = read( newsockfd,buffer,255 );
if (n < 0)
{
perror("ERROR server cannot read file.\n");
exit(1);
}
printf("The file name is: %s\n", buffer);

fp = fopen(buffer, "rw+");

if (fp==NULL)
{
perror("File could not be opened\n");
n = write(newsockfd,"ERROR:FILE NOT FOUND",20);
if (n < 0)
{
perror("ERROR writing to client.\n ");
exit(1);
}
exit(1);
}

while ((c = fgetc(fp)) != EOF)
{
if (x == 255)
{

/* Relay file contents to the client */
n = write(newsockfd,buffer,x);

if (n < 0)
{
perror("ERROR writing to client.\n ");
exit(1);
}
x = 0;
}
buffer[x] = (char) c;
x++;

}


/* Relay file contents to the client */
n = write(newsockfd,buffer,x);
if (n < 0)

{
perror("ERROR writing to client.\n ");
exit(1);
}




return 0;

}

最佳答案

socklen_t *clilen;
*clilen = sizeof(cli_addr);

这是未定义的行为。 clilen 未初始化,然后取消引用。

tempsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, (socklen_t *)&clilen);

您正在将 socklen_t** 类型的指针强制转换为 socklen_t*。这不可能有任何意义。

将其更改为

socklen_t clilen;
clilen = sizeof(cli_addr);

tempsockfd = accept(sockfd, (struct sockaddr*)&cli_addr, &clilen);

请注意最后一个参数中没有强制转换。不要插入强制转换来沉默编译器消息,尤其是当您不确定真正需要什么类型时。

关于c - 为什么我的accept()套接字函数返回 "Invalid argument"errno?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23278844/

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