/*************Client****************/
/*****Hello World (For Network)*****/
/*****Written by. Necky My.J********/
/***********************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
void ErrorHandling(char *message);
int main()
{
WSADATA wsaData;
SOCKET hSocket;
char message[30];
int strLen;
SOCKADDR_IN servAddr;
//Load WinSocket 2.2 DLL
if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
ErrorHandling("WSAStartup() error");
//Create Client Socket to connect server
hSocket=socket(PF_INET, SOCK_STREAM, 0);
if(hSocket == INVALID_SOCKET)
ErrorHandling("hSocket () error");
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servAddr.sin_port = htons(4000);
//Request to connect to server
if(connect(hSocket, (SOCKADDR*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR)
ErrorHandling("connect() error");
//Receive Data
strLen = recv(hSocket, message, sizeof(message)-1, 0);
if(strLen == -1)
ErrorHandling("read() error");
message[strLen] = 0;
printf("Message from server : %s \n", message);
//close connection
closesocket(hSocket);
WSACleanup();
return 0;
}
void ErrorHandling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}