Hi,
I have never been into sockets, so I decided to dive in.
With some help from devshed, I managed to make a server as shown below. But I cant get it receive data from client.
Please correct me :)

ERROR:
socket.error: (10057, 'Socket is not connected')

import socket as skt

class MServer:
    def __init__(self):
        print "Welcome to Hosanna Chat server: \n Server started...."
    
    # Make a socket
    def makesock(self, stype = skt.AF_INET, smode = skt.SOCK_STREAM):
        newsock = skt.socket(stype, smode) 
        return newsock
    # Bind socket to address
    def bindsock(self, sock, name="localhost", address_port=80):
        sock.bind((name, address_port))
        
    #Listen to connections
    def listensock(self, max_conn, sock):
        sock.listen(max_conn)
        print "Listening...."
    
    #Aceept the connection
    def acceptsock(self, sock):
        clientsock, address = sock.accept()
        print "Accepted connection from  Client socket Address %s, port %s" %address
    
    #Get data from client
    def getdata(self, buff, sock):
        data = sock.recv(buff)
        return data
        
        
sv1 = MServer()
s1 = sv1.makesock()
sv1.bindsock(s1)
sv1.listensock(2, s1)
while True:
    sv1.acceptsock(s1)
    data = sv1.getdata(1024, s1)
    print data

You should listen to what the computer is telling you! Socket is not connected implies that you forgot to connect your socket!!

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect( (ipaddr, port) )
# ... etc

EDIT: And keep in mind that connect() automatically binds the socket if it isn't already. [ source ] So in your bind function just add the connect() after the bind and hopefully that should get you on your way.

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