/*****************************************/
/*****Hello World (For Network Server)*****/
/*********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 hServSock;
SOCKET hClntSock;
SOCKADDR_IN servAddr;
SOCKADDR_IN clntAddr;
int szClntAddr;
char message[] = "hello World";
if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
ErrorHandling("WSAStartup() error");
//Create Server Socket
hServSock=socket(PF_INET, SOCK_STREAM, 0);
if(hServSock == INVALID_SOCKET)
ErrorHandling("socket () error");
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(4000);
//Assign Address of Socket
if(bind(hServSock, (SOCKADDR*) &servAddr, sizeof(servAddr)) == SOCKET_ERROR)
ErrorHandling("bind() error");
//Ready to request connection
if(listen(hServSock, 5) == SOCKET_ERROR)
ErrorHandling("listen() Error");
szClntAddr = sizeof(clntAddr);
//Accept to request connection
hClntSock = accept(hServSock, (SOCKADDR*)&clntAddr, &szClntAddr);
if(hClntSock == INVALID_SOCKET)
ErrorHandling("accept() error");
//send data
send(hClntSock, message, sizeof(message), 0);
//close connection
closesocket(hClntSock);
WSACleanup();
return 0;
}
void ErrorHandling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}