// MinGW64を使った、TCP通信プログラム(サーバ)の基本形

// http://robot009.blogspot.jp/2014/01/mingw-socket-programming-in-c.html?showComment=1395901671168#c7037415389755666315 から転載させていただいています。

// gcc -g server.c -o server -lwsock32 -lws2_32

/***********************************************************
 ** Function : RDCS network system Server test
 ** File Name: server.c
 ** Library  :
 ** Author   :
 ** create   :
 ** modify   :
 ***********************************************************/
//----------------------------------------------------------
// Includes
//----------------------------------------------------------
#include <stdio.h>
#include <stdio.h>
#include <sys/types.h>
#include <winsock2.h>
#include <ws2tcpip.h>
//#include <sys/socket.h>
//#include <netinet/in.h>
#include <errno.h>

//----------------------------------------------------------
// Defines
//----------------------------------------------------------
#define _POSIX_SOURCE  1    /* POSIX comliant source (POSIX)*/
#define BUFFSIZE       256
#define FALSE          0
#define TRUE           1

//----------------------------------------------------------
// Globals
//----------------------------------------------------------

//----------------------------------------------------------
// Prototype
//----------------------------------------------------------
int main();

/***********************************************************
 ** Function :
 ** Category :
 ** Parameter:
 **          :
 ** ---------------------------------------------------------
 ** return   : normal end :0
 **          : error      : -1  error progess is upper routine.
 ** note     :
 **          :
 ************************************************************/
int main()
{
  WSADATA wsaData;
  SOCKET sock;
  SOCKET sock0;
  
  struct sockaddr_in addr;
  struct sockaddr_in client;
  
  //int sock0;
  //int sock;
  
  int len;
  int n;
  /*******************************************************
   ** Initialize
   ********************************************************/
  if (WSAStartup(MAKEWORD(2 ,0), &wsaData) == SOCKET_ERROR) {
    printf ("Error initialising WSA.\n");
    return -1;
  }
  
  /*******************************************************
   ** Process
   *******************************************************/
 //------------------------------------------------------
 // (1) Open Socket
 //------------------------------------------------------
  sock0 = socket(AF_INET, SOCK_STREAM, 0);
  if (sock0 < 0) {
    perror("socket");
    printf("%d\n", errno);
    return 1;
  }
  printf("after scoket\n");
  
  //------------------------------------------------------
  // (2) bind
  //------------------------------------------------------
  addr.sin_family = AF_INET;
  addr.sin_port = htons(50000);
  addr.sin_addr.s_addr = INADDR_ANY;
  
  if (bind(sock0, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
    perror("bind");
    return 1;
  }
  
  //------------------------------------------------------
  // (3) listen
  //------------------------------------------------------
  if (listen(sock0, 5) != 0) {
    perror("listen");
    return 1;
  }
  
  //------------------------------------------------------
  // (4) accept
  //------------------------------------------------------
  while (1) {
    len = sizeof(client);
    sock = accept(sock0, (struct sockaddr *)&client, &len);
    if (sock < 0) {
      perror("accept");
      break;
    }
    
    n = send(sock, "RDCS SERVER", 11, 0);
    
    if (n < 1) {
      perror("write");
      break;
    }
    
    //close(sock);
    closesocket(sock);
  }
  
  /*******************************************************
   ** Terminate
   *******************************************************/
  //close(sock0);
  closesocket(sock0);
  WSACleanup();
  
  return 0;
}