Client backup

 avatar
user_1944374
plain_text
a year ago
1.1 kB
0
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>


int main() {
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;

    //get the ip address
    char ip_address[16];
    printf("Enter IP Address: ");
    fgets(ip_address, sizeof(ip_address), stdin);
    server_addr.sin_addr.s_addr = inet_addr(ip_address);

    printf("IP Address is %s", ip_address);

    //get the port number
    int port;
    printf("Enter Port Number: ");
    scanf("%d", &port);
    server_addr.sin_port = port;

    int result = connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
    if(result == -1) {
        printf("Connect failed\n");
        exit(EXIT_FAILURE);
    }
    while(1) {
        char input[1024];
        printf("Enter Input: ");
        fgets(input, 1024, stdin);
        write(sock, input, sizeof(input));
        if(strcmp(input, "quit") == 0) break;
    }
    return 0;
}