473,624 Members | 2,119 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_f ds);
FD_SET(sockfd_i n, &read_fds);
FD_SET(STDIN_FI LENO, &read_fds);
FD_SET(sockfd_o ut, &write_fds);
my_addr.sin_fam ily = AF_INET; // host byte order
my_addr.sin_por t = htons(MYPORT_IN ); // short, network byte order
my_addr.sin_add r.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(my_add r.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_OU T); // short, network byte order
inet_aton("127. 0.0.1", &(their_addr.si n_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_i n + 1, &read_fds, NULL, NULL, NULL);
printf("here\n" );
if(ready > 0){
if(FD_ISSET(soc kfd_in, &read_fds)){
printf("in\n");
if ((numbytes=recv from(sockfd_in, buf, MAXBUFLEN-1, 0, (struct
sockaddr *)&their_addr, &addr_len)) == -1){
perror("recvfro m");
exit(1);
}
printf("got packet from %s\n",inet_ntoa (their_addr.sin _addr));
printf("packet is %d bytes long\n",numbyte s);
buf[numbytes] = '\0';
printf("packet contains \"%s\"\n",bu f);
//close(sockfd_in );
}
if(FD_ISSET(STD IN_FILENO, &read_fds)){
fgets(string, 100, stdin);
printf("out\n") ;
if ((numbytes=send to(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_n toa(their_addr. sin_addr));
//close(sockfd_ou t);
}
}
if(ready < 0){
perror("select" );
}
}
return 0;
}

Nov 14 '05 #1
4 1402
"jessethepr o" <je*********@ma c.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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
"jessethepr o" <je*********@ma c.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.progr ammer.

--
Keith Thompson (The_Other_Keit h) 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(STD IN_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
5241
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 radio option, like so: Choice: (Radio1) type: (select box1) Choice: (Radio1) type: (select box2) Choice: (Radio1) type: (select box3) Choice: (Radio1) type: (select box4) Choice: (Radio1) type: (select box5)
4
6940
by: Elroyskimms | last post by:
Using SQL 2000... tblCustomer: CustomerID int CompanyName varchar(20) HasRetailStores bit HasWholesaleStores bit HasOtherStores bit tblInvoiceMessages:
4
7390
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. Unfortunately, Internet Explorer doesn't quite work as expected -- it gives me an "invalid argument" error that I don't know how to fix. Here's the entire script, with form, annotated to explain what I'm doing and where the problem is occurring. I...
3
5722
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 pointing to the sentence they come from (a column called "regret"). I also have a table of stop words (called "GenericStopWords") that contains the words I do not want to consider. That table has a single column called "word". I started off using a...
10
5613
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 create a single View and specify also in this View the WHERE clause that is common in these stored procedures, I will have the new stored procecures changed to be like:
1
2965
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 select return in a cursor, tried setting the values for each field with a separate select. Think I've just got the syntax wrong. Here's one of my attempts: use ESBAOffsets go
9
51980
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 2nd Select Menu are "printing options" for 4x6 and 5x7 prints respectively. the 3rd Select Menu holds an "Email" option that can be toggled (appear/reappear) by the 1st and 2nd Select Menus. So if a user selects prints from either 4x6 or 5x7 menu,...
2
3922
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: ----------------------------------------------------- <loanService>
4
4388
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. When an option is selected in the 2nd select list, the ASP page posts itself. Assume that the 1st select list has the following 10 options (note that both the select lists are actually populated from 2 different database tables): Australia
6
3391
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 the select was "111" and not "43+34+22+12".
0
8234
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8172
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8677
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8335
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8474
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5563
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4079
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.