Reputation: 2098
I'm making a program to send and receive messages over a network, I currently have it set up to repeat the message 50 times, but I'd like it to delay each message by one second. Is there a way I can do this using select? If not, how else can I do it?
Thanks.
Here's the code for my client
void
client (char * servername) {
ssize_t bytes;
int skt;
char buff[BUF_SIZE];
int i;
do{
skt = createsocket ( servername, PORT );
bytes = (ssize_t) sprintf (buff, "Hello %s, sequence number:", servername);
if (write (skt, buff, bytes) < bytes) {
fprintf (stderr, "WARNING: write didn't accept complete message\n");
}
memset (buff, 0, BUF_SIZE);
bytes = read (skt, buff, BUF_SIZE);
if (bytes < 0) {
perror ("read()");
exit (1);
}
printf ("Server echoed the following: %s\n", buff);
i++;
}while(i < 50);
}
P.s. I'm also going to try to add a sequence number in there using a long type, how would I go about this?
Upvotes: 0
Views: 121
Reputation: 27552
This should be reasonably close to what you want. (Did not test.)
void client (char * servername)
{
ssize_t bytes;
int skt;
char buff[BUF_SIZE];
int i = 0;
long seqNum = 0;
skt = createsocket ( servername, PORT );
do
{
memset (buff, 0, BUF_SIZE);
struct timeval t = {1, 0};
select(0, NULL, NULL, NULL, &t);
bytes = (ssize_t) sprintf (buff, "Hello %s, sequence number: %ld", servername, seqNum++);
if (write (skt, buff, bytes) < bytes)
{
fprintf (stderr, "WARNING: write didn't accept complete message\n");
}
memset (buff, 0, BUF_SIZE);
bytes = read (skt, buff, BUF_SIZE);
if (bytes < 0)
{
perror ("read()");
exit (1);
}
printf ("Server echoed the following: %s\n", buff);
i++;
}
while (i < 50);
}
Upvotes: 1