Connecting Server and client into ip address.
Server:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
HOST_NAME=socket.gethostname()
PORT=12345
s.bind((HOST_NAME,PORT))
s.listen(4)
while True:
client, address=s.accept()
print("client is connect and has this address", address)
Client:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
HOST_NAME=socket.gethostname()
PORT=12345
Outpou:
C:\Users\91989\AppData\Local\Programs\Python\Python36\python.exe D:/Localy/clint-server/server.py
client is connect and has this address ('127.0.0.1', 64535)
Message sent to client.
Server:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
HOST_NAME=socket.gethostname()
PORT=12345
s.bind((HOST_NAME,PORT))
s.listen(4)
while True:
client, address=s.accept()
client.send(bytes("hello, how are you?", "utf-8"))
print("client is connect and has this address", address)
Client:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
HOST_NAME=socket.gethostname()
PORT=12345
s.connect((HOST_NAME,PORT))
msg=s.recv(100)
print(msg.decode('utf-8'))
output:
C:\Users\91989\AppData\Local\Programs\Python\Python36\python.exe D:/Localy/clint-server/client.py
hello, how are you?
CHAT with commant run time.
Server:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
HOST_NAME=socket.gethostname()
PORT=12345
s.bind((HOST_NAME,PORT))
s.listen(4)
client,address=s.accept()
while True:
message=input("Server:")
client.send(bytes(message, "utf-8"))
message_from_client=client.recv(50)
print("Client:"+ message_from_client.decode("utf-8"))
Client:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
HOST_NAME=socket.gethostname()
PORT=12345
s.connect((HOST_NAME,PORT))
while True:
msg=s.recv(100)
print("Server:"+msg.decode('utf-8'))
message_to_send=input("Client:")
s.send(bytes(message_to_send, 'utf-8'))
#print(msg.decode('utf-8'))
output:
C:\Users\91989\AppData\Local\Programs\Python\Python36\python.exe D:/Localy/clint-server/server.py
Server:hello
Client:Hi,
Server:How are you?
Client:Gooooooooooddddddddddddd
Server:
Output: (client)
C:\Users\91989\AppData\Local\Programs\Python\Python36\python.exe D:/Localy/clint-server/client.py
Server:hello
Client:Hi,
Server:How are you?
Client:Gooooooooooddddddddddddd
CHAT with Tkinter Server and commant client.
Server:
import socket
from tkinter import *
#===============TKinter Start================
def send(listbox, entry):
message=entry.get()
listbox.insert('end', message)
entry.delete(0, END)
client.send(bytes(message, "utf-8"))
def recieve(listbox):
message_from_client=client.recv(50)
listbox.insert('end', message_from_client.decode("utf-8"))
root=Tk()
entry=Entry()
entry.pack(side=BOTTOM)
listbox=Listbox(root)
listbox.pack()
button=Button(root, text='Send', command=lambda :send(listbox,entry))
button.pack(side=BOTTOM)
rbutton=Button(root, text='Recieve', command=lambda :recieve(listbox))
rbutton.pack(side=BOTTOM)
#===============TKinter Endt================
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
HOST_NAME=socket.gethostname()
PORT=12345
s.bind((HOST_NAME,PORT))
s.listen(4)
client,address=s.accept()
# while True:
# message=input("Server:")
# client.send(bytes(message, "utf-8"))
# message_from_client=client.recv(50)
# print("Client:"+ message_from_client.decode("utf-8"))
root.mainloop
client:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
HOST_NAME=socket.gethostname()
PORT=12345
s.connect((HOST_NAME,PORT))
while True:
msg=s.recv(100)
print("Server:"+msg.decode('utf-8'))
message_to_send=input("Client:")
s.send(bytes(message_to_send, 'utf-8'))
#print(msg.decode('utf-8'))
CHAT with Tkinter Server and Tkinter client.
Server:
import socket
from tkinter import *
#===============TKinter Start================
def send(listbox, entry):
message=entry.get()
listbox.insert('end',"Server:"+message)
entry.delete(0, END)
client.send(bytes(message, "utf-8"))
def recieve(listbox):
message_from_client = client.recv(50)
listbox.insert('end', "Client:"+message_from_client.decode("utf-8"))
root=Tk()
entry=Entry()
entry.pack(side=BOTTOM)
listbox=Listbox(root)
listbox.pack()
button=Button(root, text='Send', command=lambda :send(listbox,entry))
button.pack(side=BOTTOM)
rbutton=Button(root, text='Recieve', command=lambda :recieve(listbox))
rbutton.pack(side=BOTTOM)
root.title("Server")
#===============TKinter Endt================
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
HOST_NAME=socket.gethostname()
PORT=12345
s.bind((HOST_NAME,PORT))
s.listen(4)
client,address=s.accept()
# while True:
# message=input("Server:")
# client.send(bytes(message, "utf-8"))
# message_from_client=client.recv(50)
# print("Client:"+ message_from_client.decode("utf-8"))
root.mainloop()
client:
import socket
from tkinter import *
#===============TKinter Start================
def send(listbox, entry):
message=entry.get()
listbox.insert('end', "Server:"+ message)
entry.delete(0, END)
s.send(bytes(message, "utf-8"))
recieve(listbox)
def recieve(listbox):
message=s.recv(50)
listbox.insert('end', "Server:"+message.decode("utf-8"))
#============== button start =================
root = Tk()
entry = Entry()
entry.pack(side=BOTTOM)
listbox = Listbox(root)
listbox.pack()
button = Button(root, text='Send', command=lambda: send(listbox, entry))
button.pack(side=BOTTOM)
rbutton = Button(root, text='Recieve', command=lambda: recieve(listbox))
rbutton.pack(side=BOTTOM)
root.title("Client")
# ============button end =================
#===============TKinter Wnd================
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
HOST_NAME=socket.gethostname()
PORT=12345
s.connect((HOST_NAME,PORT))
# while True:
# msg=s.recv(100)
# print("Server:"+msg.decode('utf-8'))
# message_to_send=input("Client:")
root.mainloop()
#print(msg.decode('utf-8'))
output:
No comments