Home > other >  Server/client communication problems
Server/client communication problems

Time:01-03

This is the "core python programming (3rd edition)" in the second chapter example, task: the server to accept the client sends data string, and time stamp on it and return to the client,
Unfortunately, running the server code first, then run the client code, did not get any results! Can you tell me how to solve?
The server-side code tsTserv3. Py:
 #! The/usr/bin/env python 

From the socket import *
From the time the import ctime
The HOST='
PORT=21565
BUFSIZ=1024
ADDR=(HOST, PORT)

TcpSerSock=socket (AF_INET SOCK_STREAM)
TcpSerSock. Bind (ADDR)
TcpSerSock. Listen (5)

While True:
Print (' waiting for the connection... ')
TcpCliSock, addr=tcpSerSock. The accept ()
Print ('... Connected the from: 'and addr)

While True:
Data=https://bbs.csdn.net/topics/tcpCliSock.recv (BUFSIZ)
If not data:
Break
TcpCliSock. Send (' % s' [% s] % (
Utf-8 bytes (ctime (), ' '), data))
TcpCliSock. Close ()
TcpSerSock. Close ()


The client code tsTcint3. Py:
 #! The/usr/bin/env python 

From the socket import *

The HOST='127.0.0.1'
PORT=21565
BUFSIZ=1024
ADDR=(HOST, PORT)

TcpCliSock=socket (AF_INET SOCK_STREAM)
TcpCliSock. Connect (ADDR)

While True:
Data=https://bbs.csdn.net/topics/input (' & gt; ') encode ()
If not data:
Break
TcpCliSock. Send (data)
Data=https://bbs.csdn.net/topics/tcpCliSock.recv (BUFSIZ)
If not data:
Break
Print (data, decode (' utf-8))

TcpCliSock. Close ()

  • Related