sockets

sockets

Postby lewashby » Wed Jan 15, 2014 9:39 pm

In this line -> tcpSerSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) what is the first 'socket' after the '=' sign, is that refering to a socket class making tcpSerSock an object or just to a module called socket?

After reading the documentation I see that socket.accept returns a connection and an address pair, but it doesn't say what is returns them in, a list or a tuple or something else? So how would this line work
-> tcpCliSock, addr = tcpSerSock.accept() ?

And can anyone tell me why the following two server/client programs are not working on my system (Linux)? When I run the client it says -> tcpCliSock.connect(ADDR) -> OSError: [Errno 101] Network is unreachable

Client

Code: Select all
#!/usr/bin/python3

from socket import *

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

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

while(True):
    data = input('> ')
    if not data:
        break
    tcpCliSock.send(data)
    data = tcpCliSock.recv(BUFSIZ)
    if not data:
        break
    print(data.decode('utf-8'))

tcpCliSock.close()



Server

Code: Select all
#!/usr/bin/python3

from socket import *
from time import ctime

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

tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)

while True:
    print('waiting for connection...')
    tcpCliSock, addr = tcpSerSock.accept()
    print('...connected from:', addr)

    while True:
        data = tcpCliSock.recv(BUFSIZ)
        if not data:
            break
        tcpCliSock.send('[%s] %s' % (bytes(ctime(), 'utf-8'), data))
       
    tcpCliSock.close()
tcpSerSock.clsoe()
Last edited by stranac on Wed Jan 15, 2014 9:46 pm, edited 1 time in total.
Reason: First post lock.
lewashby
 
Posts: 2
Joined: Wed Jan 15, 2014 9:32 pm

Return to Networking

Who is online

Users browsing this forum: No registered users and 2 guests

cron