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

Computer networks lab 5 subnetting code

 def findClass(ip):     if (ip[0]>=0 and ip[0]<=126):         return "A"     elif(ip[0]>=128 and ip[0]<=191):         return "B"     elif(ip[0]>=192 and ip[0]<=223):         return "C"     elif(ip[0]>=224 and ip[0]<=239):         return "D"     else:         return "E"         def separate (ip, className):     if (className=="A"):         print("Network address is : " , ip[0])         print("Host address is : ",".".join(ip[1:4]))             elif(className=="B"):         print("Network address is : ",".".join(ip[0:2]))         print("Host address is : ",".".join(ip[2:4]))             elif(className=="C"):         print("netw...

Sem 7 Practical Exam

 ML LAB ASSIGNMENTS  LAB-1 https://colab.research.google.com/drive/1iQXKgk2nsysWXnO7xbajrSjGcYlReIhE LAB-2 https://colab.research.google.com/drive/1Ilmd52oXNojLpIUKNpVGkDiweL3smrvz LAB-3 https://colab.research.google.com/drive/1U6tD9lT7ktMxfDFVQRXTGGMh7sSA_-Sf LAB-4 https://colab.research.google.com/drive/1S3LFQdfJ0sodYIrNBIvCbuineHN5xRBs LAB-5 https://colab.research.google.com/drive/1u61o7jXxmqw4gnvF-YBKHElMDjHDwBSN LAB-6 https://colab.research.google.com/drive/1-LdxSoE2oKJr9Q84qWPH4Xo5uM7aDvLB QAI LAB ASSIGNMENTS  LAB-1 https://colab.research.google.com/drive/1w8j9LEK_ihcgc6QYhR-s_WQ0FZLbcF3t LAB-2 https://colab.research.google.com/drive/1l10kBvC7qNS2L17f3VBmpyDpNFlG2Mj7 LAB-3 https://colab.research.google.com/drive/1V2u0BZP63lZT9rMYRTcs88XnMNK0Uz6z LAB-4 https://colab.research.google.com/drive/1i7NJukPd-TQ5VAJP02TEOKCArEylmoGp LAB-6 https://colab.research.google.com/drive/1EvbH1DuDC06dkHCJi4XUd6iG87w3DhAl DMV LAB ASSIGNMENTS  LAB-1 https://colab.research.google.co...