In network programming, socket programming is the most basic, all other network programming packages come in on the basis of its
socket object, AF_INET on behalf of ipv4, AF_INET6 on behalf of ipv6, AF_UNIX, unix only
SOCKET_STREAM representatives tcp. SOCK_DGRAM representatives udp
** client no function. server functionality more
TCP client has had server
**
socket preceded by IP, protocol, port, port monitoring need to bind bind it, listen listen port
and then accept, the client receives the link and wait for links, link after receiving, accept will returns a new socket and remote address and port, through the new socket returned by accept it establish a link between the client with the two socket established channels to exchange data
socket is occupied file descriptors, run out need to be returned
client通过指定远程的ip端口,尝试和socket进行链接,accept看到有人来链接,会把这个client链接跟一个新的socket建立链接,
client就和new socket 建立链接了
accept和recv会阻塞住,一个主线程阻塞住,就不能变成群聊了,可以使用多线程,accept就扔到一个线程理
tcp server里面开多个线程,把accept放到单独线程里,这样主线程可以进行管理的工作,网络也属于IO,IO密集型就是多线程
也就是现在的版本,有个ChatServer类,创建socket,初始化IP,端口,listen之后要accept,但是accept会阻塞,就需要开一个线程,每accept一次就创建一个新的线程,
与客户端连接就创建一个新的线程,把socket传递进去,new socket就管理和客户端的通信问题,如果阻塞,最好的办法就是以后开多线程放进去
群聊,就需要记住客户端,就要有个容器存放socket,但是有个删除的过程,就是一个字典
元组能不呢个做字典的key就需要看里面的元素是不是可hash
看到的消息群发给客户端
运行一下,accept启动
1对1没有问题
另外的也可以收到消息,这就是群聊
while TRUE 可以用event代替
创建多线程的event
所有的while true替换一下
客户端应该有个断开机制,进行收尾的工作,比如客户端发送quit,告诉服务器不想跟你玩了
也就是recv拿到消息,要判断这个消息
可以使用strip,判断后面quit字符串如果加了空格算不算退出,可以把两头的空白字符去掉
退出,首先需要把它从字段剔除掉,使用pop(),每个线程启动传入的都是不同的sock和raddr,只要这个线程不退出,sock和raddr都存在
break 以后,,socket关掉,线程就执行完了
pop是有返回值的,可以这么写,但是看起来不舒服,暂时不这么写
客户端发送quit,主动退出,就会在列表里把你直接清除掉。然后socket.close,这里是内建数据结构,GIL这把锁,所以多线程可以这么写,但是JAVA就不可以这么写了(因为有可能其他线程正在读,不能pop,有的线程在读,有的线程在写,不加锁是不行的)
发消息两个都收到了,断其中的一个
另外一个发送消息,但是收是收到了,有一个小问题
问题出在这里
添加一些表明的代码,再次重复刚才的两个链接发消息,然后断开一个
现在这里收到一个空的东西,也就是断开的时候,主动发送一个空串、这里报的windows的错误,底层操作系统对socket的实现还是有些不一样的,windows开发的软件
现在发送一个消息,两个客户端都可以收到
点这个断开会发送一个空串
用自己些的quit
现在就剩一个链接了
现在CS之间进行了一种约定,何时才断开
点软件的断开,就会出现错误
收到一个空串
对38行代码进行修改
再次点链接断开,只要我们断开,其实就给我们发送一个空串
So could write, empty string or quit when will disconnect
now disconnect, and not being given
one is to write their own quit mechanism, and the other is a direct link is disconnected, the server will see the empty string, so direct judgment empty string is also possible, disconnect the time to do clean-up resources, removal from the list
Client disconnects, the server does not know, but here will send an empty string, you can use this to determine disconnection
format first is fmt
logging, info later if there is no handler or to force the creation handler, create only call info