简体   繁体   English

Socket.Accept永远占用,并且无阻塞返回错误

[英]Socket.Accept taking forever, and non-blocking returning error

I am having trouble running a code that simply connects to my own IP address. 我无法运行只连接到我自己的IP地址的代码。 The socket.send function works just fine, but the socket.accept takes too long to run. socket.send函数工作正常,但socket.accept运行时间太长。 Can somebody help me? 有人能帮助我吗? This is probably a simple answer I haven't seen yet, as I'm only 13. 这可能是一个我还没有看到的简单答案,因为我只有13岁。

I've checked if the port is correct by scanning for it with another code, and I've run it on the python IDLE and in Mac terminal. 我通过用另一个代码扫描它来检查端口是否正确,我在python IDLE和Mac终端上运行它。 I've tried 'www.google.com','localhost','','127.0.0.1', and my personal IP address, yet socket.accept still doesn't work 我试过'www.google.com','localhost','','127.0.0.1'和我的个人IP地址,但socket.accept仍然不起作用

code: scanner code, returns 20000, AKA it didn't find a port 代码:扫描器代码,返回20000,AKA它没有找到端口

import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);
for port in range(0,20001):
    try:
        s.setblocking(False)
        s.bind(('127.0.0.1',port))
        s.listen(1);
        c,addr=s.accept()
        break
    except:
        if port%1000==0:
            print(port)

accept code: 接受代码:

import socket
s=socket.socket(socket.AF_UNIX,socket.SOCK_DGRAM);
s.setblocking(False)
s.bind(('127.0.0.1',20000))
s.listen(1);
c,addr=s.accept()

I want the accept code to return nothing, and the scanner to return a number from 0-20000. 我希望接受代码不返回任何内容,并且扫描程序返回0-20000之间的数字。 This is my first post on Stack-overflow, so please tell me what to do better on my next post (: 这是我关于Stack溢出的第一篇文章,所以请告诉我在下一篇文章中做得更好(:

This line is incorrect for receiving an incoming TCP connection: 此行不正确,无法接收传入的TCP连接:

s=socket.socket(socket.AF_UNIX,socket.SOCK_DGRAM);

It should instead be: 它应该是:

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);

Also, s.setblocking(False) is probably not something you want to do, since you probably want accept() to block until an incoming TCP connection has been received. 此外, s.setblocking(False)可能不是您想要做的事情,因为您可能希望accept()阻塞,直到收到传入的TCP连接。

Update: in addition to the problem above, the other problem is that the alleged port-scanning code posted in the question doesn't actually scan any ports, since it doesn't ever attempt to make an outgoing TCP connection. 更新:除上述问题外,另一个问题是问题中发布的所谓端口扫描代码实际上并不扫描任何端口,因为它不会尝试进行传出TCP连接。 For reference, here is a version of that code that actually tries to connect to ports 0-20000 on localhost and then prints out which ports it was able to make a connection to: 作为参考,这里是该代码的一个版本,它实际上尝试连接到localhost上的端口0-20000,然后打印出它能够连接到哪些端口:

import socket

connectedPorts = []
for port in range(0,20001):
    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);
    try:
        s.connect(('127.0.0.1',port))
        print("Connect succeeded on port %i" % port)
        connectedPorts.append(port)
    except Exception as e:
        print "Port %i: %s" % (port, e)
    s.close()

print("")
print("Connect succeeded on the following ports: ", connectedPorts)

... and for completeness, here is the working accept-on-port-20000 code: ...为了完整起见,这是工作上的接受端口20000代码:

import socket

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);
s.bind(('127.0.0.1',20000))
s.listen(1);
print("Calling accept...")
c,addr=s.accept()
print("Accept returned: [%s, %s]" % (c,addr))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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