Here is the server side of my sockets program.
Code:
// winsock for our network communication
#include <winsock2.h>
// stdio for I/O
#include <stdio.h>
#include <iostream.h>
#include <time.h>
// -----------------------------------------------------------------------------------
// startupServerForListening() - will return us a socket that is bound to the
// port we specify, and is listening for connections if all operations succeeded,
// and a -1 if startup failed.
int startupServerForListening(unsigned short port);
// -----------------------------------------------------------------------------------
// shutdownServer() - will take the socket we specify and shutdown the
// network utilities we started with startupServerForListening()
// Note: In order to function properly, the socket passed in MUST be the
// socket created by the startupServerForListening() function
void shutdownServer(int socket);
void main()
{
// the socket my server will use for listening
int serverSocket;
// startup our server utilities with my handy function
serverSocket = startupServerForListening(7654);
// check for errors
if (serverSocket == -1)
{
printf("Network Startup Failed!\nProgram Terminating\n");
return;
}
// accept a client
int clientSocket;
clientSocket = accept(serverSocket, 0, 0);
// check for errors
if (clientSocket == SOCKET_ERROR)
{
printf("Accept Failed!\n");
}
int nBytes;
// message size so we will know how much data we will send and receive
#define MESSAGE_SIZE 2
char inMessage[MESSAGE_SIZE];
// receive at MAX sizeof(inMessage) bytes into inMessage from the client using clientSocket
nBytes = recv(clientSocket, inMessage, sizeof(inMessage), 0);
// check for errors
if (nBytes == SOCKET_ERROR)
{
printf("Recv Failed!\n");
}
else
{
cout<<"Command received"<<endl;
cout<<"Command is: "<<inMessage<<endl;
}
//and shutting down our network.
if (inMessage[MESSAGE_SIZE] = 0)
{
// close our client socket
closesocket(clientSocket);
// and shutdown our network
shutdownServer(serverSocket);
printf("Press any key to continue ...\n");
getchar();
}
}
// -----------------------------------------------------------------------------------
// startupServerForListening() - a function to startup winsock, and open a socket for listening
int startupServerForListening(unsigned short port) {
// the winsock data structure
WSAData wsaData;
// startup winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) == SOCKET_ERROR) {
printf("Could Not Start Up Winsock!\n");
return -1;
}
// create socket
int mySocket = socket(AF_INET, SOCK_STREAM, 0);
// make sure nothing bad happened
if (mySocket == SOCKET_ERROR) {
printf("Error Opening Socket!\n");
return -1;
}
// the address structure
struct sockaddr_in server;
// fill the address structure with appropriate data
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = INADDR_ANY;
// and now bind my socket
if (bind(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {
printf("Bind Failed!\n");
closesocket(mySocket);
return -1;
}
// mark socket for listening
if (listen(mySocket, 5) == SOCKET_ERROR) {
printf("Listen Failed!\n");
closesocket(mySocket);
return -1;
}
printf("Server Started\n");
return mySocket;
}
// -----------------------------------------------------------------------------------
// shutdownServer() - a function to shutdown a socket and clean up winsock
void shutdownServer(int socket) {
// close our socket
closesocket(socket);
// shut down winsock
WSACleanup();
printf("Server Shutdown\n");
}