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

int main(int argc, char *argv[]) { 
    if (argc < 2) {
        printf("Requires atleast 1 argument!\n");
        return -1;
    }
    const char *server_name = "127.0.0.1";
    int sockfd, connfd; 
    struct sockaddr_in servaddr, cli; 

    sockfd = socket(AF_INET, SOCK_STREAM, 0); 
    servaddr.sin_family = AF_INET; 
    inet_pton(AF_INET, server_name, &servaddr.sin_addr);

    int port = 0;
    printf("Enter port to connect to: ");
    scanf("%d", &port);
    fflush(stdout);
    fflush(stdin);
    servaddr.sin_port = htons(port);

    connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
    printf("Connected to server!\n");
    fflush(stdout);

    char  file_contents[100];

    send(sockfd, argv[1], strlen(argv[1]), 0);
    int cont;
    while((cont=(recv(sockfd, file_contents, 100, 0))) > 0) {
        printf("File contents: %s", file_contents);
    }
    close(sockfd); 
}