Knowledge Base Nr: 00244 WinCESocket.cpp - http://www.swe-kaiser.de
Downloads:
MFC/WinCE: Sockethandling - Schreiben und Lesen HTTP-Request
////// beispiel
BOOL CHellosocketDlg::OnInitDialog()
{
// Allow only one instance of the application.
HWND hWnd = ::FindWindow (TEXT("hellosocket"), NULL);
if (hWnd)
{
::SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));
return -1;
}
...
GetDlgItem(IDC_HOST)->SetWindowText(TEXT("krserver"));
GetDlgItem(IDC_PORT)->SetWindowText(TEXT("80"));
GetDlgItem(IDC_REQ)->SetWindowText(TEXT("HEAD /kaiserreich/"));
...
}
#include "../../msvc60/cpp_classes/WinCESupport.h"
void CHellosocketDlg::OnButton1()
{
GetDlgItem(IDC_MSG)->SetWindowText(TEXT(""));
CString strHost, strReq, strPort, strRecv;
GetDlgItem(IDC_HOST)->GetWindowText(strHost);
GetDlgItem(IDC_REQ)->GetWindowText(strReq);
GetDlgItem(IDC_PORT)->GetWindowText(strPort);
int nPortNo = _wtoi(strPort);
HWND hWnd = 0;
if (IsDlgButtonChecked(IDC_DEBUG))
hWnd = GetSafeHwnd();
int nErr = SendAndReceiveSocket(hWnd
, strHost, strReq, nPortNo
, strRecv);
ASSERT(!nErr);
GetDlgItem(IDC_MSG)->SetWindowText(strRecv);
}
////// implementierung
#include <afxwin.h>
#include <winsock.h>
#include "WinCESupport.h"
int SendAndReceiveSocket(HWND hWnd, LPCTSTR lpwszHost, LPCTSTR lpwszReq, int nPortNo, CString& strRecv)
{
////
char szReq[200] = {0};
char szHost[100] = {0};
::WideCharToMultiByte(CP_ACP, 0
, lpwszHost, wcslen(lpwszHost)
, szHost, sizeof(szHost)
, NULL, NULL);
::WideCharToMultiByte(CP_ACP, 0
, lpwszReq, wcslen(lpwszReq)
, szReq, sizeof(szReq)
, NULL, NULL);
////
char szRequest[300] = {0};
int index = 0; // Integer index
int iReturn = 0; // Return value of recv function
char szClientA[4000]; // ASCII string
TCHAR szClientW[4000]; // Unicode string
TCHAR szError[100]; // Error message string
SOCKET ServerSock = INVALID_SOCKET; // Socket bound to the server
SOCKADDR_IN destination_sin; // Server socket address
PHOSTENT phostent = NULL; // Points to the HOSTENT structure
WSADATA WSAData; // Contains details of the
sprintf(szRequest,
"%s HTTP/1.1\n"
"Host: %s\n"
"Connection: close\n"
"\n",
szReq, szHost);
// Initialize Winsocket.
if (WSAStartup (MAKEWORD(1,1), &WSAData) != 0)
{
wsprintf (szError, TEXT("WSAStartup failed. Error: %d"),
WSAGetLastError ());
if (hWnd)
::MessageBox (hWnd, szError, TEXT("Error"), MB_OK);
return -1;
}
// Create a TCP/IP socket that is bound to the server.
if ((ServerSock = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
wsprintf (szError, TEXT("Allocating socket failed. Error: %d"),
WSAGetLastError ());
if (hWnd)
::MessageBox (hWnd, szError, TEXT("Error"), MB_OK);
return -2;
}
// Fill out the server socket's address information.
destination_sin.sin_family = AF_INET;
phostent = gethostbyname (szHost);
if (phostent == NULL)
{
wsprintf (szError, TEXT("Unable to get the host name. Error: %d"),
WSAGetLastError ());
if (hWnd)
::MessageBox (hWnd, szError, TEXT("Error"), MB_OK);
strRecv += szError;
closesocket (ServerSock);
return -3;
}
// Assign the socket IP address.
memcpy ((char FAR *)&(destination_sin.sin_addr),
phostent->h_addr,
phostent->h_length);
// Convert to network ordering.
destination_sin.sin_port = htons (nPortNo);
// Establish a connection to the server socket.
if (connect (ServerSock,
(PSOCKADDR) &destination_sin,
sizeof (destination_sin)) == SOCKET_ERROR)
{
wsprintf (szError,
TEXT("Connecting to the server failed. Error: %d"),
WSAGetLastError ());
if (hWnd)
::MessageBox (hWnd, szError, TEXT("Error"), MB_OK);
closesocket (ServerSock);
strRecv += szError;
return -4;
}
// Send request to the server.
if (send (ServerSock, szRequest, strlen (szRequest) + 1, 0)
== SOCKET_ERROR)
{
wsprintf (szError,
TEXT("Sending data to the server failed. Error: %d"),
WSAGetLastError ());
strRecv += szError;
if (hWnd)
::MessageBox (hWnd, szError, TEXT("Error"), MB_OK);
}
// Disable sending on ServerSock.
shutdown (ServerSock, 0x01);
strRecv = "";
for (;;)
{
// Receive data from the server socket.
memset(szClientA, sizeof(szClientA), 0);
iReturn = recv (ServerSock, szClientA, sizeof(szClientA), 0);
// Check if there is any data received. If there is, display it.
if (iReturn == SOCKET_ERROR)
{
wsprintf (szError, TEXT("No data is received, recv failed.")
TEXT(" Error: %d"), WSAGetLastError ());
if (hWnd)
::MessageBox (hWnd, szError, TEXT("Client"), MB_OK);
strRecv += szError;
break;
}
else if (iReturn == 0)
{
//Finished receiving data
break;
}
else
{
//todo: Convert the ASCII string to a Unicode string.
memset(szClientW, sizeof(szClientW), 0);
for (index = 0; index <= sizeof(szClientA); index++)
szClientW[index] = szClientA[index];
szClientW[iReturn] = 0;
strRecv += szClientW;
}
}
// Disable receiving on ServerSock.
shutdown (ServerSock, 0x00);
// Close the socket.
closesocket (ServerSock);
WSACleanup ();
return 0;
}