Hello!
I have a simple problem in c++: I don't know how I can set the range of rand(). For example if I want a random number between 0 and 9, random(10) doesn't work. Does anyone know the right syntax?
Thanks, lg Georg
try rand()%10
Thanks - now i can remember!
LG Georg
I'm now trying to brush up my external for reading different sound formats. One problem I am having is with sockets though.
I use a normal connect() command like this:
if ( connect (fd, (struct sockaddr *) &server, sizeof (server)) < 0 ) { post("Couldn't connect"); close(fd); return (-1); } post("connected to server");
but I noticed that when I try to connect to the wrong port of servers that exist, the app will hang indefinitly trying to connect. I never get a response at all.
for example if I connect to http://66.28.68.70:8000/puredata.ogg and nothing is there at that server on that port, it returns non-0 with an error and everything is fine.
But, if I try to connect with http://66.28.68.70:3200/puredata.ogg, it hangs completely.
Olaf, I am looking at your code a lot to do the connect and http commands and noticed that this bug also affects your code. Try connecting to http://66.28.68.70:3200/anymount
any hints? would it be better to use bind somehow?
thanks -august.
hi august,
check man 2 connect: ETIMEDOUT Timeout while attempting connection. The server may be too busy to accept new connections. Note that for IP sockets the timeout may be very long when syncookies are enabled on the server.
so you're better off using select, then you can set your own timeout.
tom
On Tue, 2 Mar 2004, august wrote:
I'm now trying to brush up my external for reading different sound formats. One problem I am having is with sockets though.
I use a normal connect() command like this:
if ( connect (fd, (struct sockaddr *) &server, sizeof (server)) < 0 ) { post("Couldn't connect"); close(fd); return (-1); } post("connected to server");
but I noticed that when I try to connect to the wrong port of servers that exist, the app will hang indefinitly trying to connect. I never get a response at all.
for example if I connect to http://66.28.68.70:8000/puredata.ogg and nothing is there at that server on that port, it returns non-0 with an error and everything is fine.
But, if I try to connect with http://66.28.68.70:3200/puredata.ogg, it hangs completely.
Olaf, I am looking at your code a lot to do the connect and http commands and noticed that this bug also affects your code. Try connecting to http://66.28.68.70:3200/anymount
any hints? would it be better to use bind somehow?
thanks -august.
PD-dev mailing list PD-dev@iem.at http://iem.at/cgi-bin/mailman/listinfo/pd-dev
On Tue, 2 Mar 2004, Tom Schouten wrote:
hi august, check man 2 connect: ETIMEDOUT Timeout while attempting connection. The server may be too busy to accept new connections. Note that for IP sockets the timeout may be very long when syncookies are enabled on the server. so you're better off using select, then you can set your own timeout. tom
Tom,
I don't even make it that far. Connect() doesn't return anything! I can't even get an error response. And without a connection, select does me no good.
Doing a search, I saw some ppl using alarm and signal around the connect call to kill it if it hangs. This looks like a dirty hack though. Any comments? code below.
-august.
signal (SIGALRM, connect_timed_out); alarm (CONNECT_TIME_OUT);
if(connect(sock,(struct sockadr *)& server,sizeof(server))<0) { perror("connect"); exit(1); }
/** ** Just in case the other end crashes without closing ** and we hang on a write ** time out the whole program after a while **/
alarm (NORMAL_TIME_OUT); signal (SIGALRM, normal_time_out);
and man 7 socket
It is possible to do non-blocking IO on sockets by setting the O_NON- BLOCK flag on a socket file descriptor using fcntl(2). Then all opera- tions that would block will (usually) return with EAGAIN (operation should be retried later); connect(2) will return EINPROGRESS error. The user can then wait for various events via poll(2) or select(2).
[august]->[Re: [PD-dev] socket gurus]->[04-03-02 16:39]
| |On Tue, 2 Mar 2004, Tom Schouten wrote: |> hi august, |> check man 2 connect: |> ETIMEDOUT |> Timeout while attempting connection. The server may be too |> busy to accept new connections. Note that |> for IP sockets the timeout may be very long when syncookies |> are enabled on the server. |> so you're better off using select, then you can set your own timeout. |> tom | |Tom, | |I don't even make it that far. Connect() doesn't return anything! |I can't even get an error response. And without a connection, select does |me no good. | |Doing a search, I saw some ppl using alarm and signal around the connect |call to kill it if it hangs. This looks like a dirty hack though. Any |comments? code below. | |-august. | | | signal (SIGALRM, connect_timed_out); | alarm (CONNECT_TIME_OUT); | | if(connect(sock,(struct sockadr *)& server,sizeof(server))<0) | { | perror("connect"); | exit(1); | } | | /** | ** Just in case the other end crashes without closing | ** and we hang on a write | ** time out the whole program after a while | **/ | | alarm (NORMAL_TIME_OUT); | signal (SIGALRM, normal_time_out); | | | | |_______________________________________________ |PD-dev mailing list |PD-dev@iem.at |http://iem.at/cgi-bin/mailman/listinfo/pd-dev |
august wrote:
for example if I connect to http://66.28.68.70:8000/puredata.ogg and nothing is there at that server on that port, it returns non-0 with an error and everything is fine.
But, if I try to connect with http://66.28.68.70:3200/puredata.ogg, it hangs completely.
Hi August,
what's the difference between the two URLs (in terms of server settings, mountpoints and the like)? As far as I understand connect() can connect to a socket in listening state and the socket we're connecting to has to know what to do when it receives a connection request. In case you accidentally try to connect to a socket meant to be used for connectionless transfers (i.e. UDP and thus not in listening state) a connect() call might block since it just doesn't get served from the other side. This might also happen in case the server crashes... The alarm / signal handler solution might be a way, although I never used it myself.
Olaf
august wrote:
for example if I connect to http://66.28.68.70:8000/puredata.ogg and nothing is there at that server on that port, it returns non-0 with an error and everything is fine.
But, if I try to connect with http://66.28.68.70:3200/puredata.ogg, it hangs completely.
Hi August,
what's the difference between the two URLs (in terms of server settings, mountpoints and the like)?
The only difference is the port numbers. On 8000, there was an icecast or shoutcast server running at one point, but not anymore. The connect() call properly checks for a connection and returns an error.
I'm not sure why, but port 3200 (as well as other ports) will cause connect() to hang.
This may have something to do with blocking and non-blocking mode on the socket.
I'm now going over select, connect and socket man pages as Tom pointed out.
As far as I understand connect() can connect
to a socket in listening state and the socket we're connecting to has to know what to do when it receives a connection request.
right, but the app will hang for a very LONG time. I'm guessing this has to do with the socket being in blocking mode.
In case you
accidentally try to connect to a socket meant to be used for connectionless transfers (i.e. UDP and thus not in listening state) a connect() call might block since it just doesn't get served from the other side. This might also happen in case the server crashes...
actually, I think Tom's suggest was on track....to set the socket in non-blocking mode, and then mabey poll() the filedescriptor ?????
any examples?
-august.
On Tue, Mar 02, 2004 at 04:56:17PM +0100, august wrote:
actually, I think Tom's suggest was on track....to set the socket in non-blocking mode, and then mabey poll() the filedescriptor ?????
Most of the literature I've read (such as _Linux Socket Programming_ by Que publishing) seems to suggest setting up the socket and then using select to check for data periodically. You should do the same for detecting a time-out as someone else has suggested.
Regards,
Chris. --------------------------------- chris@mccormick.cx http://www.mccormick.cx http://www.sciencegirlrecords.com ---------------------------------
ok, finally got it.
let me know if this looks ok.
you first use fcntl to set the socket to non-blocking mode.
then you connect , expecting errno to be EINPROGRESS, ...then selecting on the socket with a timeout value to see if it is ready.
Olaf, this should fix the same problem with oggamp~ and mp3amp~.....and I noticed that x_net.c from pd/src is affected by this problem
netsend will hang and block pd entirely on a netsend connect to certain ip's and ports.
this should also "fix" that as well, assuming pd is willing to wait for a timeval while connecting with netsend. the comments in .37 version suggest that this will be put into a thread.
thanks for the help -august.
----------------------------------------
int flags = fcntl( fd, F_GETFL, 0); fcntl( fd, F_SETFL, FNDELAY); // make this socket's calls non-blocking // fcntl( fd, F_SETFL, flags | O_NONBLOCK);
if (connect( fd, (struct sockaddr *) &server, sizeof(server) ) == -1 && errno != EINPROGRESS) { /* * If non-blocking connect() couldn't finish, it returns * EINPROGRESS. That's OK, we'll take care of it a little * later, in the select(). But if some other error code was * returned there's a real problem... */ sys_closesocket (fd); return(-1);
} else {
FD_ZERO (&fdset); FD_SET (fd, &fdset); tv.tv_sec = 1; /* seconds */ tv.tv_usec = 0; /* microseconds */
// you want to do the select on the WRITEablity of the socket, // HTTP expects a get command, so make sure to pass args to // both read and write fdset switch (select( fd+1 , &fdset, &fdset, NULL, &tv) ) { /* * select() will return when the socket is ready for action, * or when there is an error, or the when timeout specified * using tval is exceeded without anything becoming ready. */
case 0: // timeout //do whatever you do when you couldn't connect cout << "InputStream:: connect timed out" <<endl; sys_closesocket (fd); return (-1); break; case -1: // error cout << "InputStream:: connection error" <<endl; sys_closesocket (fd); return (-1); break; default: // your file descriptor is ready... fcntl( fd, F_SETFL, flags); break; } }
hey leute,
I'm now piecing readanysf~ together for another release.
I'd appreciate any bug reports or suggustions.
found here: http://aug.ment.org/software/readanysf~-0.13.pre.tar.gz
changes: autoconf, automake system support for darwin - tested and runs on OSX now support for flext as a shared library
change in message for opening stream, now it is a single "open" command for all files, from disk or from the net. it will also open up normal .mp3 or .ogg files on a webserver
bug fixes for streaming support
flac is no longer seekable due to bugs in the flac library (anyone experienced with this ?) it will still play, just no seeking support
future still: windows support nudge feature (as requested) backwards play (if I can setup a buffer object that can be smart enought to remember its seek point in the file for all formats)
merci y gracias -augusto.
PS: of course, thanks to thomas for flext!