|
Home > Archive > Unix Programming > May 2006 > doubts regarding socket programming !!
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
doubts regarding socket programming !!
|
|
| sayantan.chowdhury@gmail.com 2006-05-18, 1:16 pm |
| Something like select() or poll() can be used to determine when a
socket has data to be read() or has space to allow a write() (although
it won't always tell you how much space, depends on the platform and
if it supports the "watermarks").
Is the above statement true ??
Could anyone please elaborate on this with some code examples ??
any input will be of great help.
bye,
Sayantan Chowdhury
| |
| Sjoerd 2006-05-18, 1:16 pm |
| I use the function below to see if there is input from the keyboard or
from a barcode reader. Instead of using STDIN and codereader, you could
also use one or more socket descriptors.
/* select wrapper */
/* mask can be INP_KEYBOARD, INP_CODEREADER or both
* timeout is the timeout in milliseconds or NOTIMEOUT for no timeout
* returns the mask for the inputs which are readable
*/
int waitforinput(int mask, int timeout) {
fd_set readfds;
int max;
struct timeval _tv;
struct timeval * tv=&_tv;
int res;
output_refresh();
if (mask==0) return 0;
/* set timeout struct */
if (timeout!=NOTIMEOUT) {
tv->tv_sec=timeout/1000;
tv->tv_usec=(timeout%1000)*1000;
} else {
tv=NULL;
}
/* set fd_set */
FD_ZERO(&readfds);
if (mask & INP_KEYBOARD) {
FD_SET(STDIN, &readfds);
max=STDIN;
}
if (mask & INP_CODEREADER) {
FD_SET(codereader, &readfds);
max=codereader;
}
if (timeout==-1) {
res=select(max+1, &readfds, NULL, NULL, NULL);
} else {
res=select(max+1, &readfds, NULL, NULL, tv);
}
if (res==-1) return -1;
/* check readfds to see which fds are readable */
res=0;
if (FD_ISSET(STDIN, &readfds)) {
res|=INP_KEYBOARD;
}
if (FD_ISSET(codereader, &readfds)) {
res|=INP_CODEREADER;
}
return res;
}
| |
| Fred Kleinschmidt 2006-05-18, 7:15 pm |
| "Sjoerd" <sjoerder@gmail.com> wrote in message
news:1147970827.643997.251480@i40g2000cwc.googlegroups.com...
>I use the function below to see if there is input from the keyboard or
> from a barcode reader. Instead of using STDIN and codereader, you could
> also use one or more socket descriptors.
Are you answering someone's questiopn? If so, do NOT omit their posting!
You force me to search for the context .
>
> /* select wrapper */
> /* mask can be INP_KEYBOARD, INP_CODEREADER or both
> * timeout is the timeout in milliseconds or NOTIMEOUT for no timeout
> * returns the mask for the inputs which are readable
> */
> int waitforinput(int mask, int timeout) {
> fd_set readfds;
> int max;
> struct timeval _tv;
> struct timeval * tv=&_tv;
> int res;
>
> output_refresh();
>
> if (mask==0) return 0;
> /* set timeout struct */
> if (timeout!=NOTIMEOUT) {
crash! NOTIMEOUT not defined
> tv->tv_sec=timeout/1000;
> tv->tv_usec=(timeout%1000)*1000;
> } else {
> tv=NULL;
> }
>
> /* set fd_set */
> FD_ZERO(&readfds);
crash! FD_ZERO not defined
> if (mask & INP_KEYBOARD) {
crash! INP_KEYBOARD not defined
> FD_SET(STDIN, &readfds);
crash! FD_SET not defined
crash! STDIN not defined
> max=STDIN;
> }
> if (mask & INP_CODEREADER) {
crash! INP_CODEREADER not defined
> FD_SET(codereader, &readfds);
> max=codereader;
> }
>
> if (timeout==-1) {
> res=select(max+1, &readfds, NULL, NULL, NULL);
> } else {
> res=select(max+1, &readfds, NULL, NULL, tv);
> }
> if (res==-1) return -1;
>
> /* check readfds to see which fds are readable */
> res=0;
> if (FD_ISSET(STDIN, &readfds)) {
crash! FD_ISSET not defined
> res|=INP_KEYBOARD;
> }
> if (FD_ISSET(codereader, &readfds)) {
> res|=INP_CODEREADER;
> }
>
> return res;
> }
>
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
| |
| sayantan.chowdhury@gmail.com 2006-05-19, 1:25 am |
| I would like to know how we can check whether a socket is ready for
writing for , say N bytes.
Please explain with code examples.
Any input will be of great help.
Thanks,
Sayantan Chowdhury
| |
| Maxim Yegorushkin 2006-05-19, 1:25 am |
|
sayantan.chowdhury@gmail.com wrote:
> I would like to know how we can check whether a socket is ready for
> writing for , say N bytes.
You can probably get that value. The only problem is that the value
would be of little use. By the time you use that value the situation
may have changed completely. A typical race condition.
| |
| Rick Jones 2006-05-19, 1:17 pm |
| Maxim Yegorushkin <maxim.yegorushkin@gmail.com> wrote:
> sayantan.chowdhury@gmail.com wrote:
[vbcol=seagreen]
> You can probably get that value.
Might not be portable though 
> The only problem is that the value would be of little use. By the
> time you use that value the situation may have changed completely. A
> typical race condition.
Although if there are no other processes/threads accessing the socket,
you can reasonably XXX-u-me that if at time T0 it was possible to
write N bytes, at T1 it is probably still possible to write at least N
bytes. True, the remote could have close the connection between T0
and T1, but at that point the attempt to write will not block - it
will still either write the bytes, or get an error.
rick jones
--
oxymoron n, commuter in a gas-guzzling luxury SUV with an American flag
these opinions are mine, all mine; HP might not want them anyway... 
feel free to post, OR email to rick.jones2 in hp.com but NOT BOTH...
| |
| Alex Fraser 2006-05-19, 1:17 pm |
| "Rick Jones" <rick.jones2@hp.com> wrote in message
news:kWmbg.829$3_2.423@news.cpqcorp.net...
> Maxim Yegorushkin <maxim.yegorushkin@gmail.com> wrote:
>
>
> Might not be portable though 
>
>
> Although if there are no other processes/threads accessing the socket,
> you can reasonably XXX-u-me that if at time T0 it was possible to
> write N bytes, at T1 it is probably still possible to write at least N
> bytes.
I would say "probably" just isn't good enough in many situations, in
particular for "unattended software" such as a typical server. If you want
to ensure that write() won't block (which is, for instance, a common
requirement when using select() or poll() to multiplex I/O on multiple
sockets), you can set O_NONBLOCK on the descriptor. Then the issue of
portably finding out how many bytes you can write without blocking is moot
and the unavoidable problem that the number could change anyway is
irrelevant.
Alex
| |
| davids@webmaster.com 2006-05-21, 7:14 pm |
| >I would say "probably" just isn't good enough in many situations, in
>particular for "unattended software" such as a typical server. If you want
>to ensure that write() won't block (which is, for instance, a common
>requirement when using select() or poll() to multiplex I/O on multiple
>sockets), you can set O_NONBLOCK on the descriptor. Then the issue of
>portably finding out how many bytes you can write without blocking is moot
>and the unavoidable problem that the number could change anyway is
>irrelevant.
Only setting the socket non-blocking guarantees that you will not
block. Even if you find out what you think is how many bytes you can
send without blocking, there is no guarantee that number of bytes
consumed in the buffer is equal to the number of bytes sent. For
example, there may be 12,000 bytes available in the buffer, but sending
11,000 bytes may block because the OS may internally need some bytes to
hold control information.
DS
|
|
|
|
|