-
Notifications
You must be signed in to change notification settings - Fork 0
/
conudps.c
executable file
·40 lines (28 loc) · 880 Bytes
/
conudps.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
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
int main(){
int udpSocket, nBytes;
char buffer[1024];
struct sockaddr_in serverAddr, clientAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size, client_addr_size;
int i;
udpSocket = socket(PF_INET, SOCK_DGRAM, 0);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
bind(udpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
addr_size = sizeof serverStorage;
while(1){
nBytes = recv(udpSocket,buffer,1024,0);
printf("Message received: %s", buffer);
printf("Enter message to send: ");
scanf("%s",buffer);
send(udpSocket,buffer,1024,0);
}
return 0;
}