UDP Client Server using connect | C implementation Last Updated : 09 Dec, 2025 Comments Improve Suggest changes 3 Likes Like Report UDP is a connectionless protocol. No connection is established between the client and the server. In UDP, the client does not form a connection like TCP; instead, it simply sends datagrams. Similarly, the server does not accept connections and only waits for incoming datagrams.Although the connect() function can be used with UDP, it does not perform a three-way handshake like TCP. Instead, it only associates a default peer address (IP and port) with the socket. After calling connect(), the application can use send() and recv() instead of sendto() and recvfrom().. Necessary Functions : int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);returns : 0 if OK -1 on errorarguments :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, strlen(message), 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: Create Quiz Comment M Mohityadav Follow 3 Improve M Mohityadav Follow 3 Improve Article Tags : Computer Networks Explore Computer Network BasicsBasics of Computer Networking4 min readTypes of Computer Networks6 min readIntroduction to Internet5 min readNetwork Devices4 min readWhat is OSI Model? - Layers of OSI Model11 min readTCP/IP Model6 min readOSI and TCP/IP Model4 min readPhysical LayerPhysical Layer in OSI Model3 min readTypes of Network Topology9 min readTransmission Modes3 min readTransmission Media in Computer Networks7 min readData Link LayerData Link Layer in OSI Model4 min readSwitching | Computer Networks3 min readVirtual LAN (VLAN)3 min readFraming in Data Link Layer3 min readError Control in Data Link Layer3 min readFlow Control4 min readPiggybacking in Computer Networks2 min readNetwork LayerNetwork Layer in OSI Model3 min readIntroduction of Classful IP Addressing7 min readClassless Addressing in IP Addressing7 min readWhat is an IP Address?11 min readIPv4 Datagram Header4 min readDifference Between IPv4 and IPv63 min readPublic and Private IP addresses4 min readIntroduction To Subnetting5 min readWhat is Routing?10 min readNetwork Layer Protocols9 min readTransport LayerTransport Layer in OSI Model4 min readTransport Layer Protocols9 min readTransmission Control Protocol - TCP4 min readUser Datagram Protocol - UDP3 min readSession Layer & Presentation LayerSession Layer in OSI model2 min readPresentation Layer in OSI model2 min readSecure Socket Layer (SSL)4 min readPoint-to-Point Tunneling Protocol - PPTP2 min readMultipurpose Internet Mail Extension (MIME) Protocol3 min readApplication LayerApplication Layer in OSI Model4 min readClient-Server Model3 min readWorld Wide Web (WWW)5 min readIntroduction to Electronic Mail4 min readWhat is a Content Distribution Network and how does it work?4 min readProtocols in Application Layer4 min readAdvanced TopicsWhat is Network Security?4 min readQuality of Service and Multimedia5 min readAuthentication in Computer Network3 min readEncryption, Its Algorithms And Its Future6 min readIntroduction of Firewall in Computer Network3 min readMAC Filtering in Computer Network3 min readWi-Fi Standards Explained2 min readWhat is Bluetooth?6 min readGenerations of wireless communication2 min readCloud Networking4 min readPracticeTop 50 Plus Networking Interview Questions and Answers15+ min readTop 50 TCP/IP Interview Questions and Answers 202515+ min readNetwork Fundamentals Interview Questions - Computer Networks15+ min readLast Minute Notes for Computer Networks14 min readComputer Network - Cheat Sheet15+ min read Like