On 14 April 2012 17:41, Khalid Al-Ghamdi <emailkg...@gmail.com> wrote:

> Hi All,
>
> (python 3.2 on windows)
>
> I have a couple of questions regarding the below code:
>
> 1- In line (15), what are these variables tcpCliSock, addr supposed to
> hold and do?
> 2- Why do I have to specify the buffer size and what does it mean?
> 3- When I try to run the below code and its corresponding client it works
> ok for the first time, but then it gives me this error:
>
> Traceback (most recent call last):
>   File "C:\Python32\Khalid Stuff\tsTserv3.py", line 12, in <module>
>     tcpSerSock.bind(ADDR)
> socket.error: [Errno 10048] Only one usage of each socket address
> (protocol/network address/port) is normally permitted
>
> I thought it had to do with  the address so I changed the port and it
> worked ok. so,:
>
> A/ isn't the optional tcpSerSock.close() supposed to close the connection
> for later reuse?
> B/ why is it when i go to the IDLE and enter tcpSerSock.close() and it
> accepts it, it still gives the same error and doesn't close the connection
> for reuse by my code?
>
> Thanks a lot
>
>
>    1. from socket import *
>    2. from time import ctime
>    3.
>    4. HOST = ''
>    5.  PORT = 21567
>    6.  BUFSIZ = 1024
>    7.  ADDR =(HOST, PORT)
>    8.
>    9. tcpSerSock = socket(AF_INET, SOCK_STREAM)
>    10. tcpSerSock.bind(ADDR)
>    11.  tcpSerSock.listen(5)
>    12.
>    13. while True:
>    14.      print('waiting for connection ...')
>    15.     tcpCliSock, addr = tcpSerSock.accept()
>    16.     print('...connected from: ', addr)
>    17.
>    18.     while True:
>    19.          data = tcpCliSock.recv(BUFSIZ)
>    20.          if not data:
>    21.              break
>    22.         tcpCliSock.send(bytes('[{}] {}'.format(ctime(),data.decode(
>    'utf-8')),'utf-8'))
>    23.
>    24.     tcpCliSock.close()
>    25.  tcpSerSock.close()
>
>
>
In response to question A:
>From python docs:
http://docs.python.org/py3k/library/socket.html?highlight=socket.close#socket.socket.close

Close the socket. All future operations on the socket object will fail. The
remote end will receive no more data (after queued data is flushed).
Sockets are automatically closed when they are garbage-collected.

Note

close()<http://docs.python.org/py3k/library/socket.html?highlight=socket.close#socket.socket.close>releases
the resource associated with a connection but does not necessarily
close the connection immediately. If you want to close the connection in a
timely fashion, call
shutdown()<http://docs.python.org/py3k/library/socket.html?highlight=socket.close#socket.socket.shutdown>before
close()<http://docs.python.org/py3k/library/socket.html?highlight=socket.close#socket.socket.close>
.

Bodsda


>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to