4

I'm doing an assignment regarding socket programming in python using a client and server. I'm currently on windows 10. Before getting into the little details of the assignment, I've been trying to simply connect the server and client.

Every time I try to run the client file, I would get this error

File "tcpclient.py", line 9, in <module>
    s.connect((host, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

I have opened the firewall ports and still nothing. I've tried replacing host with '', 0.0.0.0, socket.gethostname() in both the client and server file but the error still persists. I've even tried different port numbers but it made no difference. I've tried running this code on Ubuntu and Max and I get the same error - connection refused. I've been researching for many solutions but I still have yet to find one that works. Any help would be greatly appreciated!

Note: this code was taken online but it's essentially the basis of what I need to accomplish. tcpclient.py

import socket

host = '127.0.0.1'
port = 80
buffer_size = 1024
text = "Hello, World!"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.send(text)
data = s.recv(buffer_size)
s.close()

print("received data:", data)

tcpserver.py

import socket

host = '127.0.0.1'
port = 80
buffer_size = 20  

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(1)

conn, addr = s.accept()
print 'Connection address:', addr
while 1:
    data = conn.recv(buffer_size)
if not data: break
print("received data:", data)
conn.send(data)  # echo
conn.close()
3
  • You are starting the server before trying the client, right? (Note also that port numbers below 1024 often require privilege to listen on.) Commented Feb 16, 2019 at 21:04
  • i've tried running the server first but every time I do my terminal just gets stuck and I can't ctrl+x or ctrl+c my way out. I've also tried running the client and server at the same time but I would still get the same error for client and then get stuck in the terminal because of the server. Commented Feb 16, 2019 at 21:13
  • @DavisHerring I've tried using a port over 1024 and it worked! (on my ubuntu virtual box at least) I thought I had privileges on my windows but I guess not. Thank you :) Commented Feb 16, 2019 at 21:35

5 Answers 5

1

Just use a different port. Both the client and server should have the same port and host if not it won't work. Make sure to run the server before the client script.

Sign up to request clarification or add additional context in comments.

Comments

0

For client.py

import socket
host = '127.0.0.1'
port = 9879
buffer_size = 1024
text = "Hello, World!"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
text = text.encode('utf-8')
s.send(text)
data = s.recv(buffer_size)
s.close()
print("received data:", data)

For server.py

import socket
mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
buffer_size = 1024
text = "Hello, World!"
mysocket.bind(('127.0.0.1', 9879))
mysocket.listen(5)
(client, (ip,port)) = mysocket.accept()
print(client, port)
client.send(b"knock knock knock, I'm the server")
data = client.recv(buffer_size)
print(data.decode())
mysocket.close()

Just change the port number and it will work and if you are in python3 then you will have to encode and decode as socket recieves and sends only binary strings.

Comments

-1

I was following a tutorial that used threading to start the server. Once I removed the threading then I was able to connect to it.

Comments

-1

Shut down Windows Defender. It helps very good.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
-2

I have succeed in my server!

My server python script is below:

    import socket
    host='0.0.0.0'
    port=2345
    s=socket.socket()
    s.bind((host,port))
    s.listen(2)
    while True:
    conn,addr=s.accept()
    print("Connected by",addr)
    data=conn.recv(1024)
    print("received data:",data)
    conn.send(data)
    conn.close()

My Client python script is below:

import socket
s=socket.socket()
host="xx.xx.xx.xx"       #This is your Server IP!
port=2345
s.connect((host,port))
s.send(b"hello")
rece=s.recv(1024)
print("Received",rece)
s.close()

There is two points needed to be careful in the script:

1.The host of the Server must is

'0.0.0.0'

So that the python script could user all interfaces in the server

2.I have find the question's error through the prompt:

TypeError: a bytes-like object is required, not 'str'

It means every string message in the 'send' method need to convert to 'bytes-like object',So the correct is

s.send(b"hello")

It is important that this is b'hello' not is 'hello'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.