-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudpc.c
48 lines (31 loc) · 874 Bytes
/
udpc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netinet/in.h>
#define PORT 5050
#define MAX 1024
void main(){
struct sockaddr_in servaddr, cliaddr;
int sockfd, connfd, length, leng;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd == -1){
perror("Error ");
exit(1);
}
printf("Socket created successfully\n");
memset(&servaddr, 0 , sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
printf("Enter message to send to server:\n");
char buffer[MAX];
scanf("%s", buffer);
sendto(sockfd, buffer, sizeof(buffer), MSG_CONFIRM, (struct sockaddr *)&servaddr, sizeof(servaddr));
length = strlen(buffer);
buffer[length] = '\0';
close(sockfd);
}