All pastes #1735209 Raw Edit

client.c

public c v1 · immutable
#1735209 ·published 2010-01-02 17:16 UTC
rendered paste body
#include <stdlib.h>#include <stdio.h>#include <stdint.h>#include <string.h>#include <sys/socket.h>#include <sys/select.h>#include <sys/types.h>#include <netdb.h>#include <unistd.h>int main(){  int       sd, ret;  char      *line   = NULL;  size_t    buflen  = 0;  int       nread;  if (-1 != (sd = socket(AF_INET, SOCK_STREAM, 0)))  {    struct addrinfo*  addr  = NULL,                      hints = {};    hints.ai_family   = AF_INET;    hints.ai_socktype = SOCK_STREAM;    if (0 == (ret =getaddrinfo("localhost", "9090", &hints, &addr)))    {      if (0 == connect(sd, addr->ai_addr, addr->ai_addrlen))      {        while (0 < (nread = getline(&line, &buflen, stdin)))        {          nread--;          line[nread] = '\0';          if (0 == strcmp(line, "quit"))            break;          send(sd, line, nread, 0);        }      }      else        perror("connect: ");      freeaddrinfo(addr);    }    else      printf("gai: %s\n", gai_strerror(ret));    close(sd);  }  else    perror("socket: ");  return 0;}