diff --git a/client.py b/client.py deleted file mode 100644 index 98d837c..0000000 --- a/client.py +++ /dev/null @@ -1,28 +0,0 @@ -import socket - -HEADER = 64 -PORT = 5050 -FORMAT = 'utf-8' -DISCONNECT_MESSAGE = "!DISCONNECT" -SERVER = "10.235.1.101" -ADDR = (SERVER, PORT) - -client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -client.connect(ADDR) - -def send(msg): - message = msg.encode(FORMAT) - msg_length = len(message) - send_length = str(msg_length).encode(FORMAT) - send_length += b' ' * (HEADER - len(send_length)) - client.send(send_length) - client.send(message) - print(client.recv(2048).decode(FORMAT)) - -send("Hello World!") -input() -send("Hello Everyone!") -input() -send("Hello Tim!") - -send(DISCONNECT_MESSAGE) \ No newline at end of file diff --git a/client1.py b/client1.py index 61dcaa5..b0e1b26 100644 --- a/client1.py +++ b/client1.py @@ -1,23 +1,13 @@ import socket -def Main(): +HOST = '10.235.1.146' # Enter IP or Hostname of your server +PORT = 12345 # Pick an open Port (1000+ recommended), must match the server port +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +s.connect((HOST,PORT)) - host= socket.gethostbyname(socket.gethostname()) - port = 4005 - - server = ('10.235.1.101', 4000) - - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s.connect((host,port)) - - message = input("-> ") - while message !='q': - s.sendto(message.encode('utf-8'), server) - data, addr = s.recvfrom(1024) - data = data.decode('utf-8') - print("Received from server: " + data) - message = input("-> ") - s.close() - -if __name__=='__main__': - Main() \ No newline at end of file +#Lets loop awaiting for your input +while True: + command = input('Enter your command: ') + s.send(bytes(command, 'utf-8')) + reply = s.recv(1024) + print(reply) \ No newline at end of file diff --git a/communications b/communications deleted file mode 160000 index 33f9aad..0000000 --- a/communications +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 33f9aad01e287b74c2e54d19e8acec81e48e3056 diff --git a/server.py b/server.py deleted file mode 100644 index 7cab27a..0000000 --- a/server.py +++ /dev/null @@ -1,45 +0,0 @@ -import socket -import threading - -HEADER = 64 -PORT = 5050 -SERVER = "10.235.1.101" -# SERVER = socket.gethostbyname(socket.gethostname()) -print(SERVER) -ADDR = (SERVER, PORT) -FORMAT = 'utf-8' -DISCONNECT_MESSAGE = "!DISCONNECT" - -server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -server.bind(ADDR) - -def handle_client(conn, addr): - print(f"[NEW CONNECTION] {addr} connected.") - - connected = True - while connected: - msg_length = conn.recv(HEADER).decode(FORMAT) - if msg_length: - msg_length = int(msg_length) - msg = conn.recv(msg_length).decode(FORMAT) - if msg == DISCONNECT_MESSAGE: - connected = False - - print(f"[{addr}] {msg}") - conn.send("Msg received".encode(FORMAT)) - - conn.close() - - -def start(): - server.listen() - print(f"[LISTENING] Server is listening on {SERVER}") - while True: - conn, addr = server.accept() - thread = threading.Thread(target=handle_client, args=(conn, addr)) - thread.start() - print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1}") - - -print("[STARTING] server is starting...") -start() \ No newline at end of file diff --git a/server1.py b/server1.py index 5dc2416..ccd27cc 100644 --- a/server1.py +++ b/server1.py @@ -1,23 +1,26 @@ import socket -def Main(): - - host = "10.235.1.101" - port = 4000 - print(host) - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s.bind((host, port)) +HOST = '10.235.1.146' # Server IP or Hostname +PORT = 12345 # Pick an open Port (1000+ recommended), must match the client sport +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +print('Socket created') - print("Server Started") - while True: - data, addr = s.recvfrom(1024) - data = data.decode('utf-8') - print("Message from: " + str(addr)) - print("From connected user: " + data) - data = data.upper() - print("Sending: " + data) - s.sendto(data.encode('utf-8'), addr) - c.close() +#managing error exception +try: + s.bind((HOST, PORT)) +except socket.error: + print ('Bind failed ') -if __name__=='__main__': - Main() \ No newline at end of file +s.listen(5) +print ('Socket awaiting messages') +(conn, addr) = s.accept() +print ('Connected') + +# awaiting for message +while True: + data = conn.recv(1024) + print (data) + + # # Sending reply + conn.send(data+bytes(" works",'utf-8')) + # conn.close() # Close connections