-
December 21st, 2010, 08:23 PM
#1
Please help! Network programming error :/
Hello everyone..
I have a problem.. The main idea of my project is a server that accept client connections and everytime someone connects it checks if the client is available for reading or for writing with select() function.
If is ready for writing then is send a buffer.. Well, this is a sample of code..
...
FD_ZERO(&master);
FD_SET(fdmax, &master);
for(; {
read_fds = master;
write_fds = master;
if(select(fdmax + 1, &read_fds, &write_fds, NULL, NULL) == -1) {
perror("select");
return -1;
}
for(i = 0; i <= fdmax; i++) { // check through all socket
if(FD_ISSET(i, &read_fds) != 0) {
bla bla bla
}
if(FD_ISSET(i, &write_fds)) { // teh client is ready for writing
printf("send bytes: %ld\n", sizeof(jobs[counter]) );
bytes = send(i, &jobs[counter], sizeof(jobs[counter]), 0);
counter++;
}
also the client source is..
while(f == 0) {
memset(&jobs, 0, sizeof(jobs));
if((bytes = recv(sockfd, &jobs, sizeof(jobs), 0)) == -1) {
printf("Error recving\n");
}
printf("here");
bla bla bla
}
the problem is that the server althougt is was supposed to send the msg only 1 time it is send it 10-15 times... it is like finds the client client all the time available for writing ( maybe i am doing sthng wrong with select and fds?).. Also the client exiting with segmentation fault JUST AFTER the printf function and i dunno why.. i suppose it is from the "oversending" of the packets from the server
What should i do ? Thanks in advance..
(i compiled it with gcc on Ubuntu distro)
-
December 22nd, 2010, 04:48 AM
#2
Re: Please help! Network programming error :/
Originally Posted by Zisis
What should i do ? Thanks in advance..
The first thing you have to do is edit your post to add Code tags around code snippets and proper code indentations inside the snippets. Otherwise your "code" is absolutely unreadable.
See Announcement: Before you post....
Victor Nijegorodov
-
December 27th, 2010, 06:38 AM
#3
Re: Please help! Network programming error :/
you have to re-initialise your read_fds and write_fds after select() returns, because they will be altered by the select() call.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|