diff --git a/README.md b/README.md index 4adef36e..abf06648 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,67 @@ Socket programming finds applications in various domains, including web developm 4. Networked Games: Online multiplayer games rely on socket programming to facilitate communication between game clients and servers. 5. RPC mechanisms: which allow processes to execute code on a remote server, often use socket programming for communication. +<<<<<<< HEAD + +======= +## Program: +# Client.py +``` +import socket + +# Create a socket object +client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + +# Connect to the server +client_socket.connect(('localhost', 8000)) + +# Print the client's socket name +print(f"Client connected from: {client_socket.getsockname()}") + +# Receive a message from the server +server_message = client_socket.recv(1024).decode() +print(f"Received from server: {server_message}") + +# Send a message to the server +client_socket.send("Acknowledgement received from the client.".encode()) + +# Close the connection +client_socket.close() +``` +# Server.py +```import socket + +# Create a socket object +server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + +# Bind the socket to the host and port +server_socket.bind(('localhost', 8000)) + +# Listen for incoming connections (max 1 connection) +server_socket.listen(1) +print("Server is waiting for a connection...") + +# Accept the connection +conn, addr = server_socket.accept() +print(f"Connected by {addr}") + +# Send a message to the client +conn.send("Hello from the server!".encode()) + +# Receive a message from the client +data = conn.recv(1024) +print(f"Received from client: {data.decode()}") + +# Close the connection +conn.close() +server_socket.close() +``` +## Output: + +# client: +![alt text]() +# server: +![alt text]() ## Result: Thus the study of Socket Programming Completed Successfully diff --git a/Screenshot 2026-04-24 141858.png b/Screenshot 2026-04-24 141858.png new file mode 100644 index 00000000..38048d19 Binary files /dev/null and b/Screenshot 2026-04-24 141858.png differ diff --git a/Screenshot 2026-04-24 141908.png b/Screenshot 2026-04-24 141908.png new file mode 100644 index 00000000..9a75847b Binary files /dev/null and b/Screenshot 2026-04-24 141908.png differ diff --git a/client.py b/client.py new file mode 100644 index 00000000..5e2034db --- /dev/null +++ b/client.py @@ -0,0 +1,20 @@ +import socket + +# Create a socket object +client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + +# Connect to the server +client_socket.connect(('localhost', 8000)) + +# Print the client's socket name +print(f"Client connected from: {client_socket.getsockname()}") + +# Receive a message from the server +server_message = client_socket.recv(1024).decode() +print(f"Received from server: {server_message}") + +# Send a message to the server +client_socket.send("Acknowledgement received from the client.".encode()) + +# Close the connection +client_socket.close() diff --git a/server.py b/server.py new file mode 100644 index 00000000..b456686d --- /dev/null +++ b/server.py @@ -0,0 +1,26 @@ +import socket + +# Create a socket object +server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + +# Bind the socket to the host and port +server_socket.bind(('localhost', 8000)) + +# Listen for incoming connections (max 1 connection) +server_socket.listen(1) +print("Server is waiting for a connection...") + +# Accept the connection +conn, addr = server_socket.accept() +print(f"Connected by {addr}") + +# Send a message to the client +conn.send("Hello from the server!".encode()) + +# Receive a message from the client +data = conn.recv(1024) +print(f"Received from client: {data.decode()}") + +# Close the connection +conn.close() +server_socket.close()