How to create a chat program using python 3.0 or above
If you're a beginner at programming and ever wandered if you could create a chat program using the little knowledge that you have then you are at the right place.This post will show you how a simple chat program can be created and developed over the course of time to function to perfection where a chat server would be handling clients and allowing them to chat either locally or over the internet.
import socket # This is the module that will be used to establish the connection
#and send data from one end to the other
import sys
import time
s = socket.socket() # initialising the socket object allowing connections and data transfer.
host = input(str("Please enter the hostname of the server : ")) # Getting the host address from the users input
port = 8080 # setting the port to the same port as the server program
s.connect((host,port)) # Finally connecting to the server using the host address and the port
print(" Connected to chat server")
while 1:
incoming_message = s.recv(1024)# Waiting to receive any incoming messages
incoming_message = incoming_message.decode() # Decoding the incoming message into string
print(" Server : ", incoming_message) #Displaying the incoming message
print("")
message = input(str(">> ")) # Gathering the input from the user as string
message = message.encode() # Encoding the message so that it can send through the socket
s.send(message) # Sending the message to the connection that connected to the server
print("message has been sent...")
print("")
The components involved in this chat program
The first version of the chat program is very basic and would only involve a single chat server and a chat client meaning that only two people would be able to chat with each other simultaneously.
- Client program
- Server program
How the programs will function ( Flow of Code )
Connection
The server program will host on the local computer on a dedicated port after which the client program will prompt the user to input the server address so it can connect to the server on the dedicated server address and assigned port.
Beginning of the loop
Once the client program has connected to the server program. A loop will begin where two functions will be recalled one after the other. One of the function will be the send and the other would be the receive.
Calling functions
Once the loop starts the send function will allow the user to send a message to the server program which will be running the receive function allowing it to receive and display the message to the user using the server program. In the same way as soon as the message is displayed on the server program the client program will start running the receive function and the server program will now be able to run the send function allowing the user on the server program to send a message to the client program.
Enough of the explanation - Now for the Code
Server Side Code :
import socket # This is the module that will be used to establish the connection
#and send data from one end to the other
import sys
import time
## end of imports ###
s = socket.socket() # initialising the socket object allowing connections and data transfer.
host = socket.gethostname() # Initialising the server address name
print(" server will start on host : ", host)
port = 8080 # Setting the server port ( can be changed )
s.bind((host,port)) # Binding the server to the configured host address and port.
print("")
print(" Server done binding to host and port successfully")
print("")
print("Server is waiting for incoming connections")
print("")
s.listen(1) # Listening for 1 incoming connection
conn, addr = s.accept() # Accepting any incoming connections
print(addr, " Has connected to the server and is now online ...")
print("")
while 1:
message = input(str(">> ")) # Gathering the input from the user as string
message = message.encode() # Encoding the message so that it can send through the socket
conn.send(message) # Sending the message to the connection that connected to the server
print("message has been sent...")
print("")
incoming_message = conn.recv(1024) # Waiting to receive any incoming messages
incoming_message = incoming_message.decode() # Decoding the incoming message into string
print(" Client : ", incoming_message) #Displaying the incoming message
print("")
import socket # This is the module that will be used to establish the connection
#and send data from one end to the other
import sys
import time
## end of imports ###
s = socket.socket() # initialising the socket object allowing connections and data transfer.
host = socket.gethostname() # Initialising the server address name
print(" server will start on host : ", host)
port = 8080 # Setting the server port ( can be changed )
s.bind((host,port)) # Binding the server to the configured host address and port.
print("")
print(" Server done binding to host and port successfully")
print("")
print("Server is waiting for incoming connections")
print("")
s.listen(1) # Listening for 1 incoming connection
conn, addr = s.accept() # Accepting any incoming connections
print(addr, " Has connected to the server and is now online ...")
print("")
while 1:
message = input(str(">> ")) # Gathering the input from the user as string
message = message.encode() # Encoding the message so that it can send through the socket
conn.send(message) # Sending the message to the connection that connected to the server
print("message has been sent...")
print("")
incoming_message = conn.recv(1024) # Waiting to receive any incoming messages
incoming_message = incoming_message.decode() # Decoding the incoming message into string
print(" Client : ", incoming_message) #Displaying the incoming message
print("")
Client Side Code:
import socket # This is the module that will be used to establish the connection
#and send data from one end to the other
import sys
import time
s = socket.socket() # initialising the socket object allowing connections and data transfer.
host = input(str("Please enter the hostname of the server : ")) # Getting the host address from the users input
port = 8080 # setting the port to the same port as the server program
s.connect((host,port)) # Finally connecting to the server using the host address and the port
print(" Connected to chat server")
while 1:
incoming_message = s.recv(1024)# Waiting to receive any incoming messages
incoming_message = incoming_message.decode() # Decoding the incoming message into string
print(" Server : ", incoming_message) #Displaying the incoming message
print("")
message = input(str(">> ")) # Gathering the input from the user as string
message = message.encode() # Encoding the message so that it can send through the socket
s.send(message) # Sending the message to the connection that connected to the server
print("message has been sent...")
print("")
Comments
Post a Comment