multi-platform TCP, UDP socket server


Just for Test!
works on windows and linux

=================================================================================

#ifdef WIN32
#ifndef _WINSOCKAPI_
#define _WINSOCKAPI_
#endif
#endif

#include <stdio.h>

#ifdef WIN32
#pragma comment(lib, "ws2_32.lib") // Winsock 라이브러리 자동 링크

#include <windows.h>
#include <winsock2.h>

#define NET_INVALID_SOCKET INVALID_SOCKET
#define NET_SOCKET_ERROR SOCKET_ERROR
typedef int socklen_t; // unix 타입은 unsigned int, Windows는 int
#else
#include <sys/socket.h> // for socket(), bind(), connet()
#include <sys/types.h>
#include <arpa/inet.h> // for sockaddr_in, inet_ntoa()
#include <unistd.h> // for close()
#include <stdlib.h> // for exit()

typedef int SOCKET;
#define NET_INVALID_SOCKET -1
#define NET_SOCKET_ERROR -1
#endif

#define MAXCONN 10

int receiveUDP();
int receiveTCP();

int main(){

#ifdef WIN32

//HANDLE thread = CreateThread(NULL,0,StartListening, NULL, 0, NULL);

receiveUDP();

return 1;
}




/***************************************************
*
* UDP Multiplatform socket server Test
*
****************************************************/

int receiveUDP(){
#ifdef WIN32
WSADATA wsaData;
//WORD version;
#endif

//SOCKET ClientSocket;
SOCKET ServerSocket;

struct sockaddr_in ServerAddress;
struct sockaddr_in ClientAddress;
unsigned short ServerPort = 5060;
socklen_t clilen;// = sizeof(ClientAddress);
char buf[255];

printf("UDP Server start\n");

#ifdef WIN32

printf("It's WINDOWS\n");

if(WSAStartup(MAKEWORD(2,0), &wsaData) < 0)
{
printf("WSAStartup Init Error");
WSACleanup();
exit(0);
}
#endif

clilen = sizeof(ClientAddress);

ServerAddress.sin_family = AF_INET;
ServerAddress.sin_addr.s_addr = INADDR_ANY;
ServerAddress.sin_port = htons(ServerPort);

ServerSocket = socket(AF_INET, SOCK_DGRAM, 0);

if(ServerSocket == NET_INVALID_SOCKET)
{
printf("Fail to create the socket\n");
#ifdef WIN32
closesocket(ServerSocket);
WSACleanup();
#else
close(ServerSocket);
#endif
exit(0);

}

if(bind(ServerSocket, (struct sockaddr*)&ServerAddress, sizeof(ServerAddress)) == NET_SOCKET_ERROR){
printf("Fail to bind\n");
#ifdef WIN32
closesocket(ServerSocket);
WSACleanup();
#else
close(ServerSocket);
#endif
exit(0);
}

while(1){ /* main accept() loop*/
recvfrom(ServerSocket, (void *)&buf, sizeof(buf), 0, (struct sockaddr *)&ClientAddress, &clilen);
printf("recv %s\n", buf);

printf("Connect IP : %s, Port : %d\n",inet_ntoa(ClientAddress.sin_addr),htons(ClientAddress.sin_port));

sendto(ServerSocket, (void *)&buf, sizeof(buf),0,(struct sockaddr *)&ClientAddress, clilen);
printf("send %s\n",buf);
}
closesocket(ServerSocket);

#ifdef WIN32
closesocket(ServerSocket);
WSACleanup();
#else
close(ServerSocket);
#endif

printf("Close Server Session\n");
getchar();

return 1;
}


/***************************************************
*
* TCP Multiplatform socket server Test
*
****************************************************/

int receiveTCP(){

#ifdef WIN32
WSADATA wsaData;
WORD version;
#endif

SOCKET ClientSocket;
SOCKET ServerSocket;

struct sockaddr_in ServerAddress;
struct sockaddr_in ClientAddress;
unsigned short ServerPort = 5061;
socklen_t clilen;// = sizeof(ClientAddress);

printf("TCP Server start\n");

#ifdef WIN32
printf("It's WINDOWS\n");
version = MAKEWORD(2,2);
if(WSAStartup(version, &wsaData) < 0)
{
printf("WSAStartup Init Error");
WSACleanup();
exit(0);
}
#endif

ServerAddress.sin_family = AF_INET;
ServerAddress.sin_addr.s_addr = INADDR_ANY;
ServerAddress.sin_port = htons(ServerPort);

ServerSocket = socket(AF_INET, SOCK_STREAM, 0);

if(ServerSocket == NET_INVALID_SOCKET)
{
printf("Fail to create the socket\n");
#ifdef WIN32
closesocket(ServerSocket);
WSACleanup();
#else
close(ServerSocket);
#endif
exit(0);

}

if(bind(ServerSocket, (struct sockaddr*)&ServerAddress, sizeof(ServerAddress)) == NET_SOCKET_ERROR){
printf("Fail to bind\n");
#ifdef WIN32
closesocket(ServerSocket);
WSACleanup();
#else
close(ServerSocket);
#endif
exit(0);
}

if(listen(ServerSocket, MAXCONN) < 0){
printf("Fail to config listen function\n");
#ifdef WIN32
closesocket(ServerSocket);
WSACleanup();
#else
close(ServerSocket);
#endif
exit(0);
}

//socklen_t clilen = sizeof(ClientAddress);
clilen = sizeof(ClientAddress);
//int clilen = sizeof(ClientAddress);

while(1){ /* main accept() loop*/
if( (ClientSocket = accept(ServerSocket,(struct sockaddr*)&ClientAddress, &clilen)) < 0){
printf("Socket Error\n");
getchar();
}
printf("Connect IP : %s, Port : %d\n",inet_ntoa(ClientAddress.sin_addr),htons(ClientAddress.sin_port));

#ifdef WIN32
if(send(ClientSocket, "Hello, World\n", 14, 0) == -1){
printf("Fail to send a message\n");
getchar();
}
closesocket(ClientSocket);
#endif

#ifndef WIN32
if(!fork()){ /* this is the child process */
if(send(ClientSocket, "Hello, World\n", 14, 0) == -1){
printf("Fail to send a message\n");
getchar();
}
close(ClientSocket);
exit(0);
}
close(ClientSocket);

while(waitpid(-1, NULL, WNOHANG) > 0); /* clena up child processes */
#endif
}

#if 0
if( (ClientSocket = accept(ServerSocket,(struct sockaddr*)&ClientAddress, &clilen)) < 0){
printf("Socket Error\n");
getchar();
}
else{
printf("Connect IP : %s, Port : %d\n",inet_ntoa(ClientAddress.sin_addr),htons(ClientAddress.sin_port));
}
#endif

#ifdef WIN32
closesocket(ServerSocket);
WSACleanup();
#else
close(ServerSocket);
#endif

printf("Close Server Session\n");
getchar();

return 1;
}