Skip to main content

Posts

Showing posts from November, 2022

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("network address is :" ,".".join(ip[1:3]))         print("Host address is :" ,ip[3])             else :         print("This ip cannot be divided in network and host

computer networks lab 6 tcp socket

 Write a program using TCP socket for wired network for following a. Say Hello to Each other b. File transfer. Server Program: /****************** SERVER CODE ****************/ #include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> int main(){ int welcomeSocket, newSocket; char buffer[1024]; struct sockaddr_in serverAddr; struct sockaddr_storage serverStorage; socklen_t addr_size; welcomeSocket = socket(PF_INET, SOCK_STREAM, 0); serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(7891); serverAddr.sin_addr.s_addr = inet_addr(“127.0.0.1”); memset(serverAddr.sin_zero, ‘\0’, sizeof serverAddr.sin_zero); bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr)); if(listen(welcomeSocket,5)==0) printf(“Listening\n”); else printf(“Error\n”); addr_size = sizeof serverStorage; newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size); strcpy(buff

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     = &quot;127.0.0.1&quot; localPort   = 20001 bufferSize  = 1024   msgFromServer       = &quot;Hello UDP Client&quot; 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(&quot;UDP server up and listening&quot;)   # Listen for incoming datagrams while(True):     bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)     message = bytesAddressPair[0]     address = bytesAddressPair[1]     clientMsg = &quot;Message from Client:{}&quot;.format(message)     clientIP  = &quot;Client IP Address:{}&quot;.format(address)          print(clientMsg)     print(clientIP)         # Sending a reply to client     UDPServerSoc