Avisaro Online Documentation - Wiki
Impressum Kontakt Sitemap

TCP/IP (WIN-SOCKET) programing

To communicate via TCP/IP or UDP/IP, the regular WIN-Socket programming can be used. There is nothing special to the Avisaro Module.

The following code shows a general example how to use TCP on a PC using C programming language. Other languages such as JAVA, C#, Visual-Basic are suitable as well

 

/*
    Simple client example using WINSOCK
    -----------------------------------
    Link with ws2_32.lib (MSVC) or libws2_32.a (GCC/MinGW).
    
    This program connects to a web server and displays all received bytes
    (including HTTP headers) in the console window.   

    You may also visit these sites:
    *   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/getting_started_with_winsock.asp
    *   http://www.hal-pc.org/~johnnie2/winsock.html
    *   http://tangentsoft.net/wskfaq/
    *   http://www.vijaymukhi.com/
    *   http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/winsock.html
    *   http://www.sockets.com/

    Have fun,
       -> peter@avisaro.com
*/

// This is the webserver address 
// www.yahoo.akadns.net (yahoo.com)
#define IPADDRESS "216.109.117.204"
// This is the port number (HTTP)
#define PORT 80

// For 'printf' etc.
#include <stdio.h>
// Also include socket definitions
#include <windows.h>

// Macro which prints an error message 
// and exits the program
#define ERR(_x_){printf("Could not %s\n",_x_);Sleep(INFINITE);exit(1);}

// This is the simplest HTTP GET request
#define HTTP_REQUEST "GET / HTTP/1.0\r\n\r\n"

// Let's go
int main()
{
    // Structure gets filled by WSAStartup
    WSADATA wsa; 
    // Our socket handle
    SOCKET sock;
    // Structure holding the complete network address
    SOCKADDR_IN sockaddr;
    // Temporary return value from socket functions
    int result;

    // Initialize the socket library
    if (0 != WSAStartup (0x0202, &wsa)) 
       ERR ("initialize socket library");    

    // Get a socket instance
    sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock == INVALID_SOCKET)
       ERR ("create socket instance");

    // Connect to the remote service
    sockaddr.sin_family = AF_INET;
    sockaddr.sin_port = htons (PORT);
    sockaddr.sin_addr.s_addr = inet_addr (IPADDRESS);
    result = connect (sock, (SOCKADDR*)&sockaddr, sizeof (SOCKADDR_IN));
    if (result == SOCKET_ERROR)
       ERR ("connect");
    printf ("Connected to %s\n", IPADDRESS);    
    
    // Now send an HTTP request
    result = send (sock, HTTP_REQUEST, sizeof(HTTP_REQUEST)-1, 0);    
    if (result == SOCKET_ERROR)
        ERR ("send HTTP request");
    
    // Display all received data
    while (1)
    {
        // Receive buffer
        char buff[256];
        // Temporary pointer for displaying received bytes
        char *temp;
        
        // Try to get some data
        // The return value is the number of received bytes.
        result = recv (sock, buff, 256, 0);
        // Leave the loop if either the connection was closed
        // or there was an error. The web server closes the 
        // connection when all data was send.
        if (result == 0 || result == SOCKET_ERROR)
            break;
        // Something has been received - show it now.
        temp = buff;
        while (result--)
            printf ("%c", *temp++);    
    }
    printf ("\nDisconnected\n");

    // Finally close the socket
    closesocket (sock); 

	// Ready
	return 0;
}