The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 11-08-2006
Registered User
 

Join Date: Nov 2006
Posts: 2
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);
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
Reply With Quote
Forum Sponsor
  #2  
Old 11-08-2006
Registered User
 

Join Date: Jan 2006
Posts: 24
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
Reply With Quote
  #3  
Old 11-08-2006
Registered User
 

Join Date: Nov 2006
Posts: 2
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
Reply With Quote
  #4  
Old 11-08-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,610
Make an array called s and use s[count].
Reply With Quote
  #5  
Old 11-09-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 974
Quote:
Originally Posted by geester
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?
Absolutely nothing, sorry. You can't do things that way. C is not a shell. There is a difference between variable names and strings that can't be bridged -- you can't use a string as a variable name, or anything analogous, because entity names simply can't change at runtime, at all, ever.
Reply With Quote
  #6  
Old 11-20-2006
Registered User
 

Join Date: Oct 2003
Posts: 69
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 */
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:28 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0