Monday, August 29, 2011

WinSock server..

#include "stdafx.h"
#include
#include

#include
#include


void ServerThread(void* pParam)
{
//A SOCKET is simply a typedef for an unsigned int.
//In Unix, socket handles were just about same as file
//handles which were again unsigned ints.
//Since this cannot be entirely true under Windows
//a new data type called SOCKET was defined.

FILE *pFile;
struct stat fileinfo;

SOCKET server;

//WSADATA is a struct that is filled up by the call
//to WSAStartup
WSADATA wsaData;

//The sockaddr_in specifies the address of the socket
//for TCP/IP sockets. Other protocols use similar structures.
sockaddr_in local;

//WSAStartup initializes the program for calling WinSock.
//The first parameter specifies the highest version of the
//WinSock specification, the program is allowed to use.
int wsaret=WSAStartup(0x101,&wsaData);

//WSAStartup returns zero on success.
//If it fails we exit.
if(wsaret!=0)
{
// return 0;
}

//Now we populate the sockaddr_in structure
local.sin_family=AF_INET; //Address family
local.sin_addr.s_addr = inet_addr("127.0.0.1"); // 16.182.218.213"); //16.182.218.240
local.sin_port = htons(631);

//the socket function creates our SOCKET
server=socket(AF_INET,SOCK_STREAM,0);

//If the socket() function fails we exit
if(server==INVALID_SOCKET)
{
// return 0;
}

//bind links the socket we just created with the sockaddr_in
//structure. Basically it connects the socket with
//the local address and a specified port.
//If it returns non-zero quit, as this indicates error
if(bind(server,(sockaddr*)&local,sizeof(local))!=0)
{
// return 0;
}

//listen instructs the socket to listen for incoming
//connections from clients. The second arg is the backlog
if(listen(server,10)!=0)
{
// return 0;
}

//we will need variables to hold the client socket.
//thus we declare them here.
SOCKET client;
sockaddr_in from;
int fromlen=sizeof(from);

while(true)//we are looping endlessly
{
char temp[4096];

client=accept(server,
(struct sockaddr*)&from,&fromlen);

int bytes = 0;
int h = 0;
do
{
bytes = 0;
memset(&temp,0,4096);
bytes = recv(client,temp,4096,0);
if (bytes<4096) break;


}while ( bytes > 0 );

char* szFileBuf= NULL;
bytes=0;
const char filename[] = "c:\\Result_PrintJob.txt";
FILE *file;
struct stat fileinfo;
if (filename != NULL)
{
if (stat(filename, &fileinfo))
{
return ;
}
if ((file = fopen(filename, "rb")) == NULL)
{
return ;
}

rewind(file);
szFileBuf = new char[(size_t)fileinfo.st_size];
bytes = fread(szFileBuf, 1, (size_t)fileinfo.st_size, file);

if (file)
fclose(file);
h = send(client,szFileBuf,bytes,0);

break;
}


// cout << "Connection from " << inet_ntoa(from.sin_addr) <<"\r\n";

//close the client socket
closesocket(client);

}

//closesocket() closes the socket and releases the socket descriptor
closesocket(server);

//originally this function probably had some use
//currently this is just for backward compatibility
//but it is safer to call it as I still believe some
//implementations use this to terminate use of WS2_32.DLL
WSACleanup();

// return 0;
}



int _tmain(int argc, _TCHAR* argv[])
{
int nRetCode = 0;

_beginthread( ServerThread, 0, NULL );

while(_getch()!=27);

return nRetCode;

}

No comments: