// MinGW64を使った、TCP通信プログラム(クライアント)の基本形
// http://robot009.blogspot.jp/2014/01/mingw-socket-programming-in-c.html?showComment=1395901671168#c7037415389755666315 から転載させて頂いています
/***********************************************************
** Function : RDCS network system Client test
** File Name: client1.c
** Library :
** Author :
** create :
** modify :
***********************************************************/
//----------------------------------------------------------
// Includes
//----------------------------------------------------------
#include <stdio.h>
#include <sys/types.h>
#include <winsock2.h>
#include <ws2tcpip.h>
//#include <sys/socket.h>
//#include <netinet/in.h>
//#include <arpa/inet.h>
//#include <netdb.h>
//----------------------------------------------------------
// Defines
//----------------------------------------------------------
#define _POSIX_SOURCE 1 /* POSIX comliant source (POSIX)*/
#define BUFFSIZE 256
#define FALSE 0
#define TRUE 1
//----------------------------------------------------------
// Globals
//----------------------------------------------------------
//----------------------------------------------------------
// Prototype
//----------------------------------------------------------
int main(int argc, char *argv[]);
//----------------------------------------------------------
// Code
//----------------------------------------------------------
/***********************************************************
** Function :
** Category :
** Parameter:
** :
** ---------------------------------------------------------
** return : normal end :0
** : error : -1 error progess is upper routine.
** note :
** :
************************************************************/
int main(int argc, char *argv[])
{
WSADATA wsaData;
SOCKET sock;
struct sockaddr_in server;
//int sock;
char buf[32];
char *deststr;
unsigned int **addrptr;
/*******************************************************
** Initialize
********************************************************/
if (argc != 2) {
printf("Usage : %s dest\n", argv[0]);
return 1;
}
deststr = argv[1];
if (WSAStartup(MAKEWORD(2,0), &wsaData) == SOCKET_ERROR) {
printf ("Error initialising WSA.\n");
return -1;
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("socket");
return 1;
}
/*******************************************************
** Process
*******************************************************/
//------------------------------------------------------
// (1)
//------------------------------------------------------
server.sin_family = AF_INET;
server.sin_port = htons(50000);
server.sin_addr.s_addr = inet_addr(deststr);
//------------------------------------------------------
// (2)connect
//------------------------------------------------------
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) != 0) {
perror("connect");
return 1;
}
//------------------------------------------------------
// (3)
//------------------------------------------------------
char vect[512]={0};
int get=recv(sock,vect,512,0);
printf("read=%s\n", vect);
/*******************************************************
** Terminate
*******************************************************/
//close(sock);
closesocket(sock);
WSACleanup();
return 0;
}