// Yuri Zarechny for News2News (c) 2006
#include "stdafx.h"
#include < stdlib.h >
#include < winsock2.h > // dependencies: ws2_32.lib
#include "conio.h"
const int iPort=1234;
void Fatal(LPSTR sError)
{
printf("Fatal error: %s",sError);
exit(EXIT_FAILURE);
}
SOCKET ListenSocket, DataSocket;
WSAData WSData;
sockaddr_in sAddr;
int addrsize, datasize, i;
char databuf[1024];
int _tmain(int argc, _TCHAR* argv[])
{
if (WSAStartup(MAKEWORD(2,2), &WSData)>0)
{
sprintf(databuf,"Sockets interface not available. "
"Socket error: #%d\n", WSAGetLastError);
Fatal(databuf);
}
printf(databuf,"Interface found: %s",WSData.szDescription);
ListenSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (ListenSocket==INVALID_SOCKET)
{
sprintf(databuf,"Can not open listening socket. "
"Socket error: #%d\n", WSAGetLastError);
Fatal(databuf);
}
printf("Waiting for incoming connection...\n");
ZeroMemory(&sAddr,sizeof(sAddr));
sAddr.sin_family=AF_INET;
sAddr.sin_port=htons(iPort);
if (bind(ListenSocket,(SOCKADDR *) &sAddr,
sizeof(sAddr))==SOCKET_ERROR)
{
sprintf(databuf,"Can not bind() listening socket. "
"Socket error: #%d\n", WSAGetLastError);
closesocket(ListenSocket);
Fatal(databuf);
}
else if (listen(ListenSocket,1)==SOCKET_ERROR)
{
sprintf(databuf,"Can not listen() on socket. Socket error: #%d\n",
WSAGetLastError);
closesocket(ListenSocket);
Fatal(databuf);
}
else
{
ZeroMemory(&sAddr, sizeof(sAddr));
addrsize=sizeof(sAddr);
DataSocket=accept(ListenSocket,(SOCKADDR *) &sAddr, &addrsize);
if (DataSocket==INVALID_SOCKET)
{
sprintf(databuf,"Can nott accept() connection. "
"Socket error: #%d\n", WSAGetLastError);
closesocket(ListenSocket);
Fatal(databuf);
}
else
{
getpeername(DataSocket,(SOCKADDR *) &sAddr,&addrsize);
strcpy(databuf,inet_ntoa(sAddr.sin_addr));
i=ntohs(sAddr.sin_port);
printf("Connection established. (IP %s; Port %d)\n",databuf,i);
do {
datasize=recv(DataSocket,(char *) &databuf,
sizeof(databuf),0);
databuf[datasize]=0;
if (datasize>1 || (datasize>0 && databuf[0]!=(char)"*"))
printf("= %s\n",databuf);
} while (datasize>0 && databuf[0]!=(char)"*");
closesocket(DataSocket);
printf("Connection dropped.");
}
}
closesocket(ListenSocket);
printf("\nPress Enter to close this window.\n");
getch();
return 0;
}
|
In Visual FoxPro 8 declare bind function with an alias, because it may mix with BINDEVENT.
Some local ports are already used by the system and other applications. So if you assign your Winsock server to such port, the server will not start.
The Winsock error code returned is 10048 -- WSAEADDRINUSE Only one usage of each socket address (protocol/network address/port) is normally permitted.
List of local ports already used can be obtained through the system NETSTAT command, or use example in this reference Displaying all TCP connections for the local system to get the list.
Also take a look at this article Working with the NETSTAT Command.
* * *
You may even connect to the non-blocking Winsock server through the Internet. Make sure you connect to the public ip address of the server, not the one it has inside the local network. As well the port on the server has to be configured to accept external connections.
To test a connection to the non-blocking server through the Internet, first I stopped the IIS running on a computer on our LAN (after working hours, of course :). So I could use for the testing ports 80 (HTTP) and 443 (SSL) on that computer, which for sure were properly configured for accepting external connections. Then I launched a VNC connection to start the client part on a remote computer. I was able to connect to the server on each of two ports.
* * *
Feb.28, 2005: Note that once the server connects to a remote client and processes a request, all other clients are not able to connect during that short time because nobody is listening.
A robust Winsock server actually should create a thread for each new connection or use a pool of, say, 10 threads to serve connections. This kind of functionality can not be programmed using just FoxPro code.
#kwd: sln_winsock. |