Re: Need help for socket Programming
- From: "Saju Sathyan" <saju_sathyan@xxxxxxxxxxx>
- Date: Wed, 30 Jan 2008 19:46:37 +0100
The library to use is "Ws2.lib"
cheers
"Christian" wrote in message
Hi to everybody,
I'm using Visual Studio 2005 pro SP1 under Windows Vista. I'm reading
Pocket PC Network Programming. My goal is to write a server that
accept input command as Strings sent by a Java Application.
So I started with the example below that should be a server listening
on port 80:
#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#include <winsock.h>
int _tmain(int argc, _TCHAR* argv[])
{
// Initialize Winsock
WSADATA wsaData;
memset(&wsaData, 0, sizeof(WSADATA));
if(WSAStartup(MAKEWORD(1,1), &wsaData) != 0)
return FALSE;
// 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 FALSE;
}
SOCKADDR_IN sListener;
memset(&sListener, 0, sizeof(SOCKADDR_IN));
// Setup the port to bind on
sListener.sin_family = AF_INET;
sListener.sin_port = htons(80);
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 FALSE;
}
// Listen for incoming connections
if(listen(s, SOMAXCONN) == SOCKET_ERROR) {
int iSocketError = WSAGetLastError();
return FALSE;
}
// 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 FALSE;
}
// 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 FALSE;
}
// 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;
}
But there are linker 12 errors:
1>Linking...
1>moduleServer.obj : error LNK2019: unresolved external symbol
WSACleanup referenced in function wmain
1>moduleServer.obj : error LNK2019: unresolved external symbol
closesocket referenced in function wmain
The book said to link winsock.lib but there's no library like this in
the include path for smart device. I tried to use
#pragma comment(lib, "WS2_32.lib") by including such library in the
path for ARMV4 device but it can't resolve the problem.
I don't know what do to,please give me an advice. Thanks in advance.
.
- Follow-Ups:
- Re: Need help for socket Programming
- From: Christian
- Re: Need help for socket Programming
- References:
- Need help for socket Programming
- From: Christian
- Need help for socket Programming
- Prev by Date: Re: Storage Card name equivalent in Spanish device !!!
- Next by Date: Re: SetForegroundWindow crashes
- Previous by thread: Need help for socket Programming
- Next by thread: Re: Need help for socket Programming
- Index(es):
Relevant Pages
|