Skip to content

Instantly share code, notes, and snippets.

Created May 31, 2012 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2843361 to your computer and use it in GitHub Desktop.
Save anonymous/2843361 to your computer and use it in GitHub Desktop.
Client
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#define BUFFSIZE 32
void Die(char *mess) { perror(mess); exit(1); }
int main (int argc, char *argv[])
{
int sock;
struct sockaddr_in echoserver;
char buffer[BUFFSIZE];
unsigned int echolen;
int received = 0;
if (argc != 4)
{
fprintf(stderr, "USAGE: client <server_ip> <word> <port>\n");
exit(1);
}
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
Die("Failed to create socket");
}
printf("Sock is : %i\n", sock);
memset (&echoserver, 0, sizeof(echoserver));
echoserver.sin_family = AF_INET;
echoserver.sin_addr.s_addr = inet_addr(argv[1]);
echoserver.sin_port = htons(atoi(argv[3]));
if (connect(sock, (struct sockaddr *) &echoserver, sizeof(echoserver)) < 0)
{
Die("Failed to connect with server");
}
echolen = strlen(argv[2]);
if (send(sock, argv[2], echolen, 0) != echolen)
{
Die("Mismatch in number of sent bytes");
}
fprintf(stdout, "Received: ");
while (received < echolen)
{
int bytes = 0;
if ((bytes = recv(sock, buffer, BUFFSIZE-1, 0)) < 1)
{
Die("Failed to receive bytes from server");
}
received += bytes;
buffer[bytes] = '\0';
fprintf(stdout, buffer);
}
fprintf(stdout, "\n");
close(sock);
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment