UDP Client Server using connect | C implementation Last Updated : 17 Apr, 2018 Comments Improve Suggest changes Like Article Like Report UDP is a connection less protocol. There is no connection is established between client and server. Creating Standard UDP Client/Server is discussed here Prerequisite : Socket Programming in C/CPP | UDP server-client implementation In UDP, the client does not form a connection with the server like in TCP and instead, It just sends a datagram. Similarly, the server need not to accept a connection and just waits for datagrams to arrive. We can call a function called connect() in UDP but it does not result anything like it does in TCP. There is no 3 way handshake. It just checks for any immediate errors and store the peer's IP address and port number. connect() is storing peers address so no need to pass server address and server address length arguments in sendto(). Necessary Functions : int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); returns : 0 if OK -1 on error arguments : sockfd : File descriptor of socket to be connected. struct sockaddr *servaddr : server address structure. addrlen : length of server address structure. Below is the implementation showing message transfer between server-client : UDP Server code : C // server program for udp connection #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 // Driver code int main() { char buffer[100]; char *message = "Hello Client"; int listenfd, len; struct sockaddr_in servaddr, cliaddr; bzero(&servaddr, sizeof(servaddr)); // Create a UDP Socket 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 server address to socket descriptor bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)); //receive the datagram len = sizeof(cliaddr); int n = recvfrom(listenfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&cliaddr,&len); //receive message from server buffer[n] = '\0'; puts(buffer); // send the response sendto(listenfd, message, MAXLINE, 0, (struct sockaddr*)&cliaddr, sizeof(cliaddr)); } UDP Client code : C // udp client driver program #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 // Driver code int main() { char buffer[100]; char *message = "Hello Server"; int sockfd, n; struct sockaddr_in servaddr; // clear 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; // create datagram socket sockfd = socket(AF_INET, SOCK_DGRAM, 0); // connect to server if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { printf("\n Error : Connect Failed \n"); exit(0); } // request to send datagram // no need to specify server address in sendto // connect stores the peers IP and port sendto(sockfd, message, MAXLINE, 0, (struct sockaddr*)NULL, sizeof(servaddr)); // waiting for response recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)NULL, NULL); puts(buffer); // close the descriptor close(sockfd); } Outputs: Comment More infoAdvertise with us Next Article UDP Client Server using connect | C implementation M Mohityadav Follow Improve Article Tags : Computer Networks Similar Reads TCP Server-Client implementation in C Prerequisites - Socket Programming in C/C++, TCP and UDP server using select, UDP Server-Client implementation in C If we are creating a connection between client and server using TCP then it has a few functionalities like, TCP is suited for applications that require high reliability, and transmissi 4 min read TCP and UDP server using select Prerequisites: TCP UDP In previous articles, we have seen a TCP server and a UDP server. But now we can combine our concurrent TCP echo server and iterative UDP server into a single server that uses select to multiplex TCP and UDP socket. The Select function is used to select between TCP and UDP soc 5 min read RPC Implementation Mechanism in Distributed System In this article, we will go through the concept of the Remote Procedure Call (RPC) and its working mechanism. RPC is an effective mechanism for building client-server systems that are distributed. RPC enhances the power and ease of programming of the client/server computing concept. It's a protocol 5 min read TCP Connection Termination In TCP 3-way Handshake Process we studied that how connections are established between client and server in Transmission Control Protocol (TCP) using SYN bit segments. In this article, we will study how TCP close connection between Client and Server. Here we will also need to send bit segments to a 5 min read Difference Between Connection-oriented and Connection-less Services In computer networks, communication between devices occurs using two types of services: connection-oriented and connectionless. These services define how data is transferred between a source and a destination. Connection-oriented services establish a dedicated connection before data transfer, ensuri 5 min read Like