CN Assig 8 59 Bhawnesh
CN Assig 8 59 Bhawnesh
SRN No 202202319
Roll No 59
Division A
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 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.
Program :
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...")
while True:
data = cc.recv(1024)
if not data:
break
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)
cc.close()
ss.close()
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")
if choice == 1:
first_value = float(input("Enter any First value: "))
second_value = float(input("Enter any Second value: "))
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()
/************************************
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
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.