0% found this document useful (0 votes)
16 views10 pages

Part 7

cn lab 8

Uploaded by

Danish Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
16 views10 pages

Part 7

cn lab 8

Uploaded by

Danish Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 10
OUTPUT: SERVER: RCO er ne tae Sree rere) ‘ a maneeeween Tee ethan tie rity mete toe ste RO Teer et enc terea Sees Ceeer rere Enter the nessage Taipan Sree Enter the nessage... we elon rere Soest Ce ee CLIENT: Cee eee eer java uses or overrides a d Poetic mes Ear maectte cs RESULT: Thus the program was executed and output is verified successfully. c. File Transfer include include include include Hinclude include include include include include int main (void) ‘ int sockfd = 0; int bytesReceived = 0; char recvBuff(256]; memset (recvBuff, '0', sizeof (recvBuft)); struct sockaddr in serv_addr; /* Create a socket first +/ L£((sockfd = socket(AF_INET, SOCK STREAM, 0))< 0) t print£("\n Error : Could not create socket \n' return 1; 1 /* initialize sockaddr in data structure +/ serv_addr.sin family = AF_Iuet; servladdr.sin port = htons (5000); // port sery_addr.sin_addz.s addr = inet_addr("127.0.0.1"} /* Attempt a connection */ if(connect (sockfd, (struct sockaddr *)sserv_addr, sizeof (serv_addr))<0) t print£("\n Error : Connect Failed \n"); return 1; } f* Create file where data will be stored */ ELLE *£p; fp = fopen("sample_file.tat™, “ab"); LE(NULL == £p) t printf("Error opening file"); return 1; 1 /* Receive data in chunks of 256 bytes */ while |(bytesReceived = read(sockfd, recvBuff, 256)) > 0) { printf ("Bytes received %d\n",bytesReceived) ; 77 vecvBuff{nl = 07 furite(recvBuff, 1,bytesReceived, fp) 7 // printt ("ts \n", recvBufe); ) if (bytesReceived < 0) ( printé("\n Read Error \n"); ’ return 0; ’ Server: Hinclude #include #include Hinclude Hinclude #include #include #include #include int main (void) ( int listenta int connfd = 0; struct sockaddr_in serv_addrs char sendBuf£(1025]; int numrv; oF Jistenfd = socket (AF_INET, SOCK STREAM, 0); printf ("Socket retrieve success\n"); memset (éserv_addr, '0', sizeofiserv_addr}); memset (sendBuff, '0', sizeof (sendBuft)); serv_addr.sin_family = AF_INET? serv_addr.sin_addr.8_addr = htonl (TNADDR_ANY) serv_addr.sin port = htons (5000); bind(listenfd, (struct sockaddr*) éserv_addr, sizeof (serv_addr)}; if(listen(listenfd, 10) == -1) ( print£("Failed to listen\n"); return -1; while (1) ( connfd = accept (listenfd, [struct sockaddr*)NULL ,NULL) + /* Open the file that we wish to transfer */ FILE *fp = fopen("sample_file.txt","cb"); Lf (Ep==NULL) ( printf ("File opern error"); return 17 /* Read data from file and send it “/ while (1) ( /* Birst read file in chunks of 256 bytes */ unsigned char buff[256)=(0}; int nread = fread (buff, 1,256, fp) + printf ("Bytes read $d \n", nreadh; /* If read was success, send data. */ if(nzead > 0) ‘ printé ("Sending \n"); write(connfd, buff, nread); } if (nread < 256) ‘ if (feof (fp)) printé("End of file\n"); if (ferror (fp) ) printf("Error reading\n")+ break; ) close (connid) ; sleep(1)7 return 0; 9. Applications using TCP and UDP Sockets like 4. DNS AIM: IMPLEMENTATION OF SIMPLE DNS To write a UDP program for implementing the simple DNS service. ALGORITHM: UDP Echo Server PNPeAEN Include necessary header files to support functions for Socket definitions Socket types, Internet ‘addresses,l/O functions,UNIX system calls, Declare variables for Socket ID,Port number, Socket addresses,Character buffer etc. Create socket for server. Bind socket with addresses, ‘Specify number of allowed connections, Retrieve information from Connected Socket. Echo retrieved information to Connected Socket, Close Connected Socket. UDP Echo Client Noes eN Include necessary header files to support functions for Socket defini addresses,l/O functions, UNIX system calls, Declare variables for Socket ID,Port number, Socket addresses Character buffer ete, Create socket for alien. Connect client socket to the server socket addresses. Give message to be echoed. Retrieve echoed information from server and display it. Close the Socket, ns, Socket types, Internet SERVER PROGRAM import java.net"; import java i class pisender{ public static void main(String argsf) throws Exception { byle senderBuffer(] = new byte(128); 1 Packets 128 bytes long String toName, userin; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket senderSocket = new DatagramSocket(999); 11 Create socket for sending it (args.length > 0) toName = aras{O}; IP of DNS from command line else toName = "localhost while (userin=in.teadLined)!=null , 1 Read standard input senderBuffer-userin getBytes); J Convert from string to byte array 1/ Send datagram packet out socket senderSocket, send(new DatagramPacket( senderBuffer, senderBuffer.length, InetAddress.getByName(toName), 666); CLIENT PROGRAM Import java.net"; class ptreceiver { Public static void main(String args[) throws Exception { byte receiveDatal] = new bytel1 28}; DatagramSooket receiveSooket = new DatagramSocket(666); _// Create socket for receiving DatagramPacket receivePacket = new DatagramPacket(receiveData, 128); while (true) { receiveSocket.receive(receivePacket); Wait for datagram packet to arrive 1 Print packet contents as String ‘System.out.printin(new String (receivePacket.getData(, 0, receivePacket.getLength()); n ouTPUT: SERVER Ee eons Cen SCC Coe Uy Nlabprogran>java pisender localhost struct y_hiat_entry *elm, *elm2; int num_later = 1; elm = STAILQ FIRST (ar hist_}; while (elm I= NULL 6S num_Tater <- num_dup_acks_) um_later; elm = STATL NEXT(elm, linfo ); ) £ (elm != NULL) { elm ~ findDataPacketInRecvil story (STAILG_NEXT (elm, 2info_) if (elm != NULL) ( elm2 = STATLG_NEXT(eIm, Linfo_}; while (elm2 != NOLL) f (elm2->seq_num_ < seq_num 66 elm2->t_recv ime) ( STATLO_REMOVE (@r_hist_,elm2,r_hist_entry,linfo_} delete eim2; } else elm = eln2; elm2 = STAILG_NEXT(eIm, linfo_): ) ) void DCCPYERCAgent: :removeAcksReevistory ) { struct © hist entry *elml = STAILO FIRST (ér hist); stzuct F hist entry *elm2; int num later = 1; while (elm! != NULL ¢& num later om Lats simi = STAILQ NEXT (etm, Linfo_); num_dup_acks_){ ) if (elm == NULL) return; elma = STAILG_NEXT(eImL, info); while(elm2 != NULL) { Af (elm2->type_ == DCCP_ACK) ( STATLG REMOVE (ar _hist_,eln2,r_hist_entry,linfo_}s delete elma; ) else ( elm] = elma; ) elma = STATIO NEXT(e1m1, Linfo_); inline : hist_eatry SDCCPTREAgent#:findDataPackstInRecvilistory(r hist entry *start) ( while(start != NULL && start->type_ == DCCP_ACK) start = STAILQ NEXT(start, [info ); return starts )

You might also like