linux socket tcp 为什么老是是accept error : Bad file descriptor

xs767461465 2011-08-04 10:47:29

/*server.c*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <strings.h>
#include <pthread.h>
#define SERVPORT 1620 /* server listen port */
#define BACKLOG 20 /* max number of linking at a time */
#define BUF 100
int main()
{
void* tcp_thread (void* );
int sockfd,client_fd; /*sock_fd:listen socket;client_fd:data transport socket */
int on=1;
struct sockaddr_in my_addr; /* information of server address */
struct sockaddr_in remote_addr;/* information of client address */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)/* socket create */
{
perror( "socker create error\n ");
return 0;
}

bzero(&my_addr,sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(SERVPORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);

setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(void *)&on,sizeof(int));

if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) //bind
{
perror( "bind error\n ");
return 0;
}
if (listen(sockfd, BACKLOG) < 0) //listen
{
perror( "listen error\n ");
return 0;
}

printf( "sockfd:%d\n ", sockfd);

while(1)
{
int sin_size = sizeof(struct sockaddr);

if((client_fd=accept(sockfd,(struct sockaddr *)&remote_addr,(socklen_t*)&sin_size)) < 0)
{
perror( "accept error\n ");
continue;
}

printf( "client_fd:%d\n ",client_fd);

char ptr[32];

inet_ntop(AF_INET,(void *)&remote_addr.sin_addr,ptr,32);

printf( "received a connection from %s\n ", ptr);

pthread_t thread_id;
pthread_attr_t attr;

pthread_attr_init(&attr);

pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); /* the attribute of the thread */

pthread_create(&thread_id,&attr,tcp_thread,(void *)client_fd);

pthread_attr_destroy(&attr);
}

return 1;
}

void* tcp_thread (void* fd) //thread for a client link
{
int newfd = (int)fd;

printf( "newfd:%d\n ",newfd);

while (1)
{
fd_set rfds;
FD_ZERO( &rfds);
FD_SET(newfd, &rfds);
select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
if (FD_ISSET(newfd, &rfds))
{
FD_CLR(newfd, &rfds);
char data[BUF];
memset(data, '\0', sizeof(data));
int res = read(newfd, data, sizeof(data));
printf( "the mes is %d\n ",res);
if (res < 0)
{
perror( "Communication error\n ");
return NULL;
}
else if(res == 0)
{
printf( "Communication end\n ");
shutdown(newfd,SHUT_RDWR);
return NULL;
}
else
{
printf( "date:%s\n ",data);
}
}
else
{
continue;
}
}

}


/*client.c*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <strings.h>
#include <netdb.h>
#include <pthread.h>

#define BUF 100
void* pthfunction (void* fd) //thread for a client link
{
int newfd = (int)fd;
printf( "newfd:%d\n ",newfd);//new pthread fd
while (1)
{
fd_set rfds;
FD_ZERO( &rfds);
FD_SET(newfd, &rfds);

select(FD_SETSIZE, &rfds, NULL, NULL, NULL);

if (FD_ISSET(newfd, &rfds))
{
FD_CLR(newfd, &rfds);

char data[BUF];
scanf( "%s ",data);

memset(data, '\0', sizeof(data));//clean 0

int res = write(newfd,data,strlen(data));

printf( "the mes is %d\n ",res);

if (res < 0) //fail
{
perror( "Communication error\n ");
return NULL;
}
else if(res == 0) //interrupt
{
printf( "Communication end\n ");
shutdown(newfd,SHUT_RDWR);
return NULL;
}
else //success
{
printf( "date:%s\n ",data);
}
}
else
{
continue;
}
}

}

int main (int argc,char *argv[])
{
if (argc < 2)
{
perror( "Please enter the server 's hostname!\n ");
exit(1);
}
int fid = socket(AF_INET, SOCK_STREAM, 0);
if(fid == -1)
{
perror( "socket error!\n ");
exit(1);
}

struct sockaddr_in their_addr;
struct hostent *host;
if((host = gethostbyname(argv[1])) == NULL)
{
perror( "gethostbyname error!\n ");
exit(1);
}
bzero(&their_addr,sizeof(their_addr));
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(1620);
their_addr.sin_addr = *((struct in_addr *)host-> h_addr);

if(connect(fid,(struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1)
{
printf( "connect error\n ");
exit(1);
}
while(1)
{
int len_clt,sendfd;

len_clt = sizeof(their_addr);

if((sendfd = accept(&fid,(struct sockaddr *)&their_addr,(socklen_t*)&len_clt)) == -1)
{
perror( "accept error\n ");
exit(1);
}

pthread_t thread_id;
pthread_attr_t attr;

pthread_attr_init(&attr);

pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); // the attribute of the thread

pthread_create(&thread_id,&attr,pthfunction,(void *)sendfd);

pthread_attr_destroy(&attr);

// write(fid,data,strlen(data));

//create a pthread to send message to server
}
return 1;
}
...全文
2431 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
m_linux 2014-04-12
  • 打赏
  • 举报
回复
1: client_fd 改为全局变量 2: pthread_create(&thread_id,&attr,tcp_thread,(void*)client_fd); 改为 pthread_create(&thread_id,&attr,tcp_thread,(void*)&client_fd); 3: void* tcp_thread (void* fd) //thread for a client link { int newfd = (int)fd; ..... } 改为 int newfd = *(*int)fd;
DIE654456 2011-08-05
  • 打赏
  • 举报
回复
客户端发个消息,马上创建一个线程,发给server ,server接受这个消息创建线程来接受处理

其实客户端做单线程就行了,要完成这些动作,connect、send,没必要bind、listen、accept。
服务器也可以做单线程来对应1个客户,首先bind、listen、accept、然后就是recv和send。

等你做好这个模型之后可以把服务器改为多线程,在accept之后根据accept的返回值,即全连接的描述符
创建线程去处理与客户的消息,主线程继续accept客户。
冻结 2011-08-04
  • 打赏
  • 举报
回复
/*client.c*/ 中

