I am trying to run this program but it is giving "unable to open socket" eror.
i am running this program on a server via putty which is hosted in my univerity server with wireless network.
the code is :

#include <pthread.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <linux/if.h>
#include <sys/ioctl.h>

#define PROTOCOL htons(0x0800)
#define BUFFERLENGTH 1500

char *r_cInterface = NULL;		//stores the name of interface to be captured from
int r_iTime = 0;			//time for which sniffer will run
int sock;				//socket to be opened and programmed for capturing
long r_lPacketCount = 0;		//no of packets captured
int r_iContinue = 1;			//usd for thread
struct ip *r_pIPHeader = NULL;		//ipheader

pthread_t r_Capture;			//capturing thread
void initializeInterface (char *r_cInterface);	//programs interface in promic mode
void *captureTraffic(void * args);		//capture packets from interface and displays 
void capture_start ();				//start capturing thread
void capture_stop ();				//stop capturing thread

void initializeInterface(char *r_cInterface)
	struct ifreq ifr;		//structure to hold interface data
	strcpy (ifr.ifr_name, r_cInterface);	//copy name of interface into structure
	if ( (sock=socket(PF_PACKET,SOCK_DGRAM,PROTOCOL)) < 0)	//open socket on interface
		printf("\nUnable to open Socket");
		if ((ioctl (sock , SIOCGIFFLAGS , &ifr)) < 0)	// get flags of socket into structure
		close (sock);
		printf("\nUnable to get flags");
		exit (EXIT_FAILURE);
		ifr.ifr_flags |= IFF_PROMISC;	//set flags to promisc
		if (ioctl (sock , SIOCSIFFLAGS , &ifr) < 0)	//send structure data back to socket flags
		close (sock);
		printf("\nUnable to set flags");
		exit (EXIT_FAILURE);

void *captureTraffic(void * args)
	char r_caBuffer [BUFFERLENGTH];		//holds bytes recieved
   	r_lPacketCount = 0;
	int i=0;
	int r_iBytesReceived = 0;
	while (r_iContinue)
		r_iBytesReceived = recvfrom (sock,r_caBuffer,BUFFERLENGTH,0,NULL,NULL);
		r_lPacketCount ++;
		r_pIPHeader = (struct ip *)r_caBuffer;
		printf("\n\n\nPacket Number : %d",r_lPacketCount);
      		printf("\nSource IP     : %s",inet_ntoa(r_pIPHeader->ip_src));
      		printf("\nDestination IP: %s",inet_ntoa(r_pIPHeader->ip_dst));
      		for (i=0;i<r_iBytesReceived;i++)
      			if ((i%10) == 0)
   	}  // end while

void capture_start ()	{
	int result;
	r_iContinue = 1;
	result = pthread_create (&r_Capture, NULL, captureTraffic, (void *) 0);//create capturing thread
	if (result != 0) {
		printf("\nunable to start sniffer\n");

void capture_stop ()	{
	r_iContinue = 0;

int main (int argc, char * argv[])
	if (argc != 3)
   	printf ("\nUSAGE: sniffer <monitoring interface> <time>\n");
	r_cInterface = argv[1];			//get interface name from input
   	r_iTime = (int)atol(argv[2]);		//get running time from input
	capture_start ();
	capture_stop ();
	return 0;

Edited 7 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

This article has been dead for over six months. Start a new discussion instead.