为什么我的accept()套接字函数返回了“无效参数”错误?

时间:2014-04-24 20:12:04

标签: c sockets

当我运行这个源代码时,它会很好,直到它从客户端收到一个无效文件名的请求,然后所有连接尝试都失败了这个代码 - "无效的参数"当我试图接受时。

这是源代码,用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;

}

1 个答案:

答案 0 :(得分: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);

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