Advertisement
Guest User

Server side chat

a guest
Oct 23rd, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. # Python program to implement server side of chat room.
  2. import socket
  3. import select
  4. import sys
  5. from thread import *
  6.  
  7. """The first argument AF_INET is the address domain of the
  8. socket. This is used when we have an Internet Domain with
  9. any two hosts The second argument is the type of socket.
  10. SOCK_STREAM means that data or characters are read in
  11. a continuous flow."""
  12. server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  13. server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  14.  
  15. # checks whether sufficient arguments have been provided
  16. if len(sys.argv) != 3:
  17.     print "Correct usage: script, IP address, port number"
  18.     exit()
  19.  
  20. # takes the first argument from command prompt as IP address
  21. IP_address = str(sys.argv[1])
  22.  
  23. # takes second argument from command prompt as port number
  24. Port = int(sys.argv[2])
  25.  
  26. """
  27. binds the server to an entered IP address and at the
  28. specified port number.
  29. The client must be aware of these parameters
  30. """
  31. server.bind((IP_address, Port))
  32.  
  33. """
  34. listens for 100 active connections. This number can be
  35. increased as per convenience.
  36. """
  37. server.listen(100)
  38.  
  39. list_of_clients = []
  40.  
  41. def clientthread(conn, addr):
  42.  
  43.     # sends a message to the client whose user object is conn
  44.     conn.send("Welcome to this chatroom!")
  45.  
  46.     while True:
  47.             try:
  48.                 message = conn.recv(2048)
  49.                 if message:
  50.  
  51.                     """prints the message and address of the
  52.                     user who just sent the message on the server
  53.                     terminal"""
  54.                     print "<" + addr[0] + "> " + message
  55.  
  56.                     # Calls broadcast function to send message to all
  57.                     message_to_send = "<" + addr[0] + "> " + message
  58.                     broadcast(message_to_send, conn)
  59.  
  60.                 else:
  61.                     """message may have no content if the connection
  62.                     is broken, in this case we remove the connection"""
  63.                     remove(conn)
  64.  
  65.             except:
  66.                 continue
  67.  
  68. """Using the below function, we broadcast the message to all
  69. clients who's object is not the same as the one sending
  70. the message """
  71. def broadcast(message, connection):
  72.     for clients in list_of_clients:
  73.         if clients!=connection:
  74.             try:
  75.                 clients.send(message)
  76.             except:
  77.                 clients.close()
  78.  
  79.                 # if the link is broken, we remove the client
  80.                 remove(clients)
  81.  
  82. """The following function simply removes the object
  83. from the list that was created at the beginning of
  84. the program"""
  85. def remove(connection):
  86.     if connection in list_of_clients:
  87.         list_of_clients.remove(connection)
  88.  
  89. while True:
  90.  
  91.     """Accepts a connection request and stores two parameters,
  92.     conn which is a socket object for that user, and addr
  93.     which contains the IP address of the client that just
  94.     connected"""
  95.     conn, addr = server.accept()
  96.  
  97.     """Maintains a list of clients for ease of broadcasting
  98.     a message to all available people in the chatroom"""
  99.     list_of_clients.append(conn)
  100.  
  101.     # prints the address of the user that just connected
  102.     print addr[0] + " connected"
  103.  
  104.     # creates and individual thread for every user
  105.     # that connects
  106.     start_new_thread(clientthread,(conn,addr))   
  107.  
  108. conn.close()
  109. server.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement