I was building a simple client/server code and i keep getting this error. I dont understand why (I am trying to get used to python). here is my code:
Server Code:
import socket
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.accpet()
print("...Connected from: "),addr
while True:
data = tcpclisock.recv(BUFSIZ)
if not data:
break
tcpclisock.send('[%s] %s' %(ctime(), data))
tcpclisock.close()
tcpsersock.close()
Client Code:
import socket
from socket import*
from time import ctime
HOST = 'localhost'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
tcpclisock = socket(AF_INET, SOCK_STREAM)
tcpclisock.connect(ADDR)
while True:
data = raw_input('> ')
if not data:
break
tcpclisock.send(data)
data = tcpclisock.recv(BUFSIZ)
if not data:
break
print data
tcpclisock.close()
I get this error:
error: [Errno 10061] No connection could be made because the target machine actively refused it
Try this:
tcpclisock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
This is almost straight from the documents page for socket which you can find here socket
Probably there is no server process running on the server side (due to accpet()
?)
That suggests the remote machine has received your connection request, and send back a refusal (a RST packet). I don't think this is a case of the remote machine simply not having a process listening on that port (but i could be wrong!).
That sounds like a firewall problem. It could be a firewall on the remote machine, or a filter in the network in between, or, perhaps on your local machine - are you running any kind of security software locally?
first run the server script -- which starts listening then open the client .. or -- try to change the port the error simply indicates "that no one is listening"
精彩评论