python socket server accpet 时间,python中Socket.accept()的返回值是多少

本文展示了如何使用Python的socket模块创建一个简单的服务器和四个客户端。服务器监听特定端口,当客户端连接时,服务器打印客户端的IP地址和端口号,并发送确认消息。客户端则连接到服务器并接收回应。
摘要由CSDN通过智能技术生成

I made a simple server and a simple client with socket module in python.

server:

# server.py

import socket

s = socket.socket()

host = socket.gethostname()

port = 1234

s.bind((host, port))

s.listen(5)

while True:

c, addr = s.accept()

print 'Got connection from', addr

c.send('Thank you for your connecting')

c.close()

and client:

#client.py

import socket

s = socket.socket()

host = socket.socket()

port = 1234

s.connect((host, port))

print s.recv(1024)

I started the server and then started 4 clients and got output in server's console as below:

Got connection from ('192.168.0.99', 49170)

Got connection from ('192.168.0.99', 49171)

Got connection from ('192.168.0.99', 49172)

Got connection from ('192.168.0.99', 49173)

what is the second part in the tuple?

解决方案A pair (host, port) is used for the AF_INET address family, where host is a string representing either a hostname in Internet domain notation like 'daring.cwi.nl' or an IPv4 address like '100.50.200.5', and port is an integer.

So the second value is the port number used by the client side for the connection. When a TCP/IP connection is established, the client picks an outgoing port number to communicate with the server; the server return packets are to be addressed to that port number.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值