Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //UDP_SERVER_datetime:
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <time.h>
- #define PORT 8080
- #define MAXLINE 1024
- int main() {
- int sockfd;
- char buffer[MAXLINE];
- char *hello = "Hello from server";
- struct sockaddr_in servaddr, cliaddr;
- if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
- perror("socket creation failed");
- exit(EXIT_FAILURE);
- }
- memset(&servaddr, 0, sizeof(servaddr));
- memset(&cliaddr, 0, sizeof(cliaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_addr.s_addr = INADDR_ANY;
- servaddr.sin_port = htons(PORT);
- if (bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
- perror("bind failed");
- exit(EXIT_FAILURE);
- }
- int len, n;
- len = sizeof(cliaddr);
- n = recvfrom(sockfd, (char *)buffer, MAXLINE, MSG_WAITALL, (struct sockaddr *)&cliaddr, &len);
- buffer[n] = '\0';
- printf("Client : %s\n", buffer);
- time_t rawtime;
- struct tm *timeinfo;
- time(&rawtime);
- timeinfo = localtime(&rawtime);
- char datetime[MAXLINE];
- strftime(datetime, MAXLINE, "%Y-%m-%d %H:%M:%S", timeinfo);
- size_t response_length = strlen(hello) + strlen(datetime) + 4;
- if (response_length >= MAXLINE) {
- fprintf(stderr, "Response length exceeds buffer size\n");
- exit(EXIT_FAILURE);
- }
- char response[MAXLINE];
- if (snprintf(response, MAXLINE, "%s - %s", hello, datetime) >= MAXLINE) {
- fprintf(stderr, "Response truncated due to buffer size\n");
- exit(EXIT_FAILURE);
- }
- sendto(sockfd, (const char *)response, strlen(response), MSG_CONFIRM, (const struct sockaddr *)&cliaddr, len);
- printf("Hello message sent.\n");
- return 0;
- }
- //UDP_CLIENT_datetime:
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <time.h>
- #define PORT 8080
- #define MAXLINE 1024
- int main() {
- int sockfd;
- char buffer[MAXLINE];
- char *hello = "Hello from client";
- struct sockaddr_in servaddr;
- if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
- perror("socket creation failed");
- exit(EXIT_FAILURE);
- }
- memset(&servaddr, 0, sizeof(servaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_port = htons(PORT);
- servaddr.sin_addr.s_addr = INADDR_ANY;
- int n, len;
- time_t rawtime;
- struct tm *timeinfo;
- time(&rawtime);
- timeinfo = localtime(&rawtime);
- char datetime[MAXLINE];
- strftime(datetime, MAXLINE, "%Y-%m-%d %H:%M:%S", timeinfo);
- size_t message_length = strlen(hello) + strlen(datetime) + 4;
- if (message_length > MAXLINE) {
- fprintf(stderr, "Message length exceeds buffer size\n");
- exit(EXIT_FAILURE);
- }
- char message[MAXLINE];
- if (snprintf(message, MAXLINE, "%s - %s", hello, datetime) >= MAXLINE) {
- fprintf(stderr, "Message truncated due to buffer size\n");
- exit(EXIT_FAILURE);
- }
- sendto(sockfd, (const char *)message, strlen(message),
- MSG_CONFIRM, (const struct sockaddr *)&servaddr,
- sizeof(servaddr));
- printf("Hello message sent.\n");
- n = recvfrom(sockfd, (char *)buffer, MAXLINE,
- MSG_WAITALL, (struct sockaddr *)&servaddr,
- &len);
- buffer[n] = '\0';
- printf("Server : %s\n", buffer);
- close(sockfd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement