Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Chat example with socket lib #30

Merged
merged 1 commit into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Chat example with socket lib/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import socket


def Main():
host = 'localhost'
port = 1234

my_Socket = socket.socket()
my_Socket.connect((host, port))

print("Connected! {}:{}".format(host, port))

message = input(" -> ")
print("Waiting Server...")

while message != 'q':
my_Socket.send(message.encode())
data = my_Socket.recv(1024).decode()

print('Server: ' + data)

message = input(" -> ")
print("Waiting Server...")

my_Socket.close()


if __name__ == '__main__':
Main()
35 changes: 35 additions & 0 deletions Chat example with socket lib/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import socket
import time


def Main():
host = "localhost"
port = 1234

my_Socket = socket.socket()
my_Socket.bind((host, port))

my_Socket.listen(1)
conn, addr = my_Socket.accept()
print("Connection from: " + str(addr))
while True:
while True: # this line, if the client closed the connection, it waits for new connections.
try:
data = str(conn.recv(1024).decode())
print("Client: " + data)
break
except ConnectionResetError:
time.sleep(1)
conn, addr = my_Socket.accept()
print("Connection from: " + str(addr))
if data == "q":
break
else:
message = input(" -> ")
print("Waiting Client...")
conn.send(message.encode())
conn.close()


if __name__ == '__main__':
Main()
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy