473,715 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ 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,r es,max_iteratio ns =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_fami ly = 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,&readf d);

printf("Please enter the message: ");
bzero(buffer,25 6);
fgets(buffer,25 5,stdin);

while(flag && (max_iterations >0))

printf("sending the message %d time\n",++count );
res = sendto(sockfd,b uffer,strlen(bu ffer),0,(struct sockaddr *)&server,lengt h);
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_SETSI ZE,&testfd,(fd_ set*)0,(fd_set* )0,&timeout);
//fprintf(stderr, "res= %d\n",res);

if(FD_ISSET(soc kfd,&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);


void error(char *msg)

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..

Feb 21 '07 #1
4 5116
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
1,510 Recognized Expert Top Contributor
have a look at this thread it may help
http://www.thescripts. com/forum/thread598160.ht ml
Feb 21 '07 #3
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
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/LinuxTutorialPo sixThreads.html
Feb 25 '07 #5

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

Similar topics

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 protocols may be TCP, IPX, SAP, NETBEUI to access the server to access the functionalities exposed. The server doesnot know in advance which client is using what protocol. ALSO, ALL THE INTERACTION WILL BE MADE OVER XML. example my server has...
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 collected by the OS. At this point is seems that connect loops and starts using the same local addresses it used 4000 connections ago, resulting in an "Address already in use" exception. A possible solution to this would be to keep the connection to...
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 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>
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: http://williams.comp.ncat.edu/Networks/JavaSocketExample.htm This does not work when the client program is on a Linux machine. Error: Error Connection refused
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 comes from the default ip address on the server. Am I doing something wrong? ------------- #!/usr/bin/python import socket
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 by forking the parent connection. So far I have this, the code downloads only one file, and then gives the following error "Transfer Completed425 Unable to build data connection: Invalid argument" Appreciate if someone can help me here. The...
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' "Unix network programming" book described a TCP server (tcpcliserv04.c) and client (tcpcli04.c). I compiled and executed them successfully as follows: $ tcpcliserv04 &
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 objects. I'm testing for the situation.. after the connection between server and client is done, if server or client hits the "ctrl+c" key. The problem is either case for server or for client hits the "ctrl +c"key, they goes infinite loop. I...
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.