if((sendfd = accept(&fid,(struct sockaddr *)&their_addr,(socklen_t*)&len_clt)) == -1)
{
perror( "accept error\n ");
exit(1);
}


客户端不要accept。

xs767461465 2011-08-04
  • 打赏
  • 举报
回复
不行啊!大家看看我改的,但是只能发二次,而且server还收不到
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <strings.h>
#include <netdb.h>
#include <pthread.h>

#define MAXSIZE 100
#define BACKLOG 20

struct sockaddr_in server_addr;

char buffer[MAXSIZE];

void* pthfunction(void* fd) //thread for a server link
{
int newfd = (int )fd ; //new pthread fd

printf( "newfd:%d\n ",newfd);

while(1)
{
fd_set rfds;
FD_ZERO( &rfds);
FD_SET(newfd, &rfds);

if(connect(newfd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr)) == -1)
{
printf( "connect error\n ");
exit(1);
}

select(FD_SETSIZE, &rfds, NULL, NULL, NULL);

if (FD_ISSET(newfd, &rfds))
{
FD_CLR(newfd, &rfds);

memset(buffer, '\0', sizeof(buffer));//clean 0

int res = write(newfd,buffer,sizeof(buffer));

printf( "the mes is %d\n ",res);

if (res < 0) //write fail
{
perror( "Communication error\n ");
return NULL;
}
else if(res == 0) //interrupt
{
printf( "Communication end\n ");
shutdown(newfd,SHUT_RDWR);
return NULL;
}
else //success
{
printf( "date:%s\n ",buffer);
}
}
else
{
continue;
}
}

}

int main(int argc,char *argv[])
{
struct hostent *host;

if(argc < 2)
{
perror( "Please enter the server 's hostname!\n ");
exit(1);
}

if((host = gethostbyname(argv[1])) == NULL)
{
perror( "gethostbyname error!\n ");
exit(1);
}

bzero(&server_addr,sizeof(server_addr));

server_addr.sin_family = AF_INET;

server_addr.sin_port = htons(1620);

server_addr.sin_addr = *((struct in_addr *)host-> h_addr);

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

if(fid == -1)
{
perror( "socket error!\n ");
exit(1);
}

//如果是这样的话,这里应该是要写消息了,才创建线程的,可是不是这样的
while(1)
{
scanf( "%s ",buffer);

pthread_attr_t attr;

pthread_attr_init(&attr);

pthread_create(&fid,&attr,pthfunction,NULL);

pthread_attr_destroy(&attr);
}

return 1;
}
stjay 2011-08-04
  • 打赏
  • 举报
回复
这段报的错
if((sendfd = accept(&fid,(struct sockaddr *)&their_addr,(socklen_t*)&len_clt)) == -1)
  {
  perror( "accept error\n ");
  exit(1);
  }

