0

I've written a small tcp server in python 2.7. The code is below. It was deployed on Centos 7:

  • At start I got the error "Errno: 104 - Peer closed the socket connection." in wireshark so I've opened the destination port
  • open the port: /etc/sysconfig/iptables added rule: "-A INPUT -m state --state NEW -p tcp --dport 5555 -j ACCEPT" than restarted using "sudo systemctl restart iptables"
  • I tried rebooting etc' still get errors

In wireshark, after opening the port I get error: enter image description here

my python script based on this answer and this post and I see nothing in the log.. like nothing is finishing even the connection.

 def startServer(self,host,port):  
    s = socket.socket()
    try:
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.bind((host,port))
    except  SocketError as e:
        print ("TS: SocketError connection error %s" % e)               
        sys.exit()

    # Tell the server socket file descriptor to destroy itself when this program ends.
    socketFlags = fcntl.fcntl(s.fileno(), fcntl.F_GETFD)
    socketFlags |= fcntl.FD_CLOEXEC
    fcntl.fcntl(s.fileno(), fcntl.F_SETFD, socketFlags)
    s.listen(128)     # Allow a queue of up to 128 requests (connections).
    s.setblocking(0) # Use asynchronous sockets.

    fdmap = {s.fileno():s}

    # Listen to socket events on the server socket defined by the above bind() call.
    epoll = select.epoll()
    epoll.register(s.fileno(), select.EPOLLIN)

    connections = {}
    requests = {}
    responses = {}
    address = {}

    frameinfo = getframeinfo(currentframe()) #PT_INFO: for debug only.. 
    while True:
        try:
            events = epoll.poll(10) #TODO: improve this ... 
        except: #TODO: improve this ... 
            pass
        for fd,event in events:
            if fd is s.fileno():
                # connection is a new socket object.
                # address is client IP address. The format of address depends on the address family of the socket (i.e., AF_INET).
                connection, address = s.accept()
                print "got connnection req from: %s" % str(address)
                # Set new socket-connection to non-blocking mode.
                connection.setblocking(0)
                # Listen for read events on the new socket-connection.
                epoll.register(connection.fileno(), select.EPOLLIN)
                connections[connection.fileno()] = connection
                requests[connection.fileno()] = b''

            else:
                if event & select.EPOLLIN:
                    try:

                        # requests[fd] = connections[fd].recv(1024)
                        while(1):
                            t_data = connections[fd].recv(1024)
                            logLine = ("TS: got data %s" % t_data)
                            data += t_data
                    except  SocketError as e:
                        if ex.errno == 11:  # EAGAIN
                            # Nothing more to read;
                            pass                            
                        continue

                    #do somthing with the data
                    print "got data:" + data

                # A socket was closed on our end.
                elif event & select.EPOLLHUP:
                    print "Closed connection to", connections[fd]['address']
                    epoll.unregister(fd)
                    del connections[fd]

                # Error on a connection.
                elif event & select.EPOLLERR:
                    print "Error on connection to", connections[fd]['address']
                    epoll.modify(fd, 0)
                    connections[fd]['connection'].shutdown(socket.SHUT_RDWR)
                else:
                    #test connnection is still no by writing to the socket
                    try:
                        # todo = 'write to the socket so it will give error'
                        print "sending ' '  to socket to test if it is still up"
                        connection.send(" ")
                    except:
                        print "after sending ' '  to socket got exception"
                        epoll.modify(fd, 0)
                        connections[fd]['connection'].shutdown(socket.SHUT_RDWR)
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
user2495766
  • 123
  • 3
  • 15
  • Where is `s` (the server socket) getting created? – cdarke Mar 22 '15 at 09:16
  • Thank you for the replay, sorry I missed that. I updated the code sample - the server socket is the first thing which is created. – user2495766 Mar 22 '15 at 09:37
  • well opening the port in selinux helped for a moment: all together: semanage port -a -t http_port_t -p tcp 8088 semanage port -a -t http_port_t -p tcp 5555 in centos 7 open the port in iptables under /etc/sysconfig/iptables add: -A INPUT -m state --state NEW -p tcp --dport 8088 -j ACCEPT -A INPUT -m state --state NEW -p tcp --dport 5555 -j ACCEPT – user2495766 Mar 22 '15 at 09:57
  • the previus comment solved some portion of the issue: now data is arriving after several retries (around the 2-3 minutes) the previous error was: "telnet: connect to address 192.168.1.104: Connection refused when" issued: "telnet 192.168.1.104 5555" – user2495766 Mar 22 '15 at 10:09
  • any idea why the delay? is created? [I believe that this is in my python script but don't know what I'm missing] – user2495766 Mar 22 '15 at 10:12

0 Answers0