0

I am setting up a basic TCP server for receiving local connections. I already have client applications with sockets constructed in the following way sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM). I need the client to be able to connect with these parameters even if they aren't required. Something about my very simple server does not allow this connection if those two params are passed in.

I have made sure that the IP and port are available on my computer. I have successfully connected a UDP client to a similar Handler with these parameters passed in. I have gotten a successful TCP connection by just constructing the client as sock = socket.socket() without parameters.

Server File:

class TCPHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        self.data = self.request.recv(1024).strip()
        self.request.sendall(self.data.upper())
...
#(in main)

server = SocketServer.TCPServer((HOST,PORT), TCPHandler)
print "TCP Server Started"

Client File:

    try:
        #sock = socket.socket()
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.settimeout(3.0)
        print "Attempting to TCP connection"
        PORT = 53140
        sock.connect((HOST, PORT))
        print "Connected to TCP"
        sock.sendall(data + "\n")
        received = sock.recv(1024)
        sock.close()
    except Exception as e:
        print e

If I run the code as pasted I get a "Connection Refused" error. If I used the commented line instead, the TCP connection works.

maxopie
  • 21
  • 4
  • TCP servers don't handle UDP datagrams. You'll need to either change your clients to use TCP instead (i.e. SOCK_STREAM, not SOCK_DGRAM), or modify your server so that it receives UDP packets instead of (or in addition to) TCP connections – Jeremy Friesner Jul 18 '19 at 17:58
  • I did not realize that DGRAM was only for UDP. Adding STREAM worked. Thanks for your help! – maxopie Jul 18 '19 at 18:12

0 Answers0