WSADATA wsaData;
memset(&wsaData, 0, sizeof(WSADATA));
if(WSAStartup(MAKEWORD(1,1), &wsaData) != 0){
return 1;
}
// Create a connection-oriented socket
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Check to see if we have a valid socket
if(s == INVALID_SOCKET) {
int iSocketError = WSAGetLastError();
return 2;
}
SOCKADDR_IN sListener;
memset(&sListener, 0, sizeof(SOCKADDR_IN));
// Setup the port to bind on
sListener.sin_family = AF_INET;
sListener.sin_port = htons(8080);
sListener.sin_addr.s_addr = htonl(INADDR_ANY);
// Bind to the socket
if(bind(s, (SOCKADDR *)&sListener, sizeof(sListener)) ==
SOCKET_ERROR) {
int iSocketError = WSAGetLastError();
return 3;
}
// Listen for incoming connections
if(listen(s, SOMAXCONN) == SOCKET_ERROR) {
int iSocketError = WSAGetLastError();
return 4;
}
// Wait for a connection
SOCKADDR_IN sIncomingAddr;
memset(&sIncomingAddr, 0, sizeof(SOCKADDR_IN));
int iAddrLen = sizeof(SOCKADDR_IN);
SOCKET sIncomingSocket = accept(s, (SOCKADDR *) &sIncomingAddr,
&iAddrLen);
if(sIncomingSocket == SOCKET_ERROR) {
int iSocketError = WSAGetLastError();
return 5;
}
// We have an incoming socket request
char cResponseBuffer[1024] = "";
int nBytesReceived = 0;
// Get a basic request. In reality, we would want to check
// the HTTP request to see if it's valid, but let's just
// send a simple response.
nBytesReceived = recv(sIncomingSocket, &cResponseBuffer[0], 1024, 0);
if(nBytesReceived == SOCKET_ERROR) {
int iSocketError = WSAGetLastError();
return iSocketError;
}
// Send out a response
char cBuffer[1024] = "";
int nBytesSent = 0;
int nBytesIndex = 0;
// Setup the buffer to send
sprintf(cBuffer, "HTTP/1.0 200 OK\r\n\r\nTest Response\r\n\r\n");
int nBytesLeft = strlen(cBuffer);
// Send the entire buffer
while(nBytesLeft > 0) {
nBytesSent = send(sIncomingSocket, &cBuffer[nBytesIndex],
nBytesLeft, 0);
if(nBytesSent == SOCKET_ERROR)
break;
// See how many bytes are left. If we still need to send, loop
nBytesLeft -= nBytesSent;
nBytesIndex += nBytesSent;
}
// Close the sockets
closesocket(sIncomingSocket);
closesocket(s);
WSACleanup();
return 0;
}
then I open Pocket Internet Explorer and I type "http://localhost:
8080". the program terminates with code 10054 ("The program
'[2da34576] moduleServer.exe' has exited with code 10054 (0x2746).").
The error happens at this point I suppose: nBytesReceived =
recv(sIncomingSocket, &cResponseBuffer[0], 10000, 0).I found that this
error code is defined in winsock2.h as #define WSAECONNRESET
(WSABASEERR+54) defined here[1] as "The virtual circuit was reset by
the remote side executing a hard or abortive close. The application
should close the socket because it is no longer usable." Is it maybe
related to the fact I haven't connection for the emulator? What's the
problem?
Thanks in advance,I'm very sorry to posts easy questions for you but
I'm really new to all this matter and I HAVE TO do a probject so thank
you for any help given to me :)
1. Get rid of Vista. It's nothing but a giant headache for developers.
2. Use the emulator manager to "cradle" the emulator and then configure WMDC
to allow pass-through
3. Get an actual device as soon as feasible.
> 2) How do you debug your application? For example printf calls can't
> be displayed in the Emulator nor in Visual Studio. how do you debug
> your apps?
DEBUGMSG calls go back to the Output window in Studio (at least for CE
devices they do - can't say I've tried with WM). Break points are also
useful.
> 3) Do you know any site,code examples or updated book that deal with
> socket programming on Pocket PC?
Sockets in CE are near identical to the desktop, so any desktop resource
would be valid.
> 4) I'm trying to learn by examples so I have this code:
I'm not up for reading through the code (someone else may be though).
Again, get something working on the desktop and then try the same code on
the device.
--
Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com
Thanks that was usefull.I'll wait for other advices.
"Christian" wrote:
> Hi everybody
> I have some questions about the way you experienced programmers debug
> your Windows CE based application. First of all I use Vista,Visual
> Studio 2005 and Windows Mobile Device Center (ActiveSync for Vista).
> 1) How can I share my network connection between my PC and the
> emulator integrated in Visual Studio? If I go in the Emulator to File
> > Configure > Network and I try to flag "enable NE2000 netword adapter
> and bind to",I receive an error that invites me to download Virtual PC
> 2007. After having installed this software, I receive the error :"No
> VPC network adapters enumerated or no host network adapter with
> provided MAC address found". So I can't surf the net. I've read all
> the guide but I haven't understood how can I reach my goal. What's the
> problem?
I have read that people have to enable Virtual Machine Network Services in
the Local Area Connection properties in Vista.
I did, but still does not function