WideStudio Programming Guide

| WideStudio Programming Guide | WideStudio Index Table of contents

> ## Network communication using TCP/IP

> ### How to use network communication using TCP sockets

> TCP network communication is used in client-server oriented communications. Server side socket, WSCvssocket class can accept from client side socket, WSCvcsocket. Usually, in handling TCP sockets by C/C++ language, connection is established after accept, listen or connect procedures but in WideStudio, the processes related to establishing TCP socket connection are automatically done in an object library to conceal these processes and users do not need to write these processes. The TCP socket libraries have a property regarding IP address or PORT number, in which you can use just setting these values to transmit data using TCP socket libraries. TCP socket libraries contains connecting side(client) and connected side(server)

> Client side and server side is different on operation mainly in connecting. Comparing a client side connecting at a specific IP address and port number existing over the network, a server side wait for being connected. Thus, in using TCP connection, WSCvcsocket (Client side) should always connect to WSCvssocket (Server side) which is waiting for connection.
> For the client side property settings, the referral server TCP/IP address is set in WSNip and the port in WSNport. For the server side property settings, the awaiting socket port is set in WSNport and turn "ON" WSNrunning. Though usually WSNip is not set, it should be set when the awaiting IP address have to be specified.
> By invoking WSCvsocket::exec method in the client side, connection is established to the server side WSCvssocket. Once the connection is established, an ACTIVATE event arises on both client and server side to communicate in the event procedure.

> Here is a sample of the event procedure starting in the ACTIVATE followed by success of connection which sends/receives client-side data.

>     #include <WScom.h>
>     #include <WSCfunctionList.h>
>     #include <WSCbase.h>
>     //----------------------------------------------------------
>     //Function for the event procedure
>     //----------------------------------------------------------
>     #include <WSCvcsocket.h>

>     void com_ep(WSCbase* object){
>       //do something...
>       WSCvcsocket* sock = (WSCvcsocket*)object->cast("WSCvcsocket");
>       char buffer[128];
>       sprintf(buffer,"test!!! %d",cnt);
>       cnt++;

>       //send data;
>       long send_len = sock->write((WSCuchar*)buffer,128);
>       if (send_len == 128){
>         //success! do something..
>       }else{
>         //error!
>         return;
>       }

>       //receive data;
>       buffer[0] = 0;
>       long recv_len = sock->read((WSCuchar*)buffer,128);
>       if (recv_len == 128){
>         //success! do something..
>       }else{
>         //error!
>         return;
>       }
>     }
>     static WSCfunctionRegister  op("com_ep",(void*)com_ep);

> Next sample is the procedure for transmitting server-side data starting by ACTIVATE in establishing a connection.

>     #include <WScom.h>
>     #include <WSCfunctionList.h>
>     #include <WSCbase.h>
>     //----------------------------------------------------------
>     //Function for the event procedure
>     //----------------------------------------------------------
>     #include <WSCvssocket.h>

>     void com_ep(WSCbase* object){
>       //do something...
>       WSCvssocket* obj = (WSCvssocket*)object->cast("WSCvssocket");
>       char buffer[128];

>       //receive data:
>       //Store data from a client into buffer
>       obj->read((WSCuchar*)buffer,128);

>       //send data:
>       //Store data for a client into buffer to send it
>       strcpy(buffer,"send data...");
>       obj->write((WSCuchar*)buffer,128);
>     }
>     static WSCfunctionRegister  op("com_ep",(void*)com_ep);

> Next is an client-side event procedure which transmits data after establishing connection.

>     #include <WScom.h>
>     #include <WSCfunctionList.h>
>     #include <WSCbase.h>
>     //----------------------------------------------------------
>     //Function for the event procedure
>     //----------------------------------------------------------
>     #include <WSCvcsocket.h>
>     extern WSCvcsocket* newvcso_000;

>     void btnop(WSCbase* object){
>       //Initiate connection and exchange data after connection
>       long ret = newvcso_000->exec();
>       if (ret != WS_NO_ERR){ //connection failed
>         return;
>       }
>     }
>     static WSCfunctionRegister  op("btnop",(void*)btnop);

> ### How to use a broadcast network communication using UDP socket

> By using UDP, data can be sent to unspecified number of recipients (broadcast address: usu., xxx.xxx.xxx.255) The data can be received by processes who wait in the port specified in sending.

> For the setting in sending, referral socket port should be specified in WSNport. Also broadcast address, usually this is 255.255.255.255 is set in WSNip. In the system which is not allowed 255.255.255.255, the referral network address but with 255 in its last digit can be specified. For example, when you want to send to unspecified number of machines on the network:10.20.30.0, 10.20.30.255 can be specified. For the recipient's property settings, the awaiting socket port is set in WSNport and turn WSNrunning "ON". As for the sender side, since connection is not being established, it becomes simple comparing TCP. It is as simple as sending data calling WSCvudpsocket::write.

>     #include <WScom.h>
>     #include <WSCfunctionList.h>
>     #include <WSCbase.h>
>     //----------------------------------------------------------
>     //Function for the event procedure
>     //----------------------------------------------------------
>     #include <WSCvudpsocket.h>
>     extern WSCvudpsocket* newvudp_000;

>     void btnop(WSCbase* object){
>       static long cnt = 0;
>       WSCuchar buffer[64];
>       //send data in buffer
>       strcpy(buffer,"send data..");
>       long ret = newvudp_000->write(buffer,64);
>       if (ret < 64){
>         //failed.
>       }else{
>         //success
>       }
>     }
>     static WSCfunctionRegister  op("btnop",(void*)btnop);

> As for the recipient side, it receives in the event procedure which starts by ACTIVATE as the same as TCP's server side.

>     #include <WScom.h>
>     #include <WSCfunctionList.h>
>     #include <WSCbase.h>
>     //----------------------------------------------------------
>     //Function for the event procedure
>     //----------------------------------------------------------
>     #include <WSCvudpsocket.h>
>     extern WSCvudpsocket* newvudp_000;

>     void recvop(WSCbase* object){
>       WSCuchar buffer[64];
>       //Receiving data
>       newvudp_000->read(buffer,64);
>     }
>     static WSCfunctionRegister  op("recvop",(void*)recvop);

Document Release 3.70 for WideStudio ver 3.70, Feb 2004

WideStudio documents index | Table of contents

Copyright(C) T. Hirabayashi, 2000-2004 | Last modified: Feb 25, 2004