473,486 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How a client will connect to multiple servers through UDP Socket (Linux 2.4) + timer

4 New Member
Hai to all frns,

Reqirement 1:

1.I need to send a Request to one server through UDP socket.at that time i have to start a timer (setted to 2 sec) if i wont get the Response/ACK from that corresponding ACK from that server within 2 sec.i need to send the Request again.

for this i did like this.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <time.h>
#include <sys/ioctl.h>
#include <unistd.h>
void error(char *);

int main()
{

int sockfd,length,res,max_iterations =3;
struct sockaddr_in server, from;
char buffer[256];
struct timeval timeout;
unsigned char flag=1,i;
int count =0 ;

fd_set readfd,testfd;

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) error("socket");

server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_port = htons(9734);
length = sizeof(struct sockaddr_in);

FD_ZERO(&readfd);
FD_SET(sockfd,&readfd);
FD_SET(0,&readfd);

printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);

while(flag && (max_iterations >0))
{

printf("sending the message %d time\n",++count);
res = sendto(sockfd,buffer,strlen(buffer),0,(struct sockaddr *)&server,length);
if (res < 0) error("error in Sendto");

fprintf(stderr,"Waiting......\n");
testfd = readfd;
timeout.tv_sec =2;
timeout.tv_usec =500000;

/*select function call uses timeout value to prevent indefinite blocking of recvfrom ..timeout value is given using a struct timeval*/
res = select(FD_SETSIZE,&testfd,(fd_set*)0,(fd_set*)0,&t imeout);
//fprintf(stderr,"res= %d\n",res);
//sleep(3);

if(FD_ISSET(sockfd,&testfd))
{

//fprintf(stderr,"hi2");
res = recvfrom(sockfd,buffer,256,0,(struct sockaddr *)&from,&length);

if (res < 0)
{
error("error in recvfrom");
exit (1);
}
flag =0;
write(1,"Got an ack: ",12);
write(1,buffer,res);
}

max_iterations--;
}
close(sockfd);
}

void error(char *msg)
{
perror(msg);
exit(0);
}


Now my Requirement is ...

There are multiple servers(let us assume there are 4 servers)

for contacting each server Requirement 1 conditions should apply..In my packet i have one identifier also .to distinguish each server..

Here problem is if i have send a request to SERVER1...start the timer.....and send the request to SERVER2...start the timer with an interval of 2 sec if server1 is not responding with in 2sec time ..meanwhile server2 has replied now how i have to Recognise from which server it has come ....and also how to maintain timer functionality for all these different requests....

Some one suggested Thread Concept i have to use .But i dont knowhow to do it..

please help me..And thanks allot for showing patience to read my Post..

Reagards,
Keshav.
Feb 21 '07 #1
4 5091
Motoma
3,237 Recognized Expert Specialist
I have moved this to the C/C++ forum, as I feel the experts there will be much more helpful.
Feb 21 '07 #2
horace1
1,510 Recognized Expert Top Contributor
have a look at this thread it may help
http://www.thescripts.com/forum/thread598160.html
Feb 21 '07 #3
gaddamreddy
4 New Member
I have moved this to the C/C++ forum, as I feel the experts there will be much more helpful.
Thanks buddy
Feb 22 '07 #4
horace1
1,510 Recognized Expert Top Contributor
the simplest approach is to have a seperate thread to talk to each server (with seperate timers etc). have a look at
http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
Feb 25 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
3279
by: rdh | last post by:
Hi all, I am in process of developing a Server in C++ supporting multiple protocols. The server will be exposing various functionalities, and the clients can communicate over any of the...
11
6819
by: Tor Erik | last post by:
Hi, The reason is that my application does about 16 connects and data transfers per second, to the same 16 remote hosts. After approx 200 secs there are 4000 sockets waiting to be garbage...
2
7599
by: gaddamreddy | last post by:
Hai to all frns, Reqirement 1: 1.I need to send a Request to one server through UDP socket.at that time i have to start a timer (setted to 2 sec) if i wont get the Response/ACK from that...
5
2781
by: ganeshp | last post by:
Hi , In Java using socket programming is it possible to have a server program on windows that services a client program on linux? I tried the code in the below given link:...
5
6863
by: natambu | last post by:
I have a linux box with multiple ip addresses. I want to make my python client connect from one of the ip addresses. Here is my code, no matter what valid information I put in the bind it always...
3
3463
by: dynamo08 | last post by:
Hi, I am new to socket programming, I wrote an ftpclient which connects to an ftp server and downloads a file to the client computer. Now in one client connection I want to download multiple files...
3
11630
by: TsanChung | last post by:
I want to make a java TCP socket client to communicate with a TCP server socket on linux. Are there some sample C unix server and java client socket programs available? The Richard Stevens'...
3
7165
by: Hukkky | last post by:
I'm testing simple server/client codes on linux. just server can wait for client's connect sign and accept, and client can't connect to server, this is all. There's no problems just for this...
0
7105
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4870
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.