Hello all,
I'm new to the forum and also to python :D. Anyway I have been enjoying it quite a bit and wanted to start into something a bit more technical. So I though sockets would be a good start. I found a great sight , but the first script I found there seems to have some errors.
Could someone plz look this over and fix it for me? And also its been along time since I tackled programming but isn't there like a system pause or something I can stop scripts at a certain point with. If so can someone help refresh my memory. Thanx alot and diggin this sight.:)

code for server

from socket import *
HOST ='localhost'
PORT =21567
BUFSIZ =1024
ADDR =(HOST, PORT)
serversock = socket(AF_INET, SOCK_STREAM)
serversock.bind(ADDR)
serversock.listen(2)

while 1:
    print'waiting for connection...'
    clientsock, addr = serversock.accept()
    print'Connection made from: ', addr

    while 1:
        data =clientsock.recv(BUFSIZ)
        if not data:
            break
            clientsock.send('echoed', data)

    clientsock.close()
serversock.close()

code for client

from socket import *

HOST ='localhost'
PORT =21567
BUFSIZ =1024
ADDR =(HOST, PORT)

tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)

while 1:
    data =raw_input('> ')
    if not data:
        break
    tcpCliSock.send(data)
    data = tcpCliSock.rec(1024)
    if not data:
        break
print data
tcpCliSock.close()

In the server program, I would write

while 1:
        data =clientsock.recv(BUFSIZ)
        if not data:
            break
        clientsock.sendall('echoed ' + data)

and in the client program

tcpCliSock.sendall(data)
    data = tcpCliSock.recv(1024)
    if not data:
        break
    print data

Ok well I made those changes in the code that were kinda screwy, but now the client side hangs instead of printing the message at the end of it. Any ideas?


here is the new code

for server

from socket import *
HOST ='localhost'
PORT =21567
BUFSIZ =1024
ADDR =(HOST, PORT)
serversock = socket(AF_INET, SOCK_STREAM)
serversock.bind(ADDR)
serversock.listen(2)

while 1:
    print'waiting for connection...'
    clientsock, addr = serversock.accept()
    print'Connection made from: ', addr

    while 1:
        data =clientsock.recv(BUFSIZ)
        if not data:
            break
    clientsock.sendall('echoed' + data)

    clientsock.close()
serversock.close()

and for client

from socket import *

HOST ='localhost'
PORT =21567
BUFSIZ =1024
ADDR =(HOST, PORT)

tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)

while 1:
    data =raw_input('> ')
    if not data:
        break
    tcpCliSock.send(data)
    data = tcpCliSock.recv(1024)
    if not data:
        break
    print data
tcpCliSock.close()

You must indent the clientsock.sendall(...) . Otherwise, it seems to work ;)

Hahaha thanx a bunch. Looks like I need to work on my grammer quite a bit. :)

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.