Click here to Skip to main content
15,896,111 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionLine Profiling Pin
ashwath200516-Oct-05 20:31
ashwath200516-Oct-05 20:31 
QuestionInstallation for VC++ project Pin
Anonymous16-Oct-05 20:28
Anonymous16-Oct-05 20:28 
Questionhelp! why the compiler kept modifying my source file? Pin
ewighell16-Oct-05 19:29
ewighell16-Oct-05 19:29 
AnswerRe: help! why the compiler kept modifying my source file? Pin
Rage16-Oct-05 23:39
professionalRage16-Oct-05 23:39 
GeneralRe: help! why the compiler kept modifying my source file? Pin
ewighell17-Oct-05 1:00
ewighell17-Oct-05 1:00 
QuestionHow to find data encoding of received buffer with WSARecv Pin
Member 168985516-Oct-05 19:00
Member 168985516-Oct-05 19:00 
AnswerRe: How to find data encoding of received buffer with WSARecv Pin
ThatsAlok16-Oct-05 21:43
ThatsAlok16-Oct-05 21:43 
Questionunable to get all Messages from Clients using IOCP Pin
Member 168985516-Oct-05 18:58
Member 168985516-Oct-05 18:58 
Let me confess that I am fresher in network programming can any one help me???
I made very simple server using IOCP but my server is unable to receive all the message. Here is code for my server


#include "stdafx.h"

//#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include "winsock2.h"
#include "fstream"
#include "conio.h"
#include "stdio.h"


#pragma comment(lib,"ws2_32.lib")

/*--- MACRO Defination --*/
#define SERVER_PORT htons(8888)
#define DATA_BUFSIZE 4096
#define OP_READ 0
#define OP_WRITE 1
#define OP_ACCEPT 2

/* OverlapPlus Data Used to store Ovelap info and other information need to user*/
typedef struct _OVERLAPPEDPLUS
{
OVERLAPPED Overlapped;
SOCKET sServer,sClient;
int nOperationCode;
WSABUF wbuf;
DWORD dwBytes,dwFlags;
SOCKADDR_STORAGE ClientAddr;
int nClientNumber;
}OVERLAPPEDPLUS, * LP_OVERLAP_PLUS;

void Initialize();
DWORD WINAPI ServerWorkerThread(LPVOID lpParam);
FILE *stream;
void appendLog(TCHAR* strMessage)
{
stream = _wfopen( _T("c:\\iServer.log"), _T("a") );
fputws(_T("\n"),stream);
fputws(strMessage,stream);
fclose( stream );

}
int main()
{

WSADATA wsd;
SYSTEM_INFO SystemInfo;
SOCKADDR_IN InternetAddr;
SOCKET sListen;
HANDLE hIocp;
int nClientInc = 1;
/* Initialize Winsock2 */
Initialize();
/*--- Create an I/O completion port ---*/
hIocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (ULONG_PTR)0, 0);
if(NULL == hIocp)
{
printf("Error in initialization of Completion Port! Press any to exit...");

return -1;
}
/* Determine how many processors are on the system, For thead optimization */
GetSystemInfo(&SystemInfo);
/*--- Create worker threads based on the number of processors available on the system ---*/
for(int nProcessor = 0; nProcessor < (int) SystemInfo.dwNumberOfProcessors; nProcessor++)
{
HANDLE ThreadHandle;
ThreadHandle = CreateThread(NULL, 0,ServerWorkerThread, hIocp,0, NULL);
//CloseHandle(ThreadHandle); // Why We close thread Handle ?????
}
/*--- Create a listening socket --- */
sListen = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0,WSA_FLAG_OVERLAPPED);
/*--- Fill TCP Header Info for bind a socket ---*/
InternetAddr.sin_family = AF_INET;
InternetAddr.sin_addr.s_addr = htonl(INADDR_ANY);
InternetAddr.sin_port = SERVER_PORT;
/*-- Bind the socket with port --*/
bind(sListen, (PSOCKADDR) &InternetAddr, sizeof(InternetAddr));
/*--- Prepare socket for listening mode and Start the Server ---*/
listen(sListen, 5);
printf("Server Started in listen mode ....");
while(TRUE)
{
int ret;
char strBuffer[DATA_BUFSIZE];
ULONG_PTR *PerHandleKey;
OVERLAPPED *Overlap;
OVERLAPPEDPLUS *OverlapPlus,*olpClientOverlappedPlusData;
DWORD dwBytesXfered;
SOCKET sClient;
SOCKADDR_IN saRemote;
int nRemoteLen;
/*-- Accept connections and assign to the completion port--*/
nRemoteLen = sizeof(saRemote);
sClient = WSAAccept(sListen, (SOCKADDR *)&saRemote,&nRemoteLen,0 , 0);
printf("New Client! Socket number %d connected\n", sClient);
/* Allocate memory to olpClientOverlappedPlusData */
olpClientOverlappedPlusData = (LP_OVERLAP_PLUS)GlobalAlloc(GPTR, sizeof(OVERLAPPEDPLUS));
if(NULL == olpClientOverlappedPlusData)
{
printf("Error in initialization Memory! Press any to exit...");

return -1;
}
/*-- Fill OverlapPlusData -- */
olpClientOverlappedPlusData->sClient = sClient;
olpClientOverlappedPlusData->nClientNumber = nClientInc++;
olpClientOverlappedPlusData->nOperationCode = OP_READ;
olpClientOverlappedPlusData->wbuf.buf = strBuffer;
olpClientOverlappedPlusData->wbuf.len = DATA_BUFSIZE;
memcpy(&olpClientOverlappedPlusData->ClientAddr, &saRemote, nRemoteLen);
// Associate the accepted socket with the completion port
if(NULL == CreateIoCompletionPort((HANDLE)sClient, hIocp,(ULONG_PTR)0,0) )
{
printf("Error in initialization of Completion Port for server! Press any to exit...");

return -1;
}
/*-- Make Ready to Recive Data From given Port -- */
ret = WSARecv(sClient,&olpClientOverlappedPlusData->wbuf,1,&olpClientOverlappedPlusData->dwBytes,&olpClientOverlappedPlusData->dwFlags,&olpClientOverlappedPlusData->Overlapped, NULL);
}

