Click here to Skip to main content
15,915,164 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionhow to optimize IF condition Pin
xcavin18-Oct-06 21:04
xcavin18-Oct-06 21:04 
AnswerRe: how to optimize IF condition Pin
David Crow19-Oct-06 4:22
David Crow19-Oct-06 4:22 
QuestionRe: how to optimize IF condition Pin
Zac Howland19-Oct-06 5:04
Zac Howland19-Oct-06 5:04 
QuestionHelp Pin
Anamika200518-Oct-06 20:21
Anamika200518-Oct-06 20:21 
AnswerRe: Help Pin
Archyami18-Oct-06 20:34
Archyami18-Oct-06 20:34 
AnswerRe: Help Pin
Hamid_RT18-Oct-06 20:45
Hamid_RT18-Oct-06 20:45 
QuestionRe: Help Pin
David Crow19-Oct-06 4:24
David Crow19-Oct-06 4:24 
QuestionTCP receive 2 Pin
Archyami18-Oct-06 18:39
Archyami18-Oct-06 18:39 
this is an continuous message...

Below is the client code to receive 2 replies from a server:

/*********/

BOOL streamclient(char* ip, unsigned short port, char* message, AString* astr_reply)
{
//
// Create a TCP/IP stream socket
//
SOCKET theSocket;

theSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(theSocket==INVALID_SOCKET)
{
eprintf("ServerHandler:streamclient()\nsocket() %s %u", ip, port);
return FALSE;
}

//
// Fill in the address structure
//
SOCKADDR_IN saServer;
DWORD dwIPAddr=inet_addr(ip);

saServer.sin_family=AF_INET;
saServer.sin_addr.s_addr=*((u_long FAR*)((char*)&dwIPAddr));
saServer.sin_port=htons(port);

//
// connect to the server
//
int nRet;

nRet=connect(theSocket, // Socket
(LPSOCKADDR)&saServer, // Server address
sizeof(struct sockaddr));// Length of server address structure
if(nRet==SOCKET_ERROR)
{
closesocket(theSocket);
eprintf("ServerHandler:streamclient()\nconnect() %s %u", ip, port);
return FALSE;
}

//
// Send data to the server
//
nRet=send(theSocket, // Connected socket
message, // Data buffer
(int)strlen(message), // Length of data
0); // Flags
if(nRet==SOCKET_ERROR)
{
closesocket(theSocket);
eprintf("ServerHandler:streamclient()\nsend() %s %u", ip, port);
return FALSE;
}
aprintf("sent to CATOS (%s %u):\n%s", ip, port, message);

//
// Wait for a reply
//
char* szBuf;
AString astr;

/* receive ACK */
szBuf=(char*)malloc(sizeof(char)*(CATOS_BUFFER_SIZE+1));
if(szBuf==NULL)
{
eprintf("ServerHandler:streamclient()\nszBuf==NULL");
return FALSE;
}

nRet=recv(theSocket, // Connected socket
szBuf, // Receive buffer
CATOS_BUFFER_SIZE,//sizeof(szBuf), // Size of receive buffer
0); // Flags
if(nRet==SOCKET_ERROR)
{
closesocket(theSocket);
free(szBuf);
eprintf("ServerHandler:streamclient()\nrecv() %s %u", ip, port);
return FALSE;
}
if(astr.Set(szBuf))
aprintf("received from server:\n%s", astr.SubString(0, ETX, TRUE));
else
aprintf("received from server:\n[ACK]");

/* receive useful reply */
// clear szBuf and wait for a while so that recv() becomes ready:
for(int i=0; i<catos_buffer_size; i++)
="" szbuf[i]="\0" ;
="" sleep(second_receive_delay);
="" nret="recv(theSocket," connected="" socket
="" szbuf,="" receive="" buffer
="" catos_buffer_size,="" sizeof(szbuf),="" size="" of="" 0);="" flags
="" if(nret="=SOCKET_ERROR)
" {
="" closesocket(thesocket);
="" free(szbuf);
="" eprintf("serverhandler:streamclient()\nrecv()="" %s="" %u",="" ip,="" port);
="" aprintf("serverhandler:streamclient()\nrecv()="" port);return="" false;
="" }

="" if(astr_reply-="">Set(szBuf, nRet))
{
//
// Display the received data
//
free(szBuf);
closesocket(theSocket);
aprintf("received from server:\n%s", astr_reply->Value);
/* exception */
return TRUE;
}
aprintf("received from server:\n[useful reply]");
free(szBuf);
closesocket(theSocket);
return FALSE;
}

BOOL ServerHandler::Send(char* ip, unsigned short port, char* message)
{
WORD wVersionRequested;
WSADATA wsaData;
int nRet;
int i;

wVersionRequested=MAKEWORD(1,1);
nRet=WSAStartup(wVersionRequested, &wsaData);

if(wsaData.wVersion==wVersionRequested)
{
for(i=0; i
AnswerRe: TCP receive 2 Pin
mpk197918-Oct-06 20:34
mpk197918-Oct-06 20:34 
GeneralRe: TCP receive 2 Pin
Archyami18-Oct-06 20:43
Archyami18-Oct-06 20:43 
AnswerRe: TCP receive 2 Pin
Mark Salsbery19-Oct-06 5:51
Mark Salsbery19-Oct-06 5:51 
GeneralRe: TCP receive 2 Pin
Archyami19-Oct-06 15:35
Archyami19-Oct-06 15:35 
GeneralRe: TCP receive 2 [modified] Pin
Mark Salsbery19-Oct-06 16:26
Mark Salsbery19-Oct-06 16:26 
GeneralRe: TCP receive 2 Pin
Archyami20-Oct-06 6:00
Archyami20-Oct-06 6:00 
GeneralRe: TCP receive 2 Pin
Mark Salsbery20-Oct-06 10:08
Mark Salsbery20-Oct-06 10:08 
QuestionHow to add a worksheet in excel Pin
Abhiyantara18-Oct-06 17:32
Abhiyantara18-Oct-06 17:32 
AnswerRe: How to add a worksheet in excel Pin
Hamid_RT18-Oct-06 18:17
Hamid_RT18-Oct-06 18:17 
Questionproblem with atl new wizard Pin
With_problem18-Oct-06 17:16
With_problem18-Oct-06 17:16 
AnswerRe: problem with atl new wizard Pin
Hamid_RT18-Oct-06 18:21
Hamid_RT18-Oct-06 18:21 
QuestionRe: problem with atl new wizard Pin
David Crow19-Oct-06 4:27
David Crow19-Oct-06 4:27 
Questionclass which has no .cpp and .h Pin
gentleguy18-Oct-06 16:29
gentleguy18-Oct-06 16:29 
AnswerRe: class which has no .cpp and .h Pin
Waldermort18-Oct-06 18:57
Waldermort18-Oct-06 18:57 
AnswerRe: class which has no .cpp and .h Pin
Hamid_RT18-Oct-06 19:27
Hamid_RT18-Oct-06 19:27 
AnswerRe: class which has no .cpp and .h Pin
David Crow19-Oct-06 4:28
David Crow19-Oct-06 4:28 
GeneralRe: class which has no .cpp and .h Pin
David Crow19-Oct-06 4:52
David Crow19-Oct-06 4:52 

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.