本来要传的是fid而不是&fid
另外Client不应该有accept
xs767461465 2011-08-04
  • 打赏
  • 举报
回复
b不行阿!好多错阿!
nightkids_008 2011-08-04
  • 打赏
  • 举报
回复
void* pthfunction(void* fd) //thread for a server link
{
int newfd = (int)fd; //new pthread fd
这里改成我写的那个试试
int newfd = *(int*)fd;
xs767461465 2011-08-04
  • 打赏
  • 举报
回复
你看这样子行不行:
void* pthfunction(void* fd) //thread for a server link
{
int newfd = (int)fd; //new pthread fd

printf( "newfd:%d\n ",newfd);

struct sockaddr_in server_addr;
struct hostent *host;

if((host = gethostbyname(argv[1])) == NULL)
{
perror( "gethostbyname error!\n ");
exit(1);
}

bzero(&server_addr,sizeof(server_addr));

server_addr.sin_family = AF_INET;

server_addr.sin_port = htons(1620);

server_addr.sin_addr = *((struct in_addr *)host-> h_addr);

while(1)
{
fd_set rfds;
FD_ZERO( &rfds);
FD_SET(newfd, &rfds);

select(FD_SETSIZE, &rfds, NULL, NULL, NULL);

if (FD_ISSET(newfd, &rfds))
{
FD_CLR(newfd, &rfds);

char data[BUF];
scanf( "%s ",data);

memset(data, '\0', sizeof(data));//clean 0

if(connect(fid,(struct sockaddr *)&server_addr,sizeof(struct sockaddr)) == -1)
{
printf( "connect error\n ");
exit(1);
}

int res = write(newfd,data,strlen(data));

printf( "the mes is %d\n ",res);

if (res < 0) //write fail
{
perror( "Communication error\n ");
return NULL;
}
else if(res == 0) //interrupt
{
printf( "Communication end\n ");
shutdown(newfd,SHUT_RDWR);
return NULL;
}
else //success
{
printf( "date:%s\n ",data);
}
}
else
{
continue;
}
}

}

int main(int argc,char *argv[])
{
if(argc < 2)
{
perror( "Please enter the server 's hostname!\n ");
exit(1);
}

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

if(fid == -1)
{
perror( "socket error!\n ");
exit(1);
}

while(1)
{
pthread_create(&fid,&attr,pthfunction,NULL);
}

return 1;
}
nightkids_008 2011-08-04
  • 打赏
  • 举报
回复
要么你改线程里那句吧
newfd=*(int*)fd;

xs767461465 2011-08-04
  • 打赏
  • 举报
回复
nightkids_008:pthread_create(&fid,&attr,pthfunction(fid),(void *)sendfd);是不是啊!好像不行吧,没见过,我是一个新手
xs767461465 2011-08-04
  • 打赏
  • 举报
回复
while(1)
{
int sendfd,len_clt;

len_clt = sizeof(server_addr);

sendfd = accept(fid,(struct sockaddr *)&server_addr,(socklen_t* )&len_clt);

pthread_create(&thread_id,&attr,pthfunction,(void *)sendfd);

pthread_attr_destroy(&attr);
不知道这样可不可以在client
xs767461465 2011-08-04
  • 打赏
  • 举报
回复
我想实现的是:客户端发个消息,马上创建一个线程,发给server ,server接受这个消息创建线程来接受处理,(还没实现的再将消息发回去)
DIE654456 2011-08-04
  • 打赏
  • 举报
回复
客户端不要accept,通常accept是需要bind到特定地址和端口上才能接收别人的connect的。
你这个程序的目的?
服务器是多线程,只接收;客户端是单线程,只发送?
nightkids_008 2011-08-04
  • 打赏
  • 举报
回复
参数传个 int fd 好了 就把你的文件描述符号传过来。。。
xs767461465 2011-08-04
  • 打赏
  • 举报
回复
nightkids_008:那要怎么办呢?
nightkids_008 2011-08-04
  • 打赏
  • 举报
回复
void* pthfunction (void* fd) //thread for a client link
{
int newfd = (int)fd;
这里传的是个指针。。。。你转成int后再给newfd,这已经不是原来的文件描述符了。
要么改参数。。。
xs767461465 2011-08-04
  • 打赏
  • 举报
回复
不行阿,我试了一下,摆脱了

70,017

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