Hi guys, i'm new to sockets and find it pretty fun. But i got a question, in my code here:

import sys, socket

import socket, sys
dest = ('<broadcast>', 51423)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.sendto("Hi", dest)
print "Looking for replies; press Ctrl-C to stop."
while 1:
    (buf, address) = s.recvfrom(2048)
    if not len(buf):
        break
    print "Received from %s: %s" % (address, buf)

it tells me if theres any packets/messages sent across the network, but when i use it on my wireless nothing happens, even though i may have my friends on it and using the wireless. But i see nothing do you guys have any suggestions? Thanks

I am confused by your choice of using
s = socket.socket(AF_INET, SOCK_STREAM)
then immediatly doing
s = socket.socket(AF_INET, SOCK_DGRAM)
by doing that, you have created a TCP/IP socket, then turned around and turned it into a udp socket. I really dont see why.

Is this meant to be a server or a client. I'm assuming its a server because of the setsockopts, but then you go and set a destination. Decided whether or not its a upd or tcp connection and ill be happy to help

well first off, it's suspose to be a TCP/IP Socket. So thats my mistake on my part if when i created two different types of sockets. and it's a server. Thanks

well, i dont see any calls to sys in the code, so no need to import it

import socket

port = <port number>
host = '' # this will bind to all devices, so connections can from any devices you computer is using

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port)) #this was missing
s.listen(1) #as was this
s.sendto("Hi", dest)
print "Looking for replies; press Ctrl-C to stop."
while 1:
    (buf, address) = s.recvfrom(2048)
    if not len(buf):
        break
    print "Received from %s: %s" % (address, buf)

go ahead and try that.

btw, where did you learn your network coding from?

Well obviously not from a class, but from code basically from what i see. And i'll try things people have posted and learn from the syntax also see how it works what i can do with it and try to place that into codes i think of and ideas that pop in my head mostly. But since I'm young i don't really have an objective to work towards in network programming. Also the code works perfectly thanks :)

not a problem. one thing you need to know is that a server doesnt need a destination.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.