Connect to SIGVerse controller via socket using Winsock (blocking mode)

※ 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

Client side

  • Create a C++.Net Windows Form Application like this: Form.JPG
  • Add a reference to the sigverse.dll. For more information, please refer to this tutorial メッセージ送信ツールの作成?.
  • Because my client program runs on Windows, I use Winsock for the socket.
  • Declare some variables:
    • wsData for the Winsock as global variables.
    • skServer for containing server ID
    • msgSIG as a MessageSender variable for sending notification message to the controller
    • bConnectedFlag is true if it's ready to send/receive image data

Controller side

  • Basically, I reused this tutorial エージェント視点の画像取得? and modify the captureView.cpp. So you use that tutorial to build the environment at the controller side first. Then we will add the code like below.
  • Add some more code to the RobotController class:

What happens at the client side?

When the button "Connect" is pushed...

  • 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, and then we make a connection.
  • Add an push button event handler:

When the button "Get Image" is pushed ...

  • First, we notify the controller to start to send image data
  • Then, we receive the image size first, to stop waiting for receiving data from controller when all the image data has been sent to client. Or you can find some way to let client know when all data has been sent so that client will not keep waiting anymore.
  • After that, we start receiving image data

When the button "Close" is pushed ...

  • 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.

What happens at the controller side?

I attached in this tutorial the modified fileCapture_View.cpp. I just note some important code here.

  • In the onInit, bind a socket to a port and start listening on this socket.
  • In the onRecvMessage, we will handle situations when a client sends the open, get image and close message.

When the open message is received...

  • The controller will call the accept function and wait until client call the connect function.

When the get image message is received...

  • Implement the handleImageSending function (just for the onRecvMessage function to be clear).
  • Open the image file want to send (for example, view000.jpg)
  • Send the image size first
  • The final part of the code is to send image data to client.

When the close message is received...

  • Call the close function to close the socket.
Counter: 2598, today: 2, yesterday: 0

Attach file: fileForm.JPG 1494 download [Information] fileCapture_View.cpp 1885 download [Information]

Front page   Edit Freeze Diff Backup Upload Copy Rename Reload   New List of pages Search Recent changes   Help   RSS of recent changes
Last-modified: 2014-02-06 (Thu) 13:36:09 (3026d)