0% found this document useful (0 votes)
10 views8 pages

CN Assig 8 59 Bhawnesh

The document outlines an assignment for a Computer Network Laboratory course, focusing on creating a TCP socket program for client-server communication using various programming languages. It explains the theory behind network sockets, the procedures for establishing communication, and provides example code for both server and client programs in Python. The conclusion emphasizes the importance of these foundational examples for understanding network programming and developing more complex applications.

Uploaded by

shahareebhawnesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

CN Assig 8 59 Bhawnesh

The document outlines an assignment for a Computer Network Laboratory course, focusing on creating a TCP socket program for client-server communication using various programming languages. It explains the theory behind network sockets, the procedures for establishing communication, and provides example code for both server and client programs in Python. The conclusion emphasizes the importance of these foundational examples for understanding network programming and developing more complex applications.

Uploaded by

shahareebhawnesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Computer Network Laboratory

Student Name Bhawnesh Shahare

SRN No 202202319

Roll No 59

Program Computer Engg.

Year Third Year

Division A

Subject Computer Network Laboratory (BTECCE21506)

Assignment No 8
Computer Network Laboratory

Assignment Number - 08
.

Problem Statement : Write a C /C++ / Java / Python socket program for TCP where TCP Client
communicate with TCP server.

Theory :

A network socket is an endpoint of an inter-process communication flow across a computer network.


Today, most communication between computers is based on the Internet Protocol; therefore most network
sockets are Internet sockets.
A socket API is an application programming interface (API), usually provided by the operating system,
that allows application programs to control and use network sockets. Internet socket APIs are usually based
on the Berkeley sockets standard.
A socket address is the combination of an IP address and a port number, much like one end of a telephone
connection is the combination of a phone number and a particular extension. Based on this address, internet
sockets deliver incoming data packets to the appropriate application process or thread.
Procedure in Client-Server Communication
There are some procedures that we have to follow to establish client-server communication. These are as
follows.
Socket: With the help of a socket, we can create a new communication.
Bind: With the help of this we can, we can attach the local address with the socket.
Listen: With this help; we can accept the connection.
Accept: With this help; we can block the incoming connection until the request arrives.
Connect: With this help; we can attempt to establish the connection.
Send: With the help of this; we can send the data over the network.
Receive: With this help; we can receive the data over the network.
Close: With the help of this, we can release the connection from the network.

A connection is a type of relationship between two machines where the two software are known about each
other. These two software know how to establish a connection with each other; in other word s, we can say
that this two software know how to send the bits over the network. A connection of the socket
Computer Network Laboratory
means the two machines should know all the information between each other, like the phone number, IP
address, and the TCP port.

A socket is a type of object which is similar to the file that allows the program to accept the incoming
connection and allow them to send or receive the incoming connection. Also, it is a type of resource
assigned to the server's process.

Figure : TCP Client Server Communication


Computer Network Laboratory

Program :

//TCP SERVER PROGRAM


import socket
import struct

def addition(a, b):


return a + b

def main():
ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ss.bind(('127.0.0.1', 9734))
ss.listen(5)
print("Waiting for connection...")

cc, addr = ss.accept()


print("Connection accepted from", addr)

while True:
data = cc.recv(1024)
if not data:
break

msg = struct.unpack('!ffI', data)


choice = msg[2]

if choice == 1:
print("First Value is", msg[0])
print("Second Value is", msg[1])
result = addition(msg[0], msg[1])
print("Addition is", result)

reply = struct.pack('!f', result)


Computer Network Laboratory
cc.sendall(reply)

cc.close()
ss.close()

if name == " main ":


main()
//TCPCLIENT PROGRAM
import socket
import struct

def main():
cs = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
cs.connect(('127.0.0.1', 9734))
print("Connection successful")

while True:
print("\n\nSELECT Appropriate Option:")
print("1.\tAddition")
print("2.\tDisconnect And Exit")

choice = int(input("Your Choice: "))

if choice == 1:
first_value = float(input("Enter any First value: "))
second_value = float(input("Enter any Second value: "))

msg = struct.pack('!ffI', first_value, second_value, choice)


cs.sendall(msg)

data = cs.recv(1024)
result = struct.unpack('!f', data)[0]
print("Output from server:", result)

elif choice == 2:
break
Computer Network Laboratory

else:
print("Invalid Choice. Select Another.")

cs.close()

if name == " main ":


main()

/************************************
TCP SERVER TERMINAL
*************************************/
C:\Users\Acer>cd Desktop\CN

C:\Users\Acer\Desktop\CN>python server.py
Waiting for connection...
Connection accepted from ('127.0.0.1', 50543)
First Value is 10.0
Second Value is 15.0
Addition is 25.0

C:\Users\Acer\Desktop\CN>
Computer Network Laboratory

/************************************
TCP CLIENT TERMINAL
*************************************/
C:\Users\Acer\Desktop\CN>python client.py
Connection successful

SELECT Appropriate Option:


1. Addition
2. Disconnect And Exit
Your Choice: 1
Enter any First value: 10
Enter any Second value: 15
Output from server: 25.0

SELECT Appropriate Option:


1. Addition
2. Disconnect And Exit
Computer Network Laboratory
Your Choice: 2

C:\Users\Acer\Desktop\CN>

Conclusion : the provided TCP socket examples showcase basic communication between a client and
server in C, C++, Java, and Python. These programs establish connections, allowing data exchange. Key
functions include binding, listening, accepting connections, and data transmission. These foundational
examples serve as starting points for network programming, forming the basis for more complex
applications. Understanding these principles is essential for building distributed systems and facilitates
further exploration of advanced networking concepts.

You might also like

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