[vtkusers] vtkSocketCommunicator strange behaviour

Henry Lehmann henry.lehmann at informatik.tu-freiberg.de
Thu Apr 25 05:47:09 EDT 2013


Hello,

I am playing around with vtkSocketCommunicator. I want to send integral 
data types and
wrote a program to get myself into this topic. The source is at the end.
If the program is started without command line arguments, 
vtkSocketCommunicator
goes into server mode and waits for a connection at port 7777. If it is 
started
with one command line argument, the string is send to the server who
displays it in the terminal. However, the data is not transmitted correctly.

Consider this example:
At first start in server mode tp receive data:
$ ./program
Secondly start in client mode and send data:
$ ./program ABCDEFGHIJKLMNOPQRSTUVWXYZ

The output of the programs is as follows:

Server:
comm->WaitForConnection(7777)
Receive
ABCDEFGH????MNOPQRSTUVWXYZ
26
65
66
67
68
69
70
71
72
1
0
0
0
77
78
79
80
81
82
83
84
85
86
87
88
89
90
comm->Barrier()
comm->CloseConnection()

Client:
comm->ConnectTo(localhost, 7777)
Send
ABCDEFGHIJKLMNOPQRSTUVWXYZ
26
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
comm->Barrier()
comm->CloseConnection()

As you can see the data is not transmitted correctly and the numbers
73, 74, 75, 76 are replaced by 1, 0, 0, 0.
What am i doing wrong? Am i using this class not as intended?

Thanks in advance,
Henry.

Source Code:

#include <vtkNew.h>
#include <vtkSocketCommunicator.h>
#include <vtkClientServerStream.h>
#include <iostream>
#include <string>
int main(int argc, char *args[])
{
     using namespace std;

     vtkNew<vtkSocketCommunicator> comm;
     comm->SetPerformHandshake(1);
     if (argc == 1)
     {
         cout << "comm->WaitForConnection(7777)" << endl;
         comm->WaitForConnection(7777);
     }
     else if (argc == 2)
     {
         cout << "comm->ConnectTo(localhost, 7777)" << endl;
         comm->ConnectTo("localhost", 7777);
     }
     comm->Handshake();
     comm->Barrier();
     if (comm->GetIsServer())
     {
         std::size_t size = 0;
         comm->Receive(&size, 1, /* remoteHandle */ 1, 0);
         std::string str(size, '\n');
         comm->Receive(&str[0], size, /* remoteHandle */ 1, 1);
         cout << "Receive" << endl;
         cout << str << endl;
         cout << size << endl;
         for (std::size_t i = 0; i < size; ++i)
         {
             cout << int(str[i]) << endl;
         }
     }
     else
     {
         std::string str(args[1]);
         std::size_t size = str.size();
         comm->Send(&size, 1, /* remoteHandle */ 1, 0);
         comm->Send(str.c_str(), size, /* remoteHandle */ 1, 1);
         cout << "Send" << endl;
         cout << str << endl;
         cout << size << endl;
         for (std::size_t i = 0; i < size; ++i)
         {
             cout << int(str[i]) << endl;
         }
     }
     cout << "comm->Barrier()" << endl;
     comm->Barrier();
     cout << "comm->CloseConnection()" << endl;
     comm->CloseConnection();
     return 0;
}




More information about the vtkusers mailing list