473,499 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

use of select()

Hi all,

I have been racking my brain for days over the select() call. I use it
to monitor a socket and stdin. I run the program in two terminal
windows on localhost. The program is designed to allow two processes
to send and receive messages to each other. The problem is once one of
the processes sends its first message and the second process receives
it, the receiving processes is unable to send a message, but can
continue to receive. The code is the same for both processes I just
switch the the defined in and out ports. a copy of the program is
below and it compiles and runs on unix machines. The end result is a
p2p chat program I am designing. The program will run off of a
refferal system without a centralized server.

Thank you for any help
Jesse Johnson

//Code
/*
** dsoc.c IM program
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <time.h>
#include <sys/select.h>
#include <fcntl.h>
/* in order to run the program so the two processes can speak to each
other
** copy the program to a seperate file and switch the two ports below
*/
#define MYPORT_IN 20001 // the port users will be connecting to
#define MYPORT_OUT 20000 //port users will be writing to
#define MAXBUFLEN 100
int main(void){
int sockfd_in;
int sockfd_out;
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
int addr_len, numbytes;
char buf[MAXBUFLEN];
char string[100];
struct timeval tv;
int ready; //check if ready for input or output
fd_set read_fds;
fd_set write_fds;
tv.tv_sec = 0;
tv.tv_usec = 1000;
if ((sockfd_in = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
perror("socket_in");
exit(1);
}
if ((sockfd_out = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
perror("socket_out");
exit(1);
}
FD_ZERO(&read_fds);
FD_SET(sockfd_in, &read_fds);
FD_SET(STDIN_FILENO, &read_fds);
FD_SET(sockfd_out, &write_fds);
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT_IN); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(MYPORT_OUT); // short, network byte order
inet_aton("127.0.0.1", &(their_addr.sin_addr));
memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the
struct
if (bind(sockfd_in, (struct sockaddr *)&my_addr, sizeof(struct
sockaddr)) == -1){
perror("bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);
for(;;){
//block until stdin or sockfd_in have input
ready = select(sockfd_in + 1, &read_fds, NULL, NULL, NULL);
printf("here\n");
if(ready > 0){
if(FD_ISSET(sockfd_in, &read_fds)){
printf("in\n");
if ((numbytes=recvfrom(sockfd_in, buf, MAXBUFLEN-1, 0, (struct
sockaddr *)&their_addr, &addr_len)) == -1){
perror("recvfrom");
exit(1);
}
printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr));
printf("packet is %d bytes long\n",numbytes);
buf[numbytes] = '\0';
printf("packet contains \"%s\"\n",buf);
//close(sockfd_in);
}
if(FD_ISSET(STDIN_FILENO, &read_fds)){
fgets(string, 100, stdin);
printf("out\n");
if ((numbytes=sendto(sockfd_out, string, strlen(string), 0, (struct
sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1){
perror("sendto");
exit(1);
}
printf("sent %d bytes to %s\n",
numbytes,inet_ntoa(their_addr.sin_addr));
//close(sockfd_out);
}
}
if(ready < 0){
perror("select");
}
}
return 0;
}

Nov 14 '05 #1
4 1392
"jessethepro" <je*********@mac.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
I have been racking my brain for days over the select() call. I use it
to monitor a socket and stdin.

[snip]

The select() function and sockets are not part of standard C and are
therefore off-topic here. Perhaps news:comp.unix.programmer would be an
appropriate newsgroup.

<OT>
From a glance at your code, it looks like you are making the common mistake
of not setting up the fd_sets before each call to select(), which is
necessary because they are modified by the call.
</OT>

Alex
Nov 14 '05 #2
thank you for the help sorry about the post

Jesse Johnson

Nov 14 '05 #3
"jessethepro" <je*********@mac.com> writes:
I have been racking my brain for days over the select() call.

[snip]

The select() function is not standard C. Try comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
# //block until stdin or sockfd_in have input

# if(FD_ISSET(STDIN_FILENO, &read_fds)){
# fgets(string, 100, stdin);

fgets doesn't return until it gets a line or end of file. Having some
input available doesn't mean it has enough for fgets to return.

If you're reading and writing to the same process with blocking I/O,
there's a deadlock hazard if both processes block waiting for output
from the other.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Why are we here?
whrp
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5230
by: JT | last post by:
I have an input form for which I've created a "matrix" for user input. Basically, the user chooses a radio button and then through javascript, a select box is displayed to define a value for that...
4
6921
by: Elroyskimms | last post by:
Using SQL 2000... tblCustomer: CustomerID int CompanyName varchar(20) HasRetailStores bit HasWholesaleStores bit HasOtherStores bit tblInvoiceMessages:
4
7379
by: bobsawyer | last post by:
I've been building a series of SELECT lists that are populated dynamically using HTTPRequest. Things are going pretty well, and I've got the whole thing working flawlessly in Mozilla/Firebird....
3
5704
by: dumbledad | last post by:
Hi All, I'm confused by how to replace a SELECT statement in a SQL statement with a specific value. The table I'm working on is a list of words (a column called "word") with an index int...
10
5584
by: serge | last post by:
Using "SELECT * " is a bad practice even when using a VIEW instead of a table? I have some stored procedures that are identical with the difference of one statement in the WHERE clause. If I...
1
2957
by: serena.delossantos | last post by:
Trying to insert into a history table. Some columns will come from parameters sent to the store procedure. Other columns will be filled with a separate select statement. I've tried storing the...
9
51969
chunk1978
by: chunk1978 | last post by:
hey everyone, i've been trying to solve this problem for 2 days straight, with no end in sight. i would greatly appreciate anyone's help. EXPLANATION: There are 3 Select Menus. The 1st and...
2
3912
by: naima.mans | last post by:
Hello, i want to select 2 following brothers nodes wich are one under another (one closed to another)... i have done one xslt file... but it's not really good.. for example: the xml file:...
4
4380
by: rn5a | last post by:
A Form has 2 select lists. The 1st one whose size is 5 (meaning 5 options are shown at any given time) allows multiple selection whereas the 2nd one allows only 1 option to be selected at a time. ...
6
3383
by: Apaxe | last post by:
In the database i have a table with this information: key_id =1 key_desc =43+34+22+12 I want sum the values in key_desc. Something like: SELECT key_desc FROM table But the result of...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.