Skip to main content

Computer Networks lab 5 socket programming

 Socket Programming using C/C++/Java / python.

a. TCP Client, TCP Server

b. b. UDP Client, UDP Server.



Example: UDP Server using Python


import socket

 

localIP     = "127.0.0.1"

localPort   = 20001

bufferSize  = 1024

 

msgFromServer       = "Hello UDP Client"

bytesToSend         = str.encode(msgFromServer)

 

# Create a datagram socket

UDPServerSocket = socket.socket(family=socket.AF_INET,

type=socket.SOCK_DGRAM)

 

# Bind to address and ip

UDPServerSocket.bind((localIP, localPort))

 

print("UDP server up and listening")

 

# Listen for incoming datagrams

while(True):

    bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)

    message = bytesAddressPair[0]

    address = bytesAddressPair[1]

    clientMsg = "Message from Client:{}".format(message)

    clientIP  = "Client IP Address:{}".format(address)

    

    print(clientMsg)

    print(clientIP)

   

    # Sending a reply to client

    UDPServerSocket.sendto(bytesToSend, address)

Output:


UDP server up and listening

Message from Client:b"Hello UDP Server"

Client IP Address:("127.0.0.1", 51696)


 Example: UDP Client using Python

import socket

 

msgFromClient       = "Hello UDP Server"

bytesToSend         = str.encode(msgFromClient)

serverAddressPort   = ("127.0.0.1", 20001)

bufferSize          = 1024

 

# Create a UDP socket at client side

UDPClientSocket = socket.socket(family=socket.AF_INET,

type=socket.SOCK_DGRAM)

 

# Send to server using created UDP socket

UDPClientSocket.sendto(bytesToSend, serverAddressPort)

 

msgFromServer = UDPClientSocket.recvfrom(bufferSize)

 

msg = "Message from Server {}".format(msgFromServer[0])

print(msg)





Comments

Popular posts from this blog

FIRST YEAR SPPU NOTES

FIRST YEAR SPPU NOTES  ENGINEERING MECHANICS : Unit 1 -  OPEN PDF Unit 2 -  OPEN PDF Unit 3 -  OPEN PDF Unit 4 -  OPEN PDF ENGINEERING GRAPHICS : Unit 1 -  OPEN PDF Unit 2 -  OPEN PDF Unit 3 -  OPEN PDF Unit 4 -  OPEN PDF ENGINEERING MATHEMATICS - 2 : NIRALI PRAKASHAN UNIT 1 TO 4 -  OPEN  PDF ENGINEERING PHYSICS :                                  COMING SOON BASIC ELECTRONICS ENGINEERING :                                  COMING SOON

BACHELOR OF ENGINEERING IN ARTIFICIAL INTELLIGENCE AND DATA SCIENCE | B.E. (AI-DS)

Artificial Intelligence and Data Science is an interdisciplinary branch of science, engineering and technology creating a complete ecosystem and a paradigm shift in virtually every sector of the technical industry, academics and research. Artificial Intelligence and Data Science is the future of technology which are changing the world at very high pace. The basic objectives of this course is to train students with the next age of Intelligence and analytics generated by machines, influencing nearly every facet of our lives to help improve efficiencies and augment human capabilities, influencing consumer products with significant breakthroughs in healthcare, manufacturing, finance and retail industries. With the tremendous amount of data generated every day and the computing power available, Data Science plays important role helping every business organisation in identifying business trends and changes through advanced Big Data Analytics with variety of techniques and tools to interpret...