Error Accepting Connection Bad File Descriptor 9
Contents |
here for a quick overview of the site Help Center socket bad file descriptor python Detailed answers to any questions you might have Meta Discuss bad file descriptor socket the workings and policies of this site About Us Learn more about Stack Overflow bad file descriptor error in linux the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x what is bad file descriptor Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Linux C Socket:“bad file descriptor” occurs when accept() invokes up vote 1 down vote favorite 1 I am writing
Bad File Descriptor Recv
a send/receive project using linux/socket/tcp, I create some threads to send or recieve message,and use a global queue to store message Code: void EnQueue (M_queue queue,char * message,char * target_IP,char * target_IP_BAK) { char a; char * m=&a; strcpy(m,target_IP); M_element node=(M_element)malloc(sizeof(struct MessageNode )); node->message=message; node->target_IP=target_IP; node->next=NULL; if(IsQueueEmpty(queue)) { queue->rear=queue->front=node; } else { queue->rear->next=node; queue->rear=node; } } M_queue messages; void* receive_message(); void* send_message(void* args); int main() { pthread_t in_thread,out_thread; messages=(M_queue)malloc(sizeof(struct MessageQueue)); init(messages); int ret=pthread_create(&in_thread,NULL,receive_message,NULL); if(ret==-1) { perror("thread receive:"); } while(1) { if(!IsQueueEmpty(messages)) { M_element message=DeQueue(messages); pthread_create(&out_thread,NULL,send_message,(void*)message); } } pthread_join(in_thread,NULL); return 0; } void* send_message(void * args) { struct sockaddr_in sockaddr_out; M_element message=(M_element)args; int cfd=socket(AF_INET,SOCK_STREAM,0); if(cfd==-1) { perror("send_socket"); exit(1); } sockaddr_out.sin_family=AF_INET; sockaddr_out.sin_port=htons(9734); printf("%s\n",message->target_IP); sockaddr_out.sin_addr.s_addr=inet_addr(message->target_IP); int ret=connect(cfd,(struct sockaddr *)&sockaddr_out,sizeof(sockaddr_out)); if(ret==-1) { perror("connect"); offline_message(message->target_IP,message->message); exit(1); } if(write(cfd,message->message,strlen(message->message)+1)==-1) { perror("write"); exit(1); } close(cfd); } void* receive_message() { char buf[256]; char IP[256]; memset(IP,0,256); int first_socket=socket(AF_INET,SOCK_STREAM,0); if(first_socket==-1) { perror("socket"); e
years ago Bad file descriptor error in accept or bind or listen Reported by: jlawrence Owned by: Priority: normal Milestone: Component: other Version:
Bad File Descriptor C
trunk Keywords: Cc: Description (defun run-server (host port out) bad file descriptor socket accept (with-open-stream (server (ccl:make-socket :connect :passive :local-host host :local-port port :reuse-address t)) (with-open-stream (stream (ccl:accept-connection server)) bad file descriptor error in c (format out "~s~%" (read stream)) (print :pong stream) (finish-output stream)))) (defun make-socket/retry (host port tries) (loop (handler-case (return (ccl:make-socket :remote-host host :remote-port port)) http://stackoverflow.com/questions/11416396/linux-c-socketbad-file-descriptor-occurs-when-accept-invokes (ccl:socket-creation-error (err) (unless (plusp (decf tries)) (error err)) (format t "retry~%") (sleep 0.1))))) (defun test (host port) (ccl:process-run-function "server" #'run-server host port *standard-output*) ;(sleep 0.001) (with-open-stream (stream (make-socket/retry host port 5)) (print :ping stream) (finish-output stream) (format t "~s~%" (read stream)))) (defun run () (loop for port http://trac.clozure.com/ccl/ticket/1050 from 10000 to 20000 do (test "localhost" port))) Failure happens within three iterations on my machine. retry :PING :PONG retry > Error: on #
sockets, implementing a client-server communication. In the client after the connect () call runs a loop that executes 10 https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=73016 iterations, each iteration sends a message to the server and waits for a response. The server with the select() bad file waits for connections, once received the message from the client sends a response. At the first iteration is all ok, at the second the server gives me a bad file descriptor error when it executes the select again, bad file descriptor I can not deal with it, can anyone help? Thanks! Place the server code: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include
Board index The team Delete all board cookies All times are UTC