aboutsummaryrefslogtreecommitdiff
path: root/q6/c/client.c
blob: 12e00fac1c174b5b5d321f196d02d5bf67a014ce (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>

int main(int argc, char *argv[]) {
    int sockfd;
    struct sockaddr_in servaddr;
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);

    servaddr.sin_family = AF_INET; 
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 

    int port = atoi(argv[1]);
    servaddr.sin_port = htons(port);

    int n; 

    socklen_t len = sizeof(servaddr);
    char *buf = malloc(100);
    fgets(buf, 100, stdin);
    while(strcmp(buf, "quit\n") != 0) {
        sendto(sockfd, buf, strlen(buf), 
                MSG_CONFIRM, (const struct sockaddr *) &servaddr,  
                sizeof(servaddr)); 

        n = recvfrom(sockfd, buf, 100,  
                MSG_WAITALL, (struct sockaddr *) &servaddr, 
                &len); 
        printf("Recieved: %s", buf);
        fgets(buf, 100, stdin);
    }

    close(sockfd); 
    return 0;
}