![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| E2900 controller naming | ppass | SUN Solaris | 3 | 06-05-2008 04:45 PM |
| naming a file to hostname | deaconf19 | Shell Programming and Scripting | 4 | 11-19-2007 08:54 PM |
| Naming convention for Libraries.. | rkshukla14 | UNIX Desktop for Dummies Questions & Answers | 0 | 02-09-2007 05:31 AM |
| How to discover what naming services are available | test111111 | Linux | 0 | 10-08-2004 07:34 AM |
| Controller Naming | shibz | UNIX for Advanced & Expert Users | 1 | 01-22-2003 08:07 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
Naming a socket
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:
Code:
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); write(s+count,"some data",9) But how do I append the count integer onto the 's' Thanks, G |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
I think the only time where you can append a count to the string , is preprocessor time which can be done as
#define s##1 #define s##2 John Arackal |
|
#3
|
|||
|
|||
|
hmmm, I was hoping I would be able to do this with sprintf or similar. In VB I could use 'cstr(count)' to cast the count integer to a string, is there nothing similar in C?
Thanks, G |
|
#4
|
||||
|
||||
|
Make an array called s and use s[count].
|
|
#5
|
|||
|
|||
|
Quote:
|
|
#6
|
|||
|
|||
|
Perderabo has the only logical solution. You wouldn't want to code all those if/else statements everywhere anyway, that's horrible coding style for this. It is obvious that you have a group of sockets, thus they should be logically grouped. An array will provide a group of sockets and you can access them as such.
Code:
#DEFINE NUM_SOCKETS 5 int socket_arr[NUM_SOCKETS]; int cnt = 0; write(socket_arr[cnt], "bla bla", 7); cnt = (cnt + 1) % NUM_SOCKETS; /* rotate cnt */ |
|||
| Google The UNIX and Linux Forums |