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

C / C++ / MFC

 
AnswerRe: File Handle(very urgent) Pin
khan++14-Oct-05 21:17
khan++14-Oct-05 21:17 
GeneralRe: File Handle(very urgent) Pin
swaapu14-Oct-05 23:16
swaapu14-Oct-05 23:16 
GeneralRe: File Handle(very urgent) Pin
khan++14-Oct-05 23:38
khan++14-Oct-05 23:38 
GeneralRe: File Handle(very urgent) Pin
swaapu15-Oct-05 1:31
swaapu15-Oct-05 1:31 
QuestionADO datetime parameter - datestring or numbers to VARIANT Pin
compoundeye14-Oct-05 20:54
compoundeye14-Oct-05 20:54 
AnswerRe: ADO datetime parameter - datestring or numbers to VARIANT Pin
Mircea Puiu14-Oct-05 21:32
Mircea Puiu14-Oct-05 21:32 
GeneralRe: ADO datetime parameter - datestring or numbers to VARIANT Pin
compoundeye14-Oct-05 21:46
compoundeye14-Oct-05 21:46 
QuestionWhy unable to receive all message from connected clients using IOCP Pin
14-Oct-05 20:08
suss14-Oct-05 20:08 
Sleepy | :zzz: 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

-- modified at 3:08 Saturday 15th October, 2005
QuestionPlaying an mp3 file from memory Pin
V.G14-Oct-05 20:05
V.G14-Oct-05 20:05 
QuestionHow to find data encoding of received buffer with WSARecv() Pin
Member 168985514-Oct-05 19:49
Member 168985514-Oct-05 19:49 
AnswerRe: How to find data encoding of received buffer with WSARecv() Pin
kakan16-Oct-05 19:42
professionalkakan16-Oct-05 19:42 
GeneralRe: How to find data encoding of received buffer with WSARecv() Pin
Member 168985516-Oct-05 20:47
Member 168985516-Oct-05 20:47 
QuestionVC++6 Dll For use with VB6 Pin
tinman033014-Oct-05 18:45
tinman033014-Oct-05 18:45 
AnswerRe: VC++6 Dll For use with VB6 Pin
John R. Shaw14-Oct-05 22:01
John R. Shaw14-Oct-05 22:01 
GeneralRe: VC++6 Dll For use with VB6 Pin
tinman033015-Oct-05 12:48
tinman033015-Oct-05 12:48 
Questionwhy can't I see my class in the ClassView pane? Pin
ewighell14-Oct-05 18:05
ewighell14-Oct-05 18:05 
AnswerRe: why can't I see my class in the ClassView pane? Pin
Fired Fish14-Oct-05 20:12
Fired Fish14-Oct-05 20:12 
AnswerRe: why can't I see my class in the ClassView pane? Pin
khan++14-Oct-05 21:39
khan++14-Oct-05 21:39 
GeneralRe: why can't I see my class in the ClassView pane? Pin
ewighell15-Oct-05 6:14
ewighell15-Oct-05 6:14 
GeneralRe: why can't I see my class in the ClassView pane? Pin
fuzz_ball18-Oct-05 15:46
fuzz_ball18-Oct-05 15:46 
QuestionRichEdit Control not Drawing Properly Pin
Steve Thresher14-Oct-05 12:22
Steve Thresher14-Oct-05 12:22 
Questiontoolbar Pin
Archer28214-Oct-05 12:11
Archer28214-Oct-05 12:11 
AnswerRe: toolbar Pin
khan++14-Oct-05 21:48
khan++14-Oct-05 21:48 
QuestionListing Thread Module Names and Base Addresses Pin
Abhishek Karnik14-Oct-05 9:54
Abhishek Karnik14-Oct-05 9:54 
AnswerRe: Listing Thread Module Names and Base Addresses Pin
Arman S.14-Oct-05 10:13
Arman S.14-Oct-05 10:13 

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.