'Socket Python 3 UDP ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

I have Socket Problem

import socket

serverName = "herk-PC"
serverPort = 12000

clientSocket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

message = input('input lowercase sentence:')

clientSocket.sendto(message.encode('utf-8'),(serverName, serverPort))

modifiedMessage, serverAddress = clientSocket.recvfrom(2048)


print (modifiedMessage.decode('utf-8'))

clientSocket.close()

This code give me error

Traceback (most recent call last):
  File "J:\Sistem Jaringan\Task I\client.py", line 12, in <module>
    modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

Any solution for my error?



Solution 1:[1]

Either you have no server running at herk-PC:12000 (UDP) or there's a firewall in between. Run the server on your local machine and have the client connect to localhost:12000 instead to make sure everything works first.

If you still have the same kinda problems, have you used bind(('localhost',12000)) on your server?

Solution 2:[2]

#udp_client.py
import socket

target_host = '127.0.0.1'
target_port = 7210

client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
nBytes = client.sendto('ABCDEF'.encode('utf-8'), (target_host, target_port))
print(nBytes, 'Bytes', 'Send OK')`

udp client

#udp_server.py
import socket

bind_host = '127.0.0.1'
bind_port = 7210

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((bind_host, bind_port))

data, addr = sock.recvfrom(4096)
print(data.decode('utf-8'), addr[0], ':', addr[1])

udp_server

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jonas Byström
Solution 2