Rachel Q
Rachel Q

Reputation: 23

Errno 61 connection refused for UDP client server application

We are trying to run a simple UDP client server application.

UDPServer.py

from socket import *
serverPort = 12000
serverSocket = socket(AF_INET,SOCK_DGRAM)
serverSocket.bind(('',serverPort))
print "The server is ready to receive"
while 1:
    message, clientAddress = serverSocket.recvfrom(2048)
    modifiedMessage = message.upper()
    serverSocket.sendto(modifiedMessage, clientAddress)

UDPClient.py

from socket import *
serverName = 'servername'
serverPort = 12000
clientSocket = socket(socket.AF_INET, socket.SOCK_DGRAM)
message = raw_input('Input lowercase sentence:')
clientSocket.sendto(message,(serverName, serverPort))
modifiedMessage = clientSocket.recvfrom(1024)
print modifiedMessage
clientSocket.close()

The server runs first, but the client gets the error 'Errno 61 connection refused', why?

Upvotes: 2

Views: 2318

Answers (2)

Pierz
Pierz

Reputation: 8158

I tried the example out but I had to alter these two lines in the client to get it to run (and then it seems to run fine over a network):

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

However I have seen the 'Errno 61 connection refused' with UDP connections - Which usually indicates an ICMP Destination Unreachable response has been received in reply to a packet sent to an unbound port on the server. But this only results in a Errno 61 with a socket that has called connect((server,port)), and which has then been used to send() an initial packet, and subsequently (with enough delay for the ICMP packet to be received) calls recv() - which raises an exception containing the Errno 61 (see this answer to find out how to catch it).

Upvotes: 0

Kill Console
Kill Console

Reputation: 2023

If you run the two program in the same machine, change to client serverName to serverName = 'localhost'. If in two different machines, the serverName should be the server's IP address, and shutdown the firewall.

Upvotes: 1

Related Questions