GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Feb-2008, 10:28
kterry kterry is offline
New Member
 
Join Date: Feb 2008
Posts: 2
kterry is on a distinguished road

Probing for TCP connections


I am writing an application that will look for a specific TCP connection in windows XP. I am very new to C as I am a PHP programmer. Is there a library that I can use or is there some native function to look at the TCP connections on the machine?

Is there a way to just look at netstat data?

This application will make sure some encoding software is connected to a media server at all times. So I just need it to check if the connection is present.

I will be compiling it to use on Windows XP and I will be using C, not C++

Thanks!
  #2  
Old 14-Feb-2008, 11:56
davis
 
Posts: n/a

Re: Probing for TCP connections


Quote:
Originally Posted by kterry
Is there a way to just look at netstat data?

Yes!

Use the IpHelper API and link to iphlpapi.lib

Check out: msdn2.microsoft.com

...to see if that does about what you're trying to accomplish.


:davis:
  #3  
Old 14-Feb-2008, 12:27
davis
 
Posts: n/a

Re: Probing for TCP connections


Also,

If you want, you can write a simple C program that invokes netstat and dumps that info to a text file and then parse the text file...

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    size_t done;
    char buffer[BUFSIZ] = {0};
    FILE* fp_input;
    system( "netstat -a > output.txt" );
    fp_input = fopen( "output.txt", "r" );
    if( fp_input != NULL )
    {
        do
        {
            done = fread( buffer, BUFSIZ, 1, fp_input );
            printf( "%s", buffer );
        } while( done > 0 );
        printf( "\r\n" );
        fclose( fp_input );
    }
    return 0;
}


Output:

