You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.1 KiB
C
47 lines
1.1 KiB
C
5 years ago
|
//source: https://www.geeksforgeeks.org/udp-server-client-implementation-c/
|
||
|
|
||
|
#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>
|
||
|
|
||
|
#define PORT 30000
|
||
|
#define MAXLINE 1024
|
||
|
|
||
|
// Driver code
|
||
|
int main() {
|
||
|
int sockfd;
|
||
|
char buffer[MAXLINE];
|
||
|
char *hello = "Hello from client";
|
||
|
struct sockaddr_in servaddr;
|
||
|
|
||
|
// Creating socket file descriptor
|
||
|
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
|
||
|
perror("socket creation failed");
|
||
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
|
|
||
|
memset(&servaddr, 0, sizeof(servaddr));
|
||
|
|
||
|
// Filling server information
|
||
|
servaddr.sin_family = AF_INET;
|
||
|
servaddr.sin_port = htons(PORT);
|
||
|
servaddr.sin_addr.s_addr = inet_addr("192.168.1.7");
|
||
|
for(;;)
|
||
|
{
|
||
|
int n = 0;
|
||
|
int len = sizeof(servaddr);
|
||
|
printf("Sending hello message.\n");
|
||
|
sendto(sockfd, (const char *)hello, strlen(hello),
|
||
|
MSG_CONFIRM, (const struct sockaddr *) &servaddr,
|
||
|
sizeof(servaddr));
|
||
|
sleep(1);
|
||
|
}
|
||
|
close(sockfd);
|
||
|
return 0;
|
||
|
}
|