Using Win32 functions in Visual FoxPro Image Gallery
Code examples:
Printing Image File, programmatically set print page orientation to landscape
How to change display settings: screen resolution, screen refresh rate
Adding and deleting Scheduled Tasks using NetScheduleJob API functions
Accessing Adobe Reader 7.0 main menu from VFP application
Accessing examples contained in this reference through Web Services
Using Extended MessageBox() Class
Simple MAPI: how to pick an email recipient from Outlook Express address book
Accessing LSA Policy object (Local Security Authority)
Using Video Capture: displaying on FoxPro form frames and previewing video obtained from a digital camera
Enumerating network interfaces on the local computer
Sending a standard message with one or more attached files using default email client
Starting a dialog box for connecting to network resources and passing input parameters
Accessing examples contained in this reference from a VFP application
Custom HttpRequest class (WinHTTP)
Custom GDI+ class
Starting a dialog box for connecting to network resources (mapping network drive)
Accessing Windows Control Panel from VFP Application
Capturing keyboard activity of another application with the Raw Input API (VFP9)
Retrieving Printer Device Context using PrintDlg function
Extended MessageBox Class
A client for testing non-blocking Winsock server
Adding a background image to VFP report (VFP9, ReportListener)
How to convert a bitmap file to monochrome format (1 bpp)
Retrieving file information for the VFP executable running
How to create non-blocking Winsock server

User rating: 0/10 (0 votes)
Rate this code sample:
  • ~
More code examples    Listed functions    Add comment     W32 Constants      Translate this page Printer friendly version of this code sample
Versions:
click to open
Before you begin:


When you press Start button the program creates a socket binding to the selected port and places it in a state in which it is listening for an incoming connection.

Detecting a client, the server permits incoming connection, obtains the handle to a new socket, and receives incoming data if any.

After pressing the Stop button sometimes you have to wait 2-3 seconds for the server to stop. Several DOEVENT commands are inserted into server cycle.

The server processes requests from clients. That means you need a Winsock client program to communicate with the server.

Run the client part for this server on any other computer connected to your network, or even on the same computer, which runs the Winsock server. A client program initiates a network connection using IP address and listening port of the server.

See also:
  • A client for testing non-blocking Winsock server
  • How to build UDP responder
  • Using mailslots to send messages on the network
  • Using the NetMessageBufferSend to send messages on the network
  • Peer-to-peer LAN messenger built with Mailslot API functions
  • Using WM_COPYDATA for interprocess communication
  • // 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;
    }
    
    
    

    User rating: 0/10 (0 votes)
    Rate this code sample:
    • ~
    2499 bytes  
    Created: 2003-07-07 22:56:51  
    Modified: 2011-12-10 09:20:22  
    Visits in 7 days: 39  
    Listed functions:
    accept
    bind
    closesocket
    gethostbyname
    gethostname
    htons
    inet_addr
    inet_ntoa
    listen
    recv
    socket
    WSACleanup
    WSAGetLastError
    WSAStartup
    Printer friendly API declarations
    My comment:
    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.
    Word Index links for this example:
    Translate this page:
      Spanish    Portuguese    German    French    Italian  
    FreeTranslation.com offers instant, free translations of text or web pages.
    User Contributed Notes:
    ezequiel@professional-soft.com | 2006-08-01 06:53:56
    Say :
    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.

    But, will it be possible?

    Copyright 2001-2015 News2News, Inc. Before reproducing or distributing any data from this site please ask for an approval from its owner. Unless otherwise specified, this page is for your personal and non-commercial use. The information on this page is presented AS IS, meaning that you may use it at your own risk. Microsoft Visual FoxPro and Windows are trade marks of Microsoft Corp. All other trademarks are the property of their respective owners. 

    Privacy policy
    Credits: PHP (4.4.9), an HTML-embedded scripting language, MySQL (5.1.68), the Open Source standard SQL database, AceHTML Freeware Version 4, freeware HTML Editor of choice.   Hosted by Korax Online Inc.
    Last Topics Visited (54.166.33.25)
    24 sec.Example: 'How to make application automatically close all documents it opened'
    1.68 min.
    Function group: 'Kernel Transaction Manager'
    Google
    Advertise here!