Skip to content

Instantly share code, notes, and snippets.

@JubbaSmail
Created January 4, 2015 12:42
Show Gist options
  • Save JubbaSmail/a9692a3a99a6aeb65eef to your computer and use it in GitHub Desktop.
Save JubbaSmail/a9692a3a99a6aeb65eef to your computer and use it in GitHub Desktop.
// WIN SOCK - TCP Server sample
#include <winsock2.h>
SOCKET init_tcp()
{
WSADATA wsaData;
//Check if Operating System support WS2_32.LIB
int retval = WSAStartup(0x202, // version number 2.2
&wsaData // struct contain DLL configuration information
);
if (retval != 0)
return INVALID_SOCKET;
printf("Server: WSAStartup() is OK.\n");
//=======================================================
// TCP socket
SOCKET server_socket = socket(AF_INET, // AF_INET = TCP/IP | PF_INET
SOCK_STREAM,// SOCK_STREAM = TCP | SOCK_DGRAM = UDP
0 // 0 when use AF_INET
);
if (server_socket == INVALID_SOCKET)
return INVALID_SOCKET;
return server_socket;
}
SOCKET open_tcp(SOCKET server_socket, unsigned short port)
{
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY; //char *ip_address = NULL; inet_addr(ip_address);
address.sin_port = htons(port); //Port MUST be in Network Byte Order
int retval = bind(server_socket, // Connection Type = TCP
(struct sockaddr*)&address, // IP & Port information
sizeof(address) // Size
);
if ( retval != 0)
return INVALID_SOCKET;
printf("Server: bind() is OK.\n");
//================================
retval = listen(server_socket, 5 //Max clients count
);
if ( retval == SOCKET_ERROR)
return INVALID_SOCKET;
//printf("Server: listen() is OK.\n");
printf("Server: I'm listening and waiting connection\non port %d, protocol TCP\n", port);
return server_socket; // (Connection Type + IP + Port) information
}
int recieve_tcp(SOCKET channel, char Buffer[], int length)
{
int retval = recv(channel, Buffer, length, 0);
if (retval == SOCKET_ERROR)
return SOCKET_ERROR;
return retval;// number of bytes recieved
}
int send_tcp(SOCKET channel, char Buffer[], int length)
{
int retval = send(channel, Buffer, length, 0);
if (retval == SOCKET_ERROR)
return SOCKET_ERROR;
return retval;// number of bytes recieved
}
int main()
{
SOCKET server_socket = init_tcp();
if (server_socket == INVALID_SOCKET)
{
printf("Server: init socket failed: error %d\n", WSAGetLastError());
return -1;
}
//=======================================
unsigned short port = 5656;
server_socket = open_tcp(server_socket, port);
if (server_socket == INVALID_SOCKET)
{
printf("Server: open socket failed: error %d\n", WSAGetLastError());
return -1;
}
//=======================================
while (TRUE)
{
struct sockaddr_in client_info;
int fromlen = sizeof(client_info);
SOCKET channel = accept(server_socket, (struct sockaddr*)&client_info, &fromlen);
if (channel != INVALID_SOCKET)
{
printf("Server: accept() is OK.\n");
printf("Server: accepted connection from %s, port %d\n",
inet_ntoa(client_info.sin_addr), htons(client_info.sin_port));
}
else
return -1;
char Buffer[128];
int retval = recieve_tcp(channel, Buffer, sizeof(Buffer));
if (retval == SOCKET_ERROR)
{
printf("Server: recieved from socket failed: error %d\n", WSAGetLastError());
closesocket(channel);
continue;
}
else if (retval == 0)
{
printf("Server: Client closed connection.\n");
closesocket(channel);
continue;
}
printf("Server: Received %d bytes, data \"%s\" from client\n", retval, Buffer);
//====================================================
printf("Server: Echoing the REVERSED data back to client...\n");
/*char Buffer2[128];
int counter = 127;
for (int i = 0; i < 128; i++)
{
Buffer2[counter--] = Buffer[i];
}*/
retval = send_tcp(channel, Buffer, sizeof(Buffer));
if (retval == SOCKET_ERROR)
printf("Server: send() failed: error %d\n", WSAGetLastError());
printf("Server: send() is OK.\n");
//========================================================
closesocket(channel);
printf("Server: I'm waiting more connection, try running the client\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment