473,799 Members | 2,665 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Timeout value on Socket Connect

Good afternoon,

I'm rewriting a piece of perl code in C++ that simply checks
availability of a port.

It's fairly simple, and returns UP, DOWN or NODNS.

This works great, and is just what I need. Except when network issues,
or firewalls drop packets silently. It'll sit and hang around forever,
which grinds the scripts dependant upon it to a halt.

Is there a way of being able to kill the connect() or set a timeout of
say three seconds before assuming a port is DOWN.

I've heard talk of using SIGALARM to break the connect(), but I've no
idea how to implement this. Would anyone mind pointing me in the right
directions?

I'm not concerned with running on anything other than UNIX.

I'm pretty green when it comes to C, so please be gentle.

Thanks in advance, and kind regards
Gareth Crispin
---- CODE ----

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define PORT 13702 // the port client will be connecting to

int main(int argc, char *argv[])
{
int sockfd, numbytes;
struct hostent *he;
struct sockaddr_in their_addr; // connector's
address information

if (argc != 2) {
fprintf(stderr, "usage: client hostname\n");
exit(1);
}

if ((he=gethostbyn ame(argv[1])) == NULL) { // get the host info
//perror("gethost byname");
printf("NODNS") ;
exit(1);
}

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket" );
exit(1);
}

their_addr.sin_ family = AF_INET; // host byte order
their_addr.sin_ port = htons(PORT); // short,
network byte order
their_addr.sin_ addr = *((struct in_addr *)he->h_addr);
memset(&(their_ addr.sin_zero), '\0', 8); // zero the rest
of the struct

if (connect(sockfd , (struct sockaddr *)&their_addr,
sizeof(struct sockaddr)) == -1) {
//perror("connect ");
printf("DOWN");
exit(1);
}

printf("UP");
close(sockfd);
return 0;

---- END CODE ----
Jul 23 '05 #1
1 6544
On Tue, 28 Jun 2005 13:51:24 +0100, Gareth Crispin
<so***@norealad dresshere.com> wrote in comp.lang.c++:
Good afternoon,

I'm rewriting a piece of perl code in C++ that simply checks
availability of a port.


[snip]

news:comp.unix. programmer

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
9597
by: Ahmet AKGUN | last post by:
Hi All; I have one server to which clients are connected to via TcpChannel. I start server on one host and establish a client connection from another host via tcpchannel. On client, using activators, I try to invoke one function on server side using channel After first successfull fucntion invoke, I unplug ethernet cable on server side and
2
5271
by: Ted Burhan | last post by:
I was wondering if there is a way to adjust the timeout period when doing Socket.Connect() I have been looking at the docs, found nothing
0
1422
by: yaron | last post by:
Hi, I want to use non-blocking socket operations (and not async socket operations) When i use this code : socket.Blocking = false; socket.Connect(remote); i get this error :
9
1631
by: yaron | last post by:
Hi, 1. It seems to me that there is a bug when using non-blocking socket connect operation, because the LocalEndPoint, RemoteEndPoint of the Socket are nulls on the client side although that the client and the server are already connected. 2. If it is a bug, is there any hotfix for this ? Thanks.
2
2166
by: Ted Burhan | last post by:
I was wondering if there is a way to adjust the timeout period when doing Socket.Connect() I have been looking at the docs, found nothing
4
9553
by: A.M-SG | last post by:
Hi, How can I increase the HTTP Timeout value at the client side? Thank you, Alan
14
25278
by: DaTurk | last post by:
I am makeing a Multicast server client setup and was wondering what the difference is between Socket.Connect, and Socket.Bind. It may be a stupid question, but I was just curious. Because I tried a test, and I can't bind two sockets to the same port/ip but I can connect with one socket, and bind with another to the same ip/port. Thanks
3
3425
by: doc | last post by:
What will a flash xml client socket connect to? I have a working php TCP/IP server socket bound to a port >1023 and the flash client will not even connect to it. I can connect to it with non-xml client. Can anyone explain this to me - it has been driving me mad for weeks! Please help! Thanks
2
1506
by: ken | last post by:
Can you please tell me what is the timeout value of httplib.HTTP? i.e. how long python will wait for a response in the below code? h = httplib.HTTP(self.url, 8080) h.putrequest('GET', '/sample/?url=' + self.url) h.endheaders() Thank you.
0
1208
by: Qui Sum | last post by:
I write ip/port scanner so users using search on our LAN know if the particular ftp is online or not. The class I write has the following structure: public class IPScanner { public IPScanner() { } private void connect(object data)
0
9550
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,...
0
10269
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10248
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
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, 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...
1
7573
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 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...
0
6811
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();...
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.