return 0;
}

DWORD WINAPI ServerWorkerThread(LPVOID CompletionPortID)
{
/*-- Notify Starting of server -- */
printf("\nHello Server Started, I am in worker thread! \n");
HANDLE hIocp = (HANDLE) CompletionPortID;
ULONG_PTR *PerHandleKey;
OVERLAPPED *Overlap;
OVERLAPPEDPLUS *OverlapPlus,*newolp;
DWORD dwBytesXfered;
int ret;
char *strData ;
while (1)
{
ret = GetQueuedCompletionStatus(hIocp,&dwBytesXfered,(PULONG_PTR)&PerHandleKey,&Overlap,INFINITE);
if (ret == 0){
continue;
}
OverlapPlus = CONTAINING_RECORD(Overlap, OVERLAPPEDPLUS, Overlapped);
/*--- Process Data According to Operation Code ---*/

switch (OverlapPlus->nOperationCode )
{
case OP_READ:
// Process the data read Repost the read if necessary, reusing the same receive buffer as before
memset(&OverlapPlus->Overlapped, 0, sizeof(OVERLAPPED));
ret = WSARecv(OverlapPlus->sClient,&OverlapPlus->wbuf,1,&OverlapPlus->dwBytes,&OverlapPlus->dwFlags,&OverlapPlus->Overlapped, NULL);
appendLog((TCHAR*) &OverlapPlus->wbuf.buf[0]);
ZeroMemory(&(OverlapPlus->Overlapped),sizeof(OVERLAPPED));// Why we reset memory ???

printf("\n\n****Inforamtion*****\n\nClient Socket:%d \n Bytes:%d \n Buff :%s \n Client No: %d\n FLAG :%d ",(int)OverlapPlus->sClient,OverlapPlus->dwBytes,OverlapPlus->wbuf.buf,OverlapPlus->nClientNumber,OverlapPlus->dwFlags);
if (ret == SOCKET_ERROR)
{
ret = WSAGetLastError();
if ( ret != WSA_IO_PENDING)
{
// What should i do in case of Error???

//printf("Error occur at WSARecv() : %d", ret);
//ExitThread(0);
break;
}
}
break;
case OP_WRITE:
// Process the data sent, etc.
break;//…
}
}
}
void Initialize()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
printf("\n couldn't find a useable winsock.dll");
exit(1);
}
}


Madhu S. Kapoor


Madhu S. Kapoor
QuestionC++ Serial Loopback port converting to char, where can I correct this Pin
ianfinlay_tyco16-Oct-05 18:19
ianfinlay_tyco16-Oct-05 18:19 
AnswerRe: C++ Serial Loopback port converting to char, where can I correct this Pin
stewartl17-Oct-05 0:11
stewartl17-Oct-05 0:11 
GeneralRe: C++ Serial Loopback port converting to char, where can I correct this Pin
ianfinlay_tyco18-Oct-05 12:53
ianfinlay_tyco18-Oct-05 12:53 
QuestionI have an error Pin
vhazell16-Oct-05 16:33
vhazell16-Oct-05 16:33 
AnswerRe: I have an error Pin
GflPower16-Oct-05 17:07
GflPower16-Oct-05 17:07 
GeneralRe: I have an error Pin
vhazell16-Oct-05 17:28
vhazell16-Oct-05 17:28 
AnswerRe: I have an error Pin
P-Rex16-Oct-05 21:32
P-Rex16-Oct-05 21:32 
GeneralRe: I have an error Pin
vhazell16-Oct-05 21:44
vhazell16-Oct-05 21:44 
GeneralRe: I have an error Pin
P-Rex16-Oct-05 22:07
P-Rex16-Oct-05 22:07 
GeneralRe: I have an error Pin
David Crow17-Oct-05 2:47
David Crow17-Oct-05 2:47 
Questionretrieve MSN messenger conversations Pin
smargada16-Oct-05 14:33
smargada16-Oct-05 14:33 
AnswerRe: retrieve MSN messenger conversations Pin
ThatsAlok16-Oct-05 19:05
ThatsAlok16-Oct-05 19:05 
QuestionRe: retrieve MSN messenger conversations Pin
David Crow17-Oct-05 8:02
David Crow17-Oct-05 8:02 
AnswerRe: retrieve MSN messenger conversations Pin
ThatsAlok17-Oct-05 18:02
ThatsAlok17-Oct-05 18:02 
Questionservices in xp Pin
Archer28216-Oct-05 13:25
Archer28216-Oct-05 13:25 
AnswerRe: services in xp Pin
GflPower16-Oct-05 17:11
GflPower16-Oct-05 17:11 
GeneralRe: services in xp Pin
Archer28216-Oct-05 17:16
Archer28216-Oct-05 17:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.