即时通讯源码php开源版带IM聊天系统app软件
本⽂介绍如何设置构建⼀个系统的即时通讯源码,并允许多个客户端使⽤客户端脚本连接到它,代码使⽤了套接字和线程的概念。
套接字编程:套接字可以被认为是双向通信信道中的端点,在服务器和⼀个或多个客户端之间建⽴通信。在这⾥,我们在每⼀端设置⼀个套接字,并允许客户端通过服务器与其他客户端交互。服务器端的套接字与服务器端的某个硬件端⼝相关联。任何具有与同⼀端⼝关联的套接字的客户机都可以与服务器套接字通信。
完整源码:p
多线程处理:线程是⼀个⼦进程,它独⽴于任何其他线程运⾏⼀组命令。因此,每次⽤户连接到服务器时,都会为该⽤户创建⼀个单独的线程,并且从服务器到客户机的通信是根据为每个客户机的标识⽽创建的套接字对象沿着各个线程进⾏的。
我们需要两个脚本来建⽴这个聊天室。⼀个⽤于保持服务的运⾏,另⼀个⽤于每个客户端都应该运⾏以连接到服务器。
服务器端脚本:服务器端脚本将尝试建⽴⼀个套接字,并将其绑定到⽤户指定的IP地址和端⼝(windows⽤户可能必须在其防⽕墙设置中对指定的端⼝号进⾏例外处理,或者可以使⽤已打开的端⼝)。然后,该脚本将保持打开状态并接收连接请求,并将相应的套接字对象附加到⼀个列表中,以跟踪活动连接。每次⽤户连接
将为该⽤户创建⼀个单独的线程。在每个线程中,服务器等待⼀条消息并将该消息发送给当前正在聊天的其他⽤户。如果服务器在尝试从特定线程接收消息时遇到错误,它将退出该线程。
使⽤:通过选择计算机上的任意节点作为服务器节点,并使⽤该计算机的专⽤IP地址作为服务器IP地址,可以在局域⽹上设置此服务器。
例如,如果⼀个局域⽹分配了⼀组从192.168.1.2到192.168.1.100的专⽤IP地址,那么来⾃这99个节点的任何计算机都可以充当服务器,其余节点可以使⽤服务器的专⽤IP地址连接到服务器节点。必须⼩⼼选择当前未使⽤的端⼝。例如,
可以充当服务器,其余节点可以使⽤服务器的专⽤IP地址连接到服务器节点。必须⼩⼼选择当前未使⽤的端⼝。例如,端⼝22是ssh的默认端⼝,端⼝80是HTTP协议的默认端⼝。所以最好不要使⽤或重新配置这两个端⼝,以使它们免费使⽤。
但是,如果服务器要在本地⽹络之外访问,则需要使⽤公共IP地址。如果来⾃本地⽹络的节点(不是路由器的节点)希望托管服务器,则需要端⼝转发。在这种情况下,我们需要将任何到达公共IP地址的请求重新路由到本地⽹络中的专⽤IP地址,因此需要端⼝转发。
/* Both the server and client script can then be run
from the Command prompt (in Windows) or from bash
Terminal (Linux users) by simply typing
"python chat_server.py " or "python client.py ".
For example, */
python chat_server.py 192.168.55.13 8081
python client.py 192.168.55.13 8081
下⾯是服务器端脚本,必须始终运⾏以保持聊天室的运⾏。
# Python program to implement server side of chat room.
import socket
import select
import sys
'''Replace "thread" with "_thread" for python 3'''
from thread import *
"""The first argument AF_INET is the address domain of the
socket. This is used when we have an Internet Domain with
any two hosts The second argument is the type of socket.
SOCK_STREAM means that data or characters are read in
a continuous flow."""
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# checks whether sufficient arguments have been provided
if len(sys.argv) != 3:
print ("Correct usage: script, IP address, port number")
print ("Correct usage: script, IP address, port number")
exit()
# takes the first argument from command prompt as IP address IP_address = str(sys.argv[1])
# takes second argument from command prompt as port number Port = int(sys.argv[2])
"""
binds the server to an entered IP address and at the specified port number.
The client must be aware of these parameters
"""
server.bind((IP_address, Port))
"""
listens for 100 active connections. This number can be increased as per convenience.
"""
server.listen(100)
list_of_clients = []
def clientthread(conn, addr):
# sends a message to the client whose user object is conn conn.send("Welcome to this chatroom!")
while True:
try:
message = v(2048)php好看主页源码
if message:
"""prints the message and address of the
user who just sent the message on the server terminal"""
terminal"""
print ("<" + addr[0] + "> " + message)
# Calls broadcast function to send message to all message_to_send = "<" + addr[0] + "> " + message broadcast(message_to_send, conn)
else:
"""message may have no content if the connection
is broken, in this case we remove the connection""" remove(conn)
except:
continue
"""Using the below function, we broadcast the message to all clients who's object is not the same as the one sending
the message """
def broadcast(message, connection):
for clients in list_of_clients:
if clients!=connection:
try:
clients.send(message)
except:
clients.close()
# if the link is broken, we remove the client
remove(clients)
"""The following function simply removes the object
from the list that was created at the beginning of
the program"""
def remove(connection):
if connection in list_of_clients:
list_ve(connection)
while True:
"""Accepts a connection request and stores two parameters,
conn which is a socket object for that user, and addr
which contains the IP address of the client that just
connected"""
conn, addr = server.accept()
"""Maintains a list of clients for ease of broadcasting
a message to all available people in the chatroom"""
list_of_clients.append(conn)
# prints the address of the user that just connected
print (addr[0] + " connected")
# creates and individual thread for every user
# that connects
start_new_thread(clientthread,(conn,addr))
conn.close()
server.close()
客户端脚本:客户端脚本将简单地尝试访问在指定的IP地址和端⼝创建的服务器套接字。⼀旦连接,
它将持续检查输⼊是来⾃服务器还是来⾃客户端,并相应地重定向输出。如果输⼊来⾃服务器,则在终端上显⽰消息。如果输⼊来⾃⽤户,则它将⽤户输⼊的消息发送到服务器,以便将其⼴播给其他⽤户。
这是客户端脚本,每个⽤户必须使⽤该脚本才能连接到服务器。
# Python program to implement client side of chat room.
import socket
import select
import sys
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if len(sys.argv) != 3:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。