所以我一直在聊天,我完成了它(使用套接字模块)。 这是我的代码:
import socket
print("Make sure to answer everything correctly or else it won't work\n")
my_ip = input('Your ip: ')
my_port = int(input('A random port number: '))
server_ip = input('Server ip: ')
server_port = int(input('Server port: '))
server = (server_ip, server_port)
def main():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((my_ip, my_port))
count = 0
name = input('Name: ')
while True:
if count == 0:
msg = input('--> ')
if msg.lower() == 'q':
break
end_msg = name + ': ' + msg
s.sendto(end_msg.encode('utf-8'), server)
count += 1
elif count == 1:
data, addr = s.recvfrom(1024)
print(data.decode('utf-8'))
msg = input('--> ')
if msg.lower() == 'q':
break
end_msg = name + ': ' + msg
s.sendto(end_msg.encode('utf-8'), server)
else:
pass
s.close()
main()
import socket
host = input('Host/ip: ')
port = int(input('Port number: '))
users = []
def main():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))
print('[!] Server started\n')
while True:
try:
data, addr = s.recvfrom(1024)
if addr not in users:
users.append(addr)
print('[*] Message from {} | {}'.format(addr, data.decode('utf-8')))
for user in users:
s.sendto(data, user)
except:
pass
s.close()
main()
如你所见,我在客户端代码上有输入,我尝试使用不同的端口但是当我使用端口3时,我收到错误'Persmission denied'。这没什么大不了的,但我很感兴趣为什么会发生这种情况,有人可以向我解释一下吗?感谢
答案 0 :(得分:0)
在许多系统上,只有root可以绑定到低于1024的端口。