0
我試圖創建一個使用p2p(點對點)連接的基本即時消息程序,因此它不需要服務器。我很確定沒有錯,但每次運行我創建的客戶端程序時,都會出現以下錯誤: s.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) NameError:全局名稱'SOL_SOCKET'未定義 這裏是程序:錯誤 - 全局名稱'SOL_SOCKET未定義
import socket
def Receiver():
# Create socket that supports IPv4, TCP Protocol
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "Socket created."
# Requests for IP of host (DNS)
dns = "localhost"
HOST = ''
PORT = 25395
try:
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
except socket.error as serror:
print "socket error"
s.bind((HOST, PORT)) # Listens on all interfaces...
print 'Listening on port 25565'
s.listen(True) # Listen on the newly created socket...
conn, addr = s.accept()
print 'Connected in port 25565'
data = conn.recv(1024)
print data
s.close()
def Sender():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
dns = "localhost"
HOST = socket.gethostbyname(dns)
port = 25565
# Connect to server
s.connect((host,port))
print "Socket connected to " + dns + " on IP " + host
# Assign message to be sent to server to a variable
message = raw_input("Message to be sent to server: ")
#Send the actual message to server
s.sendall(message)
print "Message sent successfully"
s.close()
input = raw_input('S is send, R is receive: ')
if input == 's':
Sender()
if input == 'r':
Receiver()
我試圖消除s.setsockopt(SOL_SOCKET,SO_REUSEADDR,1),但它告訴我,我不能使用2個插槽的同一個端口上時,有使用同一端口不是2個插座。
現在,它給了我這個錯誤: Errno 10013]嘗試以訪問權限禁止的方式訪問套接字 – spider93286
別擔心,我修好了。 – spider93286
@ spider93286你是如何修復它的?我想知道。 –