简体   繁体   中英

“socket.error: [Errno 11] Resource temporarily unavailable” appears randomly

I wanted to make a more complex server in Python, reciving lists of commands and returning lists of values, so I can send more unsing a socket. Sometimes it works, and sometimes it crashes, and as I am new to socket programming, I have no explnation why this is the case?

Here is the Server:

import socket
import errno
import pickle

def Main():
    host = '192.168.43.121'
    port = 12345

    all_text = ['text1', 'text2', 'text3']
    music_text = ['Konzert1', 'Konzert2', 'Konzert3']

    all_description = ['Test \n Description1\n', 'Test \n Description1\n', 'Test \n Description1\n']

    all_images = ['unlock.png', 'unlock.png', 'unlock.png']
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
    s.bind((host, port))
    s.listen(1)


    while True:

        c, addr = s.accept()
        c.setblocking(0)

        print "Connection from: " + str(addr)


        pcommand = c.recv(1024)
        command = pickle.loads(pcommand)

        if command[0] == 'GIVEALL':
            textstring = pickle.dumps([all_text, all_images, all_description])#verwandelt Liste in String
            c.send(textstring)

        elif command[0] == 'GIVEMUSIC':
            textstring = pickle.dumps([music_text, all_images, all_description])#verwandelt Liste in String
            c.send(textstring)

        elif command[0] == 'ADD':
            try:
                new_event = pickle.loads(command)
                print new_event
                caption = command[1]
                image = command[2]
                describtion = command[3]
                city = command[4]
                #add to SQL
            except:
                pass

        try:
            c.close()
            #s.setsockopt(socket.AF_INET, socket.SOCK_STREAM, 1)
            s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
        except socket.error as e:
            if e.errno != errno.ECONNRESET:
                raise
            pass

if __name__ == '__main__':
    Main()

And Here is the Client:

import socket
import pickle

from kivy.properties import StringProperty
from kivy.properties import NumericProperty
from kivy.properties import ListProperty

class Netclient(object):

    def __init__(self):
        self.texte = []
        self.current = 'All'
        self.city = 'Pawonkuw'
        self.ip = '192.168.43.121'
        self.port = 12345

    def giveWid(self):
        print 'give Widgets executed'
        if self.current == 'All':
            self.texte, self.images, self.description = self.sentHOT(self.ip, self.port)
        elif self.current == 'Music':
            self.texte, self.images, self.description = self.sentMusic(self.ip, self.port)

        return self.texte, self.images, self.description

    def sentHOT(self, host, port):
        self.s = socket.socket()
        #print self.current

        self.s.connect((host, port))
        command = ['GIVEALL', self.city]
        pcommand = pickle.dumps(command)
        self.s.send(pcommand)#sends command

        recived_string = self.s.recv(1023)

        more_text = pickle.loads(recived_string)#verwandelt string in liste

        self.s.close()
        #print 'closed'
        return more_text[0], more_text[1], more_text[2]

    def sentMusic(self, host, port):
        self.s = socket.socket()

        self.s.connect((host, port))
        command = ['GIVEMUSIC', self.city]
        pcommand = pickle.dumps(command)
        self.s.send(pcommand)#sends command

        recived_string = self.s.recv(1023)

        more_text = pickle.loads(recived_string)#verwandelt string in liste

        self.s.close()

        self.images = ['unlock.png', 'unlock.png', 'unlock.png']

        #print more_text[0]
        return more_text[0], more_text[1], more_text[2]

    def add_event(self, caption, image, description, city='Pawonkow'):
        self.s = socket.socket()

        self.s.connect((self.ip, self.port))

        new_event = ['ADD', caption, image, description, self.city]
        new_compact_event = pickle.dumps(new_event)

        self.s.send(new_compact_event)

        self.s.close()



n = Netclient()
t, i, d = n.giveWid()
print t
n.add_event('new', 'new.png', 'ew event', 'Hanau')

t, i, d = n.giveWid

And here is the full traceback:

Connection from: ('192.168.43.121', 43169)
Connection from: ('192.168.43.121', 43170)
Traceback (most recent call last):
  File "server2.py", line 61, in <module>
    Main()
  File "server2.py", line 28, in Main
    pcommand = c.recv(1024)
socket.error: [Errno 11] Resource temporarily unavailable

Please Help

Ok, I solved it, the Problem was in the server, I needed to handle the error, by adding a try and except:

import socket
import errno
import pickle

def Main():
    host = 'localhost'
    port = 12345

    all_text = ['text1', 'text2', 'text3']
    music_text = ['Konzert1', 'Konzert2', 'Konzert3']

    all_description = ['Test \n Description1\n', 'Test \n Description1\n', 'Test \n Description1\n']

    all_images = ['unlock.png', 'unlock.png', 'unlock.png']
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
    s.bind((host, port))
    s.listen(1)


    while True:

        c, addr = s.accept()
        c.setblocking(0)

        print "Connection from: " + str(addr)

        try:
            pcommand = c.recv(2048)  # here was the error
        except IOError as e:  # and here it is handeled
            if e.errno == errno.EWOULDBLOCK:
                pass

        command = pickle.loads(pcommand)

        if command[0] == 'GIVEALL':
            textstring = pickle.dumps([all_text, all_images, all_description])#verwandelt Liste in String
            c.send(textstring)

        elif command[0] == 'GIVEMUSIC':
            textstring = pickle.dumps([music_text, all_images, all_description])#verwandelt Liste in String
            c.send(textstring)

        elif command[0] == 'ADD':
            try:
                new_event = pickle.loads(command)
                print new_event
                caption = command[1]
                image = command[2]
                describtion = command[3]
                city = command[4]
                #add to SQL
            except:
                pass

        try:
            c.close()
            #s.setsockopt(socket.AF_INET, socket.SOCK_STREAM, 1)
            s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
        except socket.error as e:
            if e.errno != errno.ECONNRESET:
                raise
            pass

if __name__ == '__main__':
    Main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM