Up:[[Tutorial]]

[Tutorial is under construction]

#contents

*Connect to SIGVerse controller via socket using Winsock (blocking mode)[#ief309b1]

※ This section explains how to connect to SIGVerse controller (run on Linux) via socket from a client computer (Windows) using Winsock. The socket code is integrated to  the controller, not the server.

※ The demo application in this tutorial is ''Receive images captured inside SIGVerse to local computer''. 
- Environment: C++.Net Windows Form Application program, Visual Studio 2008, Windows 7 (32bit).
- The script of this application is as follow:
-- Build a form having 3 buttons (for the sake of clarity, but you can combine the functionality of these 3 buttons in a way you want): 
--- ''Connect'': connect to controller
--- ''Get Image'': get image from controller to client computer
--- ''Close'': close a socket after receiving an image from controller
-- When the button ''Connect'' is pushed:
--- [Client] The ''sndmsg'' function is called to notify the controller that a client is about to connect.
--- [Controller] After receiving the notification from client, the controller will call the ''accept'' function and then it wait for connection from client. 
--- [Client] Call the ''connect'' function to make a connection.
-- When the button ''Get Image'' is pushed:
--- [Client] Call the ''sndmsg'' to notify the controller to send image data.
--- [Controller] After receiving the notification from client, the controller will open the image as a binary file, read this file and send to client as a block of 512 bytes (you can change this size).
--- [Client] Receive blocks of data until no more data is received, and then save as a file.
-- When the button ''Close'' is pushed:
--- [Client] The ''sndmsg'' is called to notify the controller to close the socket.
--- [Controller] After receiving the notification, the controller will close the socket. 

※ Some problems:
- This tutorial used ''blocking'' mode socket. It means the controller will wait for a client connection and can not do anything during this period. I also show you a temporary solution but it's not so convenient. A better solution is using ''non-blocking'' mode, which will be presented in another tutorial [future work].

- This tutorial requires using Inamura Lab network or you have to use the ''port forwarding'' in order to use the '''''sndmsg''''' function. For more information about this function, please refer to this tutorial [[メッセージ送信ツールの作成]].

- The problem of this tutorial is by using socket, you have to close it after the first time you finish receiving data from controller, or else the next time you would get a mess. This is considered an open problem for readers to try.

** Set up the environment [#j07cee5a]
*** Client side [#d82bf052]
- Create a C++.Net Windows Form Application like this:
- Add a reference to the ''sigverse.dll''. For more information, please refer to this tutorial [[メッセージ送信ツールの作成]].
*** Controller side [#o0020298]
- Basically, I reused this tutorial [[エージェント視点の画像取得]] and modify the ''captureView.cpp''. So you use that tutorial to build the environment at the controller side first. I will show you what to modify later.
- Add some more code to the ''RobotController'' class:

#highlight(c++){{
//Header files for using socket in the controller
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <errno.h>
...

class RobotController : public Controller
{
private:
   ...
   //Variables for handling sockets
   int iServerSock, iClientSock; //Socket IDs
   struct sockaddr_in skaddrServer, skaddrClient; //Socket addresses
   bool bConnectedFlag; // is 'true' if there is no connection problem so far

public:
   ...
   //This function is called after the controller receive "send image" message from client
   void handleImageSending(int iClientSock, int);
}
}}

**What happens at the client side? [#mf52e6f2]
***When the button "'''Connect'''" is pushed... [#fd56c77c]
- This button sends a message notify the controller that this client is going to make a connection, so that the controller will call the ''accept'' function to wait.
- Add an push button event handler:
#highlight(c++){{
private: System::Void bt_connect_Click(System::Object^  sender, System::EventArgs^  e) 
{
	char* sMsg[] = {"open socket"};
	msgSIG->connect("socio2.iir.nii.ac.jp",9011);
	msgSIG->sendto(sAgentname,1,sMsg);
}
}}
***When the button "'''Get Image'''" is pushed ... [#d79b58d8]
- 
***When the button "'''Close'''" is pushed ... [#ub91d46d]
- Send a message to the controller to close the socket. This is because when I tried to use this opened socket for receiving data the second time, it didn't work as I expected.
#highlight(c++){{
private: System::Void bt_close_Click(System::Object^  sender, System::EventArgs^  e) {
	char* sMsg[] = {"close socket"};
	msgSIG->connect("socio2.iir.nii.ac.jp",9011);
	msgSIG->sendto(sAgentname,1,sMsg);
}
}}
#highlight(end)

Front page   New List of pages Search Recent changes   Help   RSS of recent changes