PDA

View Full Version : socket naming


geester
11-08-2006, 05:21 PM
Im not very experienced with C so this is probably a basic question. I have a script that opens up 5 sockets, it then runs through a loop and on a given event reconnects to the relevant socket and sends some data. The socket to be reconnected to is kept track of with a 'count' variable. The sockets are named s0,s1,s2 etc. This is the code I have to send the data:


if (count == 0) write(s0,"some data",9);
if (count == 1) write(s1,"some data",9);
if (count == 2) write(s2,"some data",9);
if (count == 3) write(s3,"some data",9);
if (count == 4) write(s4,"dome data",9);


Instead of this I simply want to use:


write(s+count,"some data",9)

But how do I append the count integer onto the 's'

Thanks, G

i3839
11-08-2006, 05:53 PM
C isn't PHP or anything, so you can't do it that way. Instead you let s be an array of sockets, and use array indexing.


int s[NUMSOCKS];

...

if (count >= 0 && count < NUMSOCKS)
write(s[count], "some data", 9);
else
// error: bad socket index/count
...


Of course in general you need to check the return value of write() too, or unexpected things may happen.

geester
11-08-2006, 06:00 PM
Thanks for the reply, I will gve this a go.

Appreciated, G