2020/01/13 02-TCP Group Chat Server side programming

In network programming, socket programming is the most basic, all other network programming packages come in on the basis of its Here Insert Picture Description
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
**
Here Insert Picture Description
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
Here Insert Picture DescriptionHere Insert Picture Description

client通过指定远程的ip端口,尝试和socket进行链接,accept看到有人来链接,会把这个client链接跟一个新的socket建立链接,
client就和new socket 建立链接了
Here Insert Picture Description
accept和recv会阻塞住,一个主线程阻塞住,就不能变成群聊了,可以使用多线程,accept就扔到一个线程理
Here Insert Picture Description
tcp server里面开多个线程,把accept放到单独线程里,这样主线程可以进行管理的工作,网络也属于IO,IO密集型就是多线程
Here Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture Description
也就是现在的版本,有个ChatServer类,创建socket,初始化IP,端口,listen之后要accept,但是accept会阻塞,就需要开一个线程,每accept一次就创建一个新的线程,
Here Insert Picture Description与客户端连接就创建一个新的线程,把socket传递进去,new socket就管理和客户端的通信问题,如果阻塞,最好的办法就是以后开多线程放进去Here Insert Picture Description
Here Insert Picture Description
群聊,就需要记住客户端,就要有个容器存放socket,但是有个删除的过程,就是一个字典Here Insert Picture Description
元组能不呢个做字典的key就需要看里面的元素是不是可hash
Here Insert Picture Description
看到的消息群发给客户端Here Insert Picture Description
运行一下,accept启动 Here Insert Picture Description
1对1没有问题
Here Insert Picture Description
另外的也可以收到消息,这就是群聊Here Insert Picture Description
while TRUE 可以用event代替 Here Insert Picture DescriptionHere Insert Picture Description
创建多线程的eventHere Insert Picture Description
所有的while true替换一下
Here Insert Picture Description
客户端应该有个断开机制,进行收尾的工作,比如客户端发送quit,告诉服务器不想跟你玩了Here Insert Picture Description
也就是recv拿到消息,要判断这个消息Here Insert Picture DescriptionHere Insert Picture Description
可以使用strip,判断后面quit字符串如果加了空格算不算退出,可以把两头的空白字符去掉Here Insert Picture Description
退出,首先需要把它从字段剔除掉,使用pop(),每个线程启动传入的都是不同的sock和raddr,只要这个线程不退出,sock和raddr都存在
break 以后,,socket关掉,线程就执行完了
Here Insert Picture Description
pop是有返回值的,可以这么写,但是看起来不舒服,暂时不这么写Here Insert Picture Description
Here Insert Picture Description
客户端发送quit,主动退出,就会在列表里把你直接清除掉。然后socket.close,这里是内建数据结构,GIL这把锁,所以多线程可以这么写,但是JAVA就不可以这么写了(因为有可能其他线程正在读,不能pop,有的线程在读,有的线程在写,不加锁是不行的)
Here Insert Picture Description
发消息两个都收到了,断其中的一个Here Insert Picture Description
另外一个发送消息,但是收是收到了,有一个小问题Here Insert Picture Description
问题出在这里
Here Insert Picture Description
添加一些表明的代码,再次重复刚才的两个链接发消息,然后断开一个Here Insert Picture Description
现在这里收到一个空的东西,也就是断开的时候,主动发送一个空串Here Insert Picture Description
、这里报的windows的错误,底层操作系统对socket的实现还是有些不一样的,windows开发的软件
Here Insert Picture Description
现在发送一个消息,两个客户端都可以收到Here Insert Picture Description
点这个断开会发送一个空串Here Insert Picture Description
用自己些的quit
Here Insert Picture Description
Here Insert Picture Description
现在就剩一个链接了
Here Insert Picture Description
现在CS之间进行了一种约定,何时才断开
点软件的断开,就会出现错误Here Insert Picture Description
收到一个空串
Here Insert Picture Description
对38行代码进行修改Here Insert Picture Description
再次点链接断开,只要我们断开,其实就给我们发送一个空串Here Insert Picture Description
So could write, empty string or quit when will disconnect Here Insert Picture Description
now disconnect, and not being given Here Insert Picture Description
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

Here Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture Description
Client disconnects, the server does not know, but here will send an empty string, you can use this to determine disconnection Here Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture Description
format first is fmt Here Insert Picture DescriptionHere Insert Picture Description
logging, info later if there is no handler or to force the creation handler, create only call infoHere Insert Picture DescriptionHere Insert Picture Description

Published 130 original articles · won praise 5 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_42227818/article/details/103961014