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

Sem 8 Practical Exam

 DC LAB ASSIGNMENTS  https://github.com/siddharthlanke/Sem-VIII-Academic-Labs/tree/main LAB-1 LAB-2 LAB-3 LAB-4 https://colab.research.google.com/drive/1wf96qi9RH3oGN039QbcmU6s3PEY6txn7?usp=sharing LAB-5 https://colab.research.google.com/drive/1i_a5owxrYdRB1ZQPa6vWGsKdC0ALiSHU?usp=sharing LAB-6 https://colab.research.google.com/drive/1CdPxCss_ppyZf4rCdiEl12CQSarvTBh9?usp=sharing CI LAB ASSIGNMENTS  LAB-1 https://colab.research.google.com/drive/1s07YgacC2nTC-vQUxtT3dKifgKZBwCaI?usp=sharing LAB-2 https://colab.research.google.com/drive/1wj6x-eunLDBx3J8Q-nheShHTMftyvgDu?usp=sharing LAB-3 https://colab.research.google.com/drive/1gw4ZekLF5WYVg3r4Rn0gnwKg177rkMmo?usp=sharing LAB-4 https://colab.research.google.com/drive/1WO0bPhM_y3xSMrNLJBjvVQ7gNpGJHCys?usp=sharing DL LAB ASSIGNMENTS  LAB-1 LAB-2 https://colab.research.google.com/drive/1324qVAvwPeqWoOQgEZ7SHRXwCVQVVZs1?usp=sharing LAB-3 https://colab.research.google.com/drive/1RCX1xvUXex6ksUtQtnj5zpoOGM_fipbr?usp=sharing LAB-4 https://colab.

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.com/drive/1YHv4aatqmi