LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG-Social] tcp programming q

[ILUG-Social] tcp programming q

Stephen Shirley diamond at skynet.ie
Tue Sep 11 13:51:13 IST 2001


Mornin',
	I'm writing a program at the moment that needs to know if another
host is up before sending data. The approach I'm using currently is to try
and open a tcp socket on the remote machine that I know isn't been
listened on. Here is the sample code:

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <setjmp.h>

sigjmp_buf jmp_env;

void alarm_handler(int num)
{
    printf("caught alarm\n");
    siglongjmp(jmp_env,1);
}

int main(int argc, char *argv[])
{
    struct sockaddr_in peer;
    int out_tcp_sock_id=socket(AF_INET,SOCK_STREAM,0);
    int ret_val;

    memset(&peer,0,sizeof(peer));
    peer.sin_family=AF_INET;
    inet_aton(argv[1],&peer.sin_addr);
    peer.sin_port=htons(1);

    printf("connecting to %s:%d\n",inet_ntoa(peer.sin_addr),ntohs(peer.sin_port));

    signal(SIGALRM,alarm_handler);
    alarm(1);

    if(sigsetjmp(jmp_env,1) == 0)
    {
        ret_val=connect(out_tcp_sock_id,&peer,sizeof(peer));

        printf("returned from connect with ret_val %d\n",ret_val);

        if(errno==ECONNREFUSED)
            return 0;

        if(ret_val==0)
        {
            close(out_tcp_sock_id);
            return 0;
        }
    }
    return 1;
}



Now - network programming isn't my strong point, so I was wondering if any
of ye lot have any idea if this can be made better (i.e. lower latency or
bandwidth). As it stands, there are only two packets sent, one in each
direction - to me that seems pretty much optimal. BTW the other machine is
a sun box which I have no control over if that makes any difference.

TIA,
Steve
-- 
"My mom had Windows at work and it hurt her eyes real bad"





More information about the Social mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell