client.c~ 864 Bytes
Newer Older
SHAILESH KUMAR's avatar
SHAILESH KUMAR committed
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 <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>

int main(int argc, char** argv)
{
	int s;
	int sock_fd = socket(AF_INET, SOCK_STREAM, 0);

	struct addrinfo hints, *result;
	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;

	s = getaddrinfo(NULL, "1234", &hints, &result);
	if (s != 0) {
	        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
        	exit(1);
	}

	connect(sock_fd, result->ai_addr, result->ai_addrlen);

	char buffer[100];
	while(fgets(buffer, 100, stdin) != NULL){
		printf("SENDING: %s", buffer);
		printf("===\n");
		write(sock_fd, buffer, strlen(buffer));


		char resp[1000];
		int len = read(sock_fd, resp, 999);
		resp[len] = '\0';
		printf("%s\n", resp);
	}
    	return 0;
}