Connect to SIGVerse server via socket using Winsock
[
Front page
] [
New
|
List of pages
|
Search
|
Recent changes
|
Help
]
Start:
//
// Written by D. D. Khoa on 2013 Apr
//
Up:[[Tutorial]]
----
#contents
*Connect to SIGVerse controller via socket using Winsock ...
※ This section explains how to connect to SIGVerse contro...
※ The demo application in this tutorial is ''Receive imag...
- Environment: C++.Net Windows Form Application program, ...
- The script of this application is as follow:
-- Build a form having 3 buttons (for the sake of clarity...
--- ''Connect'': connect to controller
--- ''Get Image'': get image from controller to client co...
--- ''Close'': close a socket after receiving an image fr...
-- When the button ''Connect'' is pushed:
--- [Client] The ''sndmsg'' function is called to notify ...
--- [Controller] After receiving the notification from cl...
--- [Client] Call the ''connect'' function to make a conn...
-- When the button ''Get Image'' is pushed:
--- [Client] Call the ''sndmsg'' to notify the controller...
--- [Controller] After receiving the notification from cl...
--- [Client] Receive blocks of data until no more data is...
-- When the button ''Close'' is pushed:
--- [Client] The ''sndmsg'' is called to notify the contr...
--- [Controller] After receiving the notification, the co...
※ Some problems:
- This tutorial used ''blocking'' mode socket. It means t...
- This tutorial requires using Inamura Lab network or you...
- The problem of this tutorial is by using socket, you ha...
** Set up the environment [#j07cee5a]
*** Client side [#d82bf052]
- Create a C++.Net Windows Form Application like this:
&ref(Form.JPG);
- Add a reference to the ''sigverse.dll''. For more infor...
- Because my client program runs on Windows, I use Winsoc...
- Declare some variables:
-- ''wsData'' for the Winsock as global variables.
-- ''skServer'' for containing server ID
-- ''msgSIG'' as a MessageSender variable for sending not...
-- ''bConnectedFlag'' is '''true''' if it's ready to send...
#highlight(c++){{
...
#include <winsock2.h>
#pragma comment (lib,"ws2_32.lib")
SOCKET skServer; //Contain server socket ID
WSADATA wsData; //For using Winsock
public ref class Form1 : public System::Windows::Forms::F...
{
//For sending message to SIGVerse using the sndmsg function
private: sigverse::MessageSender^ msgSIG; //declare a Mes...
private: bool bConnectedFlag;
public:
Form1(void)
{
...
WSAStartup(0x0202,&wsData); //Start the Winsock
msgSIG = gcnew sigverse::MessageSender("ImageClient");
bConnectedFlag = false; //not connected yet
}
...
~Form1()
{
...
WSACleanup();
}
}}
*** Controller side [#o0020298]
- Basically, I reused this tutorial [[エージェント視点の...
- 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; //Socke...
bool bConnectedFlag; // is 'true' if there is no conne...
public:
...
//This function is called after the controller receive...
void handleImageSending(int iClientSock);
}
}}
**What happens at the client side? [#mf52e6f2]
***When the button "'''Connect'''" is pushed... [#fd56c77c]
- This button sends a message notify the controller that ...
- Add an push button event handler:
#highlight(c++){{
private: System::Void bt_connect_Click(System::Object^ s...
{
//Notify the controller to be ready to accept client conn...
char* sMsg[] = {"open socket"};
msgSIG->connect("socio2.iir.nii.ac.jp",9011);
msgSIG->sendto("robot_0",1,sMsg);
//Set up the connection
skServer = socket(AF_INET,SOCK_STREAM,IPPROTO_IP); //cre...
sockaddr_in skaddrServer; //for sever address
skaddrServer.sin_family = AF_INET; //this connection use...
skaddrServer.sin_port = htons( 9020 ); //Choose a port f...
skaddrServer.sin_addr.S_un.S_addr = (inet_addr("136.187....
//Then, we make a connection
if (connect(skServer, (sockaddr*)&skaddrServer, sizeof(s...
bConnectedFlag = true; //Success
}
}}
***When the button "'''Get Image'''" is pushed ... [#d79b...
- First, we notify the controller to start to send image ...
- Then, we receive the image size first, to stop waiting ...
- After that, we start receiving image data
#highlight(c++){{
private: System::Void bt_getimage_Click(System::Object^ ...
if (bConnectedFlag)
{
int nByteRecv, nSize; //Number of byte received each tim...
char sResponse[512]={0}; //data received each time
//Notify the controller to send image using socket
char* sMsg[] = {"get image"};
msgSIG->connect("socio2.iir.nii.ac.jp",9011);
msgSIG->sendto("robot_0",1,sMsg);
//Start receiving image data:
//Get the image size first
nSize = recv(skServer,sResponse,sizeof(sResponse),0);
nSize = atoi(sResponse);
//Open a file to write image data
FILE * fo = fopen("image.bmp","wb");
while(1)
{
nByteRecv = recv(skServer,sResponse,sizeof(sResponse),0...
fwrite(sResponse,sizeof(char),nByteRecv,fo);
//Check to know if all the image data is received enough
nSize -= nByteRecv;
if(nSize <= 0) //Break when no more data left
break;
}
fclose(fo);
}
}
}}
***When the button "'''Close'''" is pushed ... [#ub91d46d]
- Send a message to the controller to close the socket. T...
#highlight(c++){{
private: System::Void bt_close_Click(System::Object^ sen...
char* sMsg[] = {"close socket"};
msgSIG->connect("socio2.iir.nii.ac.jp",9011);
msgSIG->sendto(sAgentname,1,sMsg);
}
}}
**What happens at the controller side? [#l53b0996]
I attached in this tutorial the modified &ref(Capture_Vie...
- In the ''onInit'', bind a socket to a port and start li...
- In the ''onRecvMessage'', we will handle situations whe...
***When the ''open'' message is received... [#ecd5a6c8]
- The controller will call the ''accept'' function and wa...
#highlight(c++){{
else if (strcmp(msg.c_str(), "open socket") == 0)
{
unsigned int iAddrLen = sizeof(sockaddr);
if ((iClientSock = accept(iServerSock, (struct sockaddr ...
{ LOG_MSG(("Failed to accept client connection")); }
else
LOG_MSG(("Accepted connection"));
}
}}
***When the '''''get image''''' message is received... [#...
- Implement the ''handleImageSending'' function (just for...
- Open the image file want to send (for example, ''view00...
#highlight(c++){{
FILE * fi = fopen("view000.jpg","rb");
if (fi==NULL)
{
LOG_MSG(("Failed to open image file"));
return;
}
}}
- Send the image size first
#highlight(c++){{
/*Send the file size to client*/
char sSize[10];
long iSize;
fseek(fi,0,SEEK_END);
iSize = ftell(fi);
fseek(fi,0,SEEK_SET);
sprintf(sSize,"%d",iSize);
write(iClientSock,sSize,sizeof(sSize));
}}
- The final part of the code is to send image data to cli...
***When the '''''close''''' message is received... [#n560...
- Call the ''close'' function to close the socket.
#highlight(end)
#counter
End:
//
// Written by D. D. Khoa on 2013 Apr
//
Up:[[Tutorial]]
----
#contents
*Connect to SIGVerse controller via socket using Winsock ...
※ This section explains how to connect to SIGVerse contro...
※ The demo application in this tutorial is ''Receive imag...
- Environment: C++.Net Windows Form Application program, ...
- The script of this application is as follow:
-- Build a form having 3 buttons (for the sake of clarity...
--- ''Connect'': connect to controller
--- ''Get Image'': get image from controller to client co...
--- ''Close'': close a socket after receiving an image fr...
-- When the button ''Connect'' is pushed:
--- [Client] The ''sndmsg'' function is called to notify ...
--- [Controller] After receiving the notification from cl...
--- [Client] Call the ''connect'' function to make a conn...
-- When the button ''Get Image'' is pushed:
--- [Client] Call the ''sndmsg'' to notify the controller...
--- [Controller] After receiving the notification from cl...
--- [Client] Receive blocks of data until no more data is...
-- When the button ''Close'' is pushed:
--- [Client] The ''sndmsg'' is called to notify the contr...
--- [Controller] After receiving the notification, the co...
※ Some problems:
- This tutorial used ''blocking'' mode socket. It means t...
- This tutorial requires using Inamura Lab network or you...
- The problem of this tutorial is by using socket, you ha...
** Set up the environment [#j07cee5a]
*** Client side [#d82bf052]
- Create a C++.Net Windows Form Application like this:
&ref(Form.JPG);
- Add a reference to the ''sigverse.dll''. For more infor...
- Because my client program runs on Windows, I use Winsoc...
- Declare some variables:
-- ''wsData'' for the Winsock as global variables.
-- ''skServer'' for containing server ID
-- ''msgSIG'' as a MessageSender variable for sending not...
-- ''bConnectedFlag'' is '''true''' if it's ready to send...
#highlight(c++){{
...
#include <winsock2.h>
#pragma comment (lib,"ws2_32.lib")
SOCKET skServer; //Contain server socket ID
WSADATA wsData; //For using Winsock
public ref class Form1 : public System::Windows::Forms::F...
{
//For sending message to SIGVerse using the sndmsg function
private: sigverse::MessageSender^ msgSIG; //declare a Mes...
private: bool bConnectedFlag;
public:
Form1(void)
{
...
WSAStartup(0x0202,&wsData); //Start the Winsock
msgSIG = gcnew sigverse::MessageSender("ImageClient");
bConnectedFlag = false; //not connected yet
}
...
~Form1()
{
...
WSACleanup();
}
}}
*** Controller side [#o0020298]
- Basically, I reused this tutorial [[エージェント視点の...
- 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; //Socke...
bool bConnectedFlag; // is 'true' if there is no conne...
public:
...
//This function is called after the controller receive...
void handleImageSending(int iClientSock);
}
}}
**What happens at the client side? [#mf52e6f2]
***When the button "'''Connect'''" is pushed... [#fd56c77c]
- This button sends a message notify the controller that ...
- Add an push button event handler:
#highlight(c++){{
private: System::Void bt_connect_Click(System::Object^ s...
{
//Notify the controller to be ready to accept client conn...
char* sMsg[] = {"open socket"};
msgSIG->connect("socio2.iir.nii.ac.jp",9011);
msgSIG->sendto("robot_0",1,sMsg);
//Set up the connection
skServer = socket(AF_INET,SOCK_STREAM,IPPROTO_IP); //cre...
sockaddr_in skaddrServer; //for sever address
skaddrServer.sin_family = AF_INET; //this connection use...
skaddrServer.sin_port = htons( 9020 ); //Choose a port f...
skaddrServer.sin_addr.S_un.S_addr = (inet_addr("136.187....
//Then, we make a connection
if (connect(skServer, (sockaddr*)&skaddrServer, sizeof(s...
bConnectedFlag = true; //Success
}
}}
***When the button "'''Get Image'''" is pushed ... [#d79b...
- First, we notify the controller to start to send image ...
- Then, we receive the image size first, to stop waiting ...
- After that, we start receiving image data
#highlight(c++){{
private: System::Void bt_getimage_Click(System::Object^ ...
if (bConnectedFlag)
{
int nByteRecv, nSize; //Number of byte received each tim...
char sResponse[512]={0}; //data received each time
//Notify the controller to send image using socket
char* sMsg[] = {"get image"};
msgSIG->connect("socio2.iir.nii.ac.jp",9011);
msgSIG->sendto("robot_0",1,sMsg);
//Start receiving image data:
//Get the image size first
nSize = recv(skServer,sResponse,sizeof(sResponse),0);
nSize = atoi(sResponse);
//Open a file to write image data
FILE * fo = fopen("image.bmp","wb");
while(1)
{
nByteRecv = recv(skServer,sResponse,sizeof(sResponse),0...
fwrite(sResponse,sizeof(char),nByteRecv,fo);
//Check to know if all the image data is received enough
nSize -= nByteRecv;
if(nSize <= 0) //Break when no more data left
break;
}
fclose(fo);
}
}
}}
***When the button "'''Close'''" is pushed ... [#ub91d46d]
- Send a message to the controller to close the socket. T...
#highlight(c++){{
private: System::Void bt_close_Click(System::Object^ sen...
char* sMsg[] = {"close socket"};
msgSIG->connect("socio2.iir.nii.ac.jp",9011);
msgSIG->sendto(sAgentname,1,sMsg);
}
}}
**What happens at the controller side? [#l53b0996]
I attached in this tutorial the modified &ref(Capture_Vie...
- In the ''onInit'', bind a socket to a port and start li...
- In the ''onRecvMessage'', we will handle situations whe...
***When the ''open'' message is received... [#ecd5a6c8]
- The controller will call the ''accept'' function and wa...
#highlight(c++){{
else if (strcmp(msg.c_str(), "open socket") == 0)
{
unsigned int iAddrLen = sizeof(sockaddr);
if ((iClientSock = accept(iServerSock, (struct sockaddr ...
{ LOG_MSG(("Failed to accept client connection")); }
else
LOG_MSG(("Accepted connection"));
}
}}
***When the '''''get image''''' message is received... [#...
- Implement the ''handleImageSending'' function (just for...
- Open the image file want to send (for example, ''view00...
#highlight(c++){{
FILE * fi = fopen("view000.jpg","rb");
if (fi==NULL)
{
LOG_MSG(("Failed to open image file"));
return;
}
}}
- Send the image size first
#highlight(c++){{
/*Send the file size to client*/
char sSize[10];
long iSize;
fseek(fi,0,SEEK_END);
iSize = ftell(fi);
fseek(fi,0,SEEK_SET);
sprintf(sSize,"%d",iSize);
write(iClientSock,sSize,sizeof(sSize));
}}
- The final part of the code is to send image data to cli...
***When the '''''close''''' message is received... [#n560...
- Call the ''close'' function to close the socket.
#highlight(end)
#counter
Page: