0% found this document useful (0 votes)
39 views6 pages

National Institute of Technology, Delhi: END SEM Practical Exam

Here are the key steps for UDP client-server communication: Client: 1. Create a socket 2. Connect to server address and port 3. Send data to server using sendto() 4. Receive response from server using recvfrom() 5. Close socket Server: 1. Create a socket 2. Bind to an address and port 3. Receive data from client using recvfrom() 4. Send response to client using sendto() 5. Close socket The main differences between TCP and UDP are: - UDP is connectionless - no handshake. Just send/receive directly. - UDP does not guarantee delivery or order of packets - UDP header

Uploaded by

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

National Institute of Technology, Delhi: END SEM Practical Exam

Here are the key steps for UDP client-server communication: Client: 1. Create a socket 2. Connect to server address and port 3. Send data to server using sendto() 4. Receive response from server using recvfrom() 5. Close socket Server: 1. Create a socket 2. Bind to an address and port 3. Receive data from client using recvfrom() 4. Send response to client using sendto() 5. Close socket The main differences between TCP and UDP are: - UDP is connectionless - no handshake. Just send/receive directly. - UDP does not guarantee delivery or order of packets - UDP header

Uploaded by

Pranshu Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

NATIONAL INSTITUTE OF TECHNOLOGY,

DELHI

Network Programming (CSB-351)

_____________________________________________________________

END SEM Practical Exam


_____________________________________________________________

Submitted To: Submitted By:


Dr.
Assistant Professor 171210043
Dept. of ECE CSE 3rd Year
Ans 1:

(a) Two scenarios:


 When Server handles both TCP and UDP.
 When Client is handling multiple descriptors(like Standard input and
network socket)

(b) Functions called in UDP communication at the client end:


● socket() ● connect() ● sendto() ● recvfrom() ● close()
Functions called in UDP communication at the server end:
● socket() ● bind() ● recvfrom() ● sendto()

(c)
recvfrom():

#include<sys/socket.h>
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t
*addrlen)

sendto():
#include<sys/socket.h>
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,const struct sockaddr *dest_addr,
socklen_t addrlen)

(d) A zombie process is a process whose execution is completed but it still has an
entry in the process table. The zombie processes can be removed from the system by
sending the SIGCHLD signal to the parent, using the kill command. If the zombie
process is still not eliminated from the process table by the parent process, then the
parent process is terminated if that is acceptable.

(e) It suspends execution of the current process until one of its child process
terminates or a signal is received. After child process terminates, parent continues its
execution after wait system call instruction.

Syntax: pid_t wait(int *stat_loc);


Ans2:
Client Side:
Code:

#include <stdio.h>
#include <strings.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>

#define PORT 5000


#define MAXLINE 1000

int main()
{
char buffer[100];
char *message = "Hello Server.Thsi is your Client Pranshu";
int sockfd, n;
struct sockaddr_in servaddr;
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);
servaddr.sin_family = AF_INET;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);

if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)


{
printf("\n Error : Connect Failed \n");
exit(0);
}
sendto(sockfd, message, MAXLINE, 0, (struct sockaddr*)NULL, sizeof(servaddr));
recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)NULL, NULL);
puts(buffer);
close(sockfd);
}

Output:

Server Side:

Code:
#include <stdio.h>
#include <strings.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include<netinet/in.h>
#define PORT 5000
#define MAXLINE 1000

int main()
{
char buffer[100];
char *msg = "Hello Pranshu";
int listenfd, len;
struct sockaddr_in servaddr, cliaddr;
bzero(&servaddr, sizeof(servaddr));

listenfd = socket(AF_INET, SOCK_DGRAM, 0);


servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
servaddr.sin_family = AF_INET;
bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr));

len = sizeof(cliaddr);
int n = recvfrom(listenfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&cliaddr,&len);
buffer[n] = '\0';
puts(buffer);

sendto(listenfd, msg, MAXLINE, 0, (struct sockaddr*)&cliaddr, sizeof(cliaddr));


}
Output:

You might also like