#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define	PORT	8823

int main()
{
  int	fd, rc, len;
  struct sockaddr_in	inet_addr;
  struct hostent	*host;
  char	buf[256];

  /* ソケットを作る	*/
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if(fd < 0){
    printf("client:socket error.\n");
    exit(rc);
  }
  printf("client:socket.\n");

  /* SERVERにconnect	*/
  memset(&inet_addr, 0, sizeof(inet_addr));
  inet_addr.sin_family = AF_INET;
  host = gethostbyname("localhost");
  memcpy(&inet_addr.sin_addr, host->h_addr, sizeof(inet_addr.sin_addr));
  inet_addr.sin_port = htons(PORT);
  len = sizeof(inet_addr);
  rc = connect(fd, (struct sockaddr*)&inet_addr, len);
  if(rc < 0){
    printf("client:connect error.\n");
    exit(rc);
  }
  printf("client:connect.\n");

  /* connectしたソケットに送信	*/
  strcpy(buf, "from client");
  rc = send(fd, buf, strlen(buf)+1, 0);
  if(rc < 0){
    printf("client:send error.\n");
    exit(rc);
  }
  printf("client:send.\n");

  /* 後始末	*/
  shutdown(fd, 2);
  close(fd);

  return 0;
}
