Jae
Jae

Reputation: 1157

Socket unable to receive bytes

I am trying to create a packet sniffer using sockets for learning purposes. On the tutorials I see, it seems to be pretty straightforward. First create a socket (in my case TCP), then receive incoming packets.

This is my current code in Python, however it does not work. My program stops running at recvfrom and I do not know why.

import socket, sys
from struct import *
# import os

def main():    
    # default port for socket 
    port = 80
    
    # 1) create an INET, raw socket
    try: 
        # socket1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
        socket1 = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
        print "Socket successfully created"
    except socket.error , msg:
        print 'Socket could not be created. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
        sys.exit()

    while True:       
        packet = socket1.recvfrom(1000) # HALTS HERE
        print("after recvfrom")

Upvotes: 0

Views: 34

Answers (1)

Barak Friedman
Barak Friedman

Reputation: 279

Your socket is not connected to anything (as a client), nor is it bound to port (as a server or a connectionless UDP socket). What did you expect to read from the socket ?

Upvotes: 0

Related Questions