Code:
Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:mysql *:* LISTEN tcp 0 0 *:www *:* LISTEN tcp 0 0 *:ftp *:* LISTEN tcp 0 0 localhost:ipp *:* LISTEN tcp 0 0 localhost:smtp *:* LISTEN tcp6 0 0 *:ssh *:* LISTEN tcp6 0 52 ::ffff:nnn.nn.nnn.n:ssh ::ffff:nnn.nn.nnn.:2131 ESTABLISHED udp 0 0 *:32769 *:* udp 0 0 *:mdns *:* Active UNIX domain sockets (servers and established) Proto RefCnt Flags Type State I-Node Path unix 2 [ ACC ] STREAM LISTENING 17344 /tmp/.X11-unix/X0 unix 2 [ ACC ] STREAM LISTENING 17191 @/var/run/dbus-ZSwXtyhGgP unix 2 [ ACC ] STREAM LISTENING 15124 /var/run/dbus/system_bus_socket unix 2 [ ACC ] STREAM LISTENING 14870 /var/run/acpid.socket unix 2 [ ACC ] STREAM LISTENING 17189 /var/run/sdp unix 2 [ ] DGRAM 8588 @/com/ubuntu/upstart unix 2 [ ACC ] STREAM LISTENING 17205 @/org/bluez/audio unix 2 [ ] DGRAM 8736 @/org/kernel/udev/udevd unix 2 [ ] DGRAM 15212 @/org/freedesktop/hal/udev_event unix 2 [ ACC ] STREAM LISTENING 16529 /var/run/cups/cups.sock unix 2 [ ACC ] STREAM LISTENING 17300 /var/run/gdm_socket unix 2 [ ACC ] STREAM LISTENING 15201 @/var/run/hald/dbus-wjjrwe1FpS unix 2 [ ACC ] STREAM LISTENING 15204 @/var/run/hald/dbus-gZH8KltPsY unix 11 [ ] DGRAM 15034 /dev/log unix 2 [ ACC ] STREAM LISTENING 17107 /var/run/avahi-daemon/socket unix 2 [ ACC ] STREAM LISTENING 16607 /var/run/mysqld/mysqld.sock unix 2 [ ACC ] STREAM LISTENING 15184 @/tmp/dbus-TR3R4juVQD unix 2 [ ] DGRAM 41184 unix 3 [ ] STREAM CONNECTED 41181 unix 3 [ ] STREAM CONNECTED 41180 unix 3 [ ] STREAM CONNECTED 17628 /tmp/.X11-unix/X0 unix 3 [ ] STREAM CONNECTED 17627 unix 3 [ ] STREAM CONNECTED 17614 /tmp/.X11-unix/X0 unix 3 [ ] STREAM CONNECTED 17613 unix 3 [ ] STREAM CONNECTED 17355 /var/run/acpid.socket unix 3 [ ] STREAM CONNECTED 17354 unix 3 [ ] STREAM CONNECTED 17580 /tmp/.X11-unix/X0 unix 3 [ ] STREAM CONNECTED 17348 unix 3 [ ] STREAM CONNECTED 17202 /var/run/dbus/system_bus_socket unix 3 [ ] STREAM CONNECTED 17201 unix 2 [ ] DGRAM 17200 unix 3 [ ] STREAM CONNECTED 17199 /var/run/dbus/system_bus_socket unix 3 [ ] STREAM CONNECTED 17198 unix 2 [ ] DGRAM 17197 unix 3 [ ] STREAM CONNECTED 17182 /var/run/dbus/system_bus_socket unix 3 [ ] STREAM CONNECTED 17181 unix 2 [ ] DGRAM 17165 unix 3 [ ] STREAM CONNECTED 17119 /var/run/dbus/system_bus_socket unix 3 [ ] STREAM CONNECTED 17118 unix 2 [ ] DGRAM 17117 unix 3 [ ] STREAM CONNECTED 17110 /var/run/dbus/system_bus_socket unix 3 [ ] STREAM CONNECTED 17109 unix 3 [ ] STREAM CONNECTED 17104 unix 3 [ ] STREAM CONNECTED 17103 unix 2 [ ] DGRAM 17101 unix 3 [ ] STREAM CONNECTED 17067 /var/run/dbus/system_bus_socket unix 3 [ ] STREAM CONNECTED 17066 unix 2 [ ] DGRAM 16604 unix 3 [ ] STREAM CONNECTED 16516 @/var/run/hald/dbus-wjjrwe1FpS unix 3 [ ] STREAM CONNECTED 16515 unix 3 [ ] STREAM CONNECTED 16513 /var/run/dbus/system_bus_socket unix 3 [ ] STREAM CONNECTED 16512 unix 3 [ ] STREAM CONNECTED 16110 /var/run/acpid.socket unix 3 [ ] STREAM CONNECTED 16109 unix 3 [ ] STREAM CONNECTED 16087 @/var/run/hald/dbus-wjjrwe1FpS unix 3 [ ] STREAM CONNECTED 16083 unix 3 [ ] STREAM CONNECTED 16086 @/var/run/hald/dbus-wjjrwe1FpS unix 3 [ ] STREAM CONNECTED 15668 unix 3 [ ] STREAM CONNECTED 16084 @/var/run/hald/dbus-wjjrwe1FpS unix 3 [ ] STREAM CONNECTED 15654 unix 3 [ ] STREAM CONNECTED 16078 @/var/run/hald/dbus-wjjrwe1FpS unix 3 [ ] STREAM CONNECTED 15641 unix 3 [ ] STREAM CONNECTED 15207 @/var/run/hald/dbus-gZH8KltPsY unix 3 [ ] STREAM CONNECTED 15206 unix 3 [ ] STREAM CONNECTED 15203 /var/run/dbus/system_bus_socket unix 3 [ ] STREAM CONNECTED 15202 unix 3 [ ] STREAM CONNECTED 15190 /var/run/dbus/system_bus_socket unix 3 [ ] STREAM CONNECTED 15189 unix 3 [ ] STREAM CONNECTED 15187 unix 3 [ ] STREAM CONNECTED 15186 unix 3 [ ] STREAM CONNECTED 15188 @/tmp/dbus-TR3R4juVQD unix 3 [ ] STREAM CONNECTED 15185 unix 3 [ ] STREAM CONNECTED 15158 /var/run/dbus/system_bus_socket unix 3 [ ] STREAM CONNECTED 15157 unix 3 [ ] STREAM CONNECTED 15147 /var/run/dbus/system_bus_socket unix 3 [ ] STREAM CONNECTED 15146 unix 2 [ ] DGRAM 15142 unix 3 [ ] STREAM CONNECTED 15127 unix 3 [ ] STREAM CONNECTED 15126 unix 2 [ ] DGRAM 15103

..then, you can set up a cron job (`at' under Windoze) to run your app and do whatever it is that you want based on the result of your parsing.


:davis:
  #4  
Old 14-Feb-2008, 12:32
kterry kterry is offline
New Member
 
Join Date: Feb 2008
Posts: 2
kterry is on a distinguished road

Re: Probing for TCP connections


The text dump is exactly what i was trying to do! Thanks!

This is making me enjoy C. I want to learn the language now.
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
hardware probing using c program helpmeforlinux C Programming Language 0 29-Sep-2006 01:29
mysql_pconnect: Too many connections error agligich MySQL / PHP Forum 6 09-Aug-2005 10:16
Determining the connections in apache mkbijoy Apache Web Server Forum 2 23-Nov-2004 22:11
Http connections mail_as C Programming Language 1 03-Jul-2004 13:16
MySQL persistant connections or not? JdS Web Hosting Forum 0 02-Jun-2002 06:49

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:15.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.