<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">Thanks for your reply.<br>
<br>
I changed the program, but the error remains the same.<br>
I put the Barrier inside the if-else-scopes so the local variables
will<br>
not get destroyed until the barrier is reached by all<br>
processes. <br>
<br>
I even put a barrier behind the send of the size, so that<br>
the size is send/received correctly before the string buffers<br>
for receiving the strings are created.<br>
<br>
I changed the example to send the same string 2 times.<br>
For the second time it works and for the first time<br>
the wrong data always is at the same position.<br>
<br>
Henry<br>
<br>
New Output:<br>
Server:<br>
comm->WaitForConnection(7777)<br>
Receive<br>
comm->Barrier()<br>
comm->Barrier()<br>
Data<br>
65, 65<br>
66, 66<br>
67, 67<br>
68, 68<br>
69, 69<br>
70, 70<br>
71, 71<br>
72, 72<br>
1, 73<br>
0, 74<br>
0, 75<br>
0, 76<br>
77, 77<br>
78, 78<br>
79, 79<br>
80, 80<br>
81, 81<br>
82, 82<br>
83, 83<br>
84, 84<br>
85, 85<br>
86, 86<br>
87, 87<br>
88, 88<br>
89, 89<br>
90, 90<br>
comm->CloseConnection()<br>
<br>
Client:<br>
comm->ConnectTo(localhost, 7777)<br>
Send<br>
comm->Barrier()<br>
comm->Barrier()<br>
Data<br>
65, 65<br>
66, 66<br>
67, 67<br>
68, 68<br>
69, 69<br>
70, 70<br>
71, 71<br>
72, 72<br>
73, 73<br>
74, 74<br>
75, 75<br>
76, 76<br>
77, 77<br>
78, 78<br>
79, 79<br>
80, 80<br>
81, 81<br>
82, 82<br>
83, 83<br>
84, 84<br>
85, 85<br>
86, 86<br>
87, 87<br>
88, 88<br>
89, 89<br>
90, 90<br>
comm->CloseConnection()<br>
<br>
Updated source code:<br>
#include <vtkNew.h><br>
#include <vtkSocketCommunicator.h><br>
#include <vtkClientServerStream.h><br>
#include <iostream><br>
#include <string><br>
int main(int argc, char *args[])<br>
{<br>
using namespace std;<br>
<br>
vtkNew<vtkSocketCommunicator> comm;<br>
comm->SetPerformHandshake(1);<br>
if (argc == 1)<br>
{<br>
cout << "comm->WaitForConnection(7777)" <<
endl;<br>
comm->WaitForConnection(7777);<br>
}<br>
else if (argc == 2)<br>
{<br>
cout << "comm->ConnectTo(localhost, 7777)"
<< endl;<br>
comm->ConnectTo("localhost", 7777);<br>
}<br>
comm->Handshake();<br>
comm->Barrier();<br>
if (comm->GetIsServer())<br>
{<br>
cout << "Receive" << endl;<br>
<br>
std::size_t size = 0;<br>
comm->Receive(&size, 1, /* remoteHandle */ 1, 0);<br>
cout << "comm->Barrier()" << endl;<br>
comm->Barrier();<br>
<br>
std::string str1(size, '\n');<br>
std::string str2(size, '\n');<br>
comm->Receive(&str1[0], size, /* remoteHandle */ 1,
1);<br>
comm->Receive(&str2[0], size, /* remoteHandle */ 1,
2);<br>
<br>
cout << "comm->Barrier()" << endl;<br>
comm->Barrier();<br>
cout << "Data" << endl;<br>
for (std::size_t i = 0; i < size; ++i)<br>
{<br>
cout << int(str1[i]) << ", " <<
int(str2[i]) << endl;<br>
}<br>
}<br>
else<br>
{<br>
cout << "Send" << endl;<br>
std::string str1(args[1]);<br>
std::string str2(args[1]);<br>
std::size_t size = str1.size();<br>
comm->Send(&size, 1, /* remoteHandle */ 1, 0);<br>
cout << "comm->Barrier()" << endl;<br>
comm->Barrier();<br>
<br>
comm->Send(str1.c_str(), size, /* remoteHandle */ 1,
1);<br>
comm->Send(str2.c_str(), size, /* remoteHandle */ 1,
2);<br>
<br>
cout << "comm->Barrier()" << endl;<br>
comm->Barrier();<br>
cout << "Data" << endl;<br>
for (std::size_t i = 0; i < size; ++i)<br>
{<br>
cout << int(str1[i]) << ", " <<
int(str2[i]) << endl;<br>
}<br>
}<br>
cout << "comm->CloseConnection()" << endl;<br>
comm->CloseConnection();<br>
return 0;<br>
}<br>
<br>
<br>
The new source code is:<br>
<br>
On 04/25/2013 12:20 PM, Joachim Pouderoux wrote:<br>
</div>
<blockquote
cite="mid:CACJ=gTiJ3DEvZiYFSz=eyGYiqRsXmKN6kXn0KAXJmZPAQA=M5A@mail.gmail.com"
type="cite">
<div dir="ltr">
<div>
<div>I did not try your example yet but I think the issue is
due to your local src.c_str() which is destroyed before the
send action is really performed (send is a asynchronous
function).<br>
</div>
So you can try something like : <br>
<br>
else<br>
{<br>
std::string str(args[1]);<br>
std::size_t size = str.size();<br>
const char *s = str.c_str();<br>
comm->Send(&size, 1, /* remoteHandle */ 1, 0);<br>
comm->Send(s, size, /* remoteHandle */ 1, 1);<br>
cout << "Send" << endl;<br>
cout << str << endl;<br>
cout << size << endl;<br>
for (std::size_t i = 0; i < size; ++i)<br>
{<br>
cout << int(str[i]) << endl;<br>
} <br>
cout << "server comm->Barrier()" <<
endl;<br>
comm->Barrier();<br>
}<br>
<br>
<br>
</div>
Regads,<br>
</div>
<div class="gmail_extra"><br clear="all">
<div><b>Joachim Pouderoux</b><br>
<blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px
solid rgb(204,204,204);padding-left:1ex"><font size="1"><i>PhD,
R&D Enginee</i>r</font> <br>
<b><font size="1"><a moz-do-not-send="true"
href="http://www.kitware.fr" target="_blank">Kitware
SAS</a></font></b><br>
</blockquote>
</div>
<br>
<br>
<div class="gmail_quote">2013/4/25 Henry Lehmann <span
dir="ltr"><<a moz-do-not-send="true"
href="mailto:henry.lehmann@informatik.tu-freiberg.de"
target="_blank">henry.lehmann@informatik.tu-freiberg.de</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
Hello,<br>
<br>
I am playing around with vtkSocketCommunicator. I want to
send integral data types and<br>
wrote a program to get myself into this topic. The source is
at the end.<br>
If the program is started without command line arguments,
vtkSocketCommunicator<br>
goes into server mode and waits for a connection at port
7777. If it is started<br>
with one command line argument, the string is send to the
server who<br>
displays it in the terminal. However, the data is not
transmitted correctly.<br>
<br>
Consider this example:<br>
At first start in server mode tp receive data:<br>
$ ./program<br>
Secondly start in client mode and send data:<br>
$ ./program ABCDEFGHIJKLMNOPQRSTUVWXYZ<br>
<br>
The output of the programs is as follows:<br>
<br>
Server:<br>
comm->WaitForConnection(7777)<br>
Receive<br>
ABCDEFGH???? MNOPQRSTUVWXYZ<br>
26<br>
65<br>
66<br>
67<br>
68<br>
69<br>
70<br>
71<br>
72<br>
1<br>
0<br>
0<br>
0<br>
77<br>
78<br>
79<br>
80<br>
81<br>
82<br>
83<br>
84<br>
85<br>
86<br>
87<br>
88<br>
89<br>
90<br>
comm->Barrier()<br>
comm->CloseConnection()<br>
<br>
Client:<br>
comm->ConnectTo(localhost, 7777)<br>
Send<br>
ABCDEFGHIJKLMNOPQRSTUVWXYZ<br>
26<br>
65<br>
66<br>
67<br>
68<br>
69<br>
70<br>
71<br>
72<br>
73<br>
74<br>
75<br>
76<br>
77<br>
78<br>
79<br>
80<br>
81<br>
82<br>
83<br>
84<br>
85<br>
86<br>
87<br>
88<br>
89<br>
90<br>
comm->Barrier()<br>
comm->CloseConnection()<br>
<br>
As you can see the data is not transmitted correctly and the
numbers<br>
73, 74, 75, 76 are replaced by 1, 0, 0, 0.<br>
What am i doing wrong? Am i using this class not as
intended?<br>
<br>
Thanks in advance,<br>
Henry.<br>
<br>
Source Code:<br>
<br>
#include <vtkNew.h><br>
#include <vtkSocketCommunicator.h><br>
#include <vtkClientServerStream.h><br>
#include <iostream><br>
#include <string><br>
int main(int argc, char *args[])<br>
{<br>
using namespace std;<br>
<br>
vtkNew<vtkSocketCommunicator> comm;<br>
comm->SetPerformHandshake(1);<br>
if (argc == 1)<br>
{<br>
cout << "comm->WaitForConnection(7777)"
<< endl;<br>
comm->WaitForConnection(7777);<br>
}<br>
else if (argc == 2)<br>
{<br>
cout << "comm->ConnectTo(localhost, 7777)"
<< endl;<br>
comm->ConnectTo("localhost", 7777);<br>
}<br>
comm->Handshake();<br>
comm->Barrier();<br>
if (comm->GetIsServer())<br>
{<br>
std::size_t size = 0;<br>
comm->Receive(&size, 1, /* remoteHandle */ 1,
0);<br>
std::string str(size, '\n');<br>
comm->Receive(&str[0], size, /* remoteHandle
*/ 1, 1);<br>
cout << "Receive" << endl;<br>
cout << str << endl;<br>
cout << size << endl;<br>
for (std::size_t i = 0; i < size; ++i)<br>
{<br>
cout << int(str[i]) << endl;<br>
}<br>
}<br>
else<br>
{<br>
std::string str(args[1]);<br>
std::size_t size = str.size();<br>
comm->Send(&size, 1, /* remoteHandle */ 1,
0);<br>
comm->Send(str.c_str(), size, /* remoteHandle */
1, 1);<br>
cout << "Send" << endl;<br>
cout << str << endl;<br>
cout << size << endl;<br>
for (std::size_t i = 0; i < size; ++i)<br>
{<br>
cout << int(str[i]) << endl;<br>
}<br>
}<br>
cout << "comm->Barrier()" << endl;<br>
comm->Barrier();<br>
cout << "comm->CloseConnection()" <<
endl;<br>
comm->CloseConnection();<br>
return 0;<br>
}<br>
<br>
_______________________________________________<br>
Powered by <a moz-do-not-send="true"
href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at <a
moz-do-not-send="true"
href="http://www.kitware.com/opensource/opensource.html"
target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Please keep messages on-topic and check the VTK FAQ at: <a
moz-do-not-send="true"
href="http://www.vtk.org/Wiki/VTK_FAQ" target="_blank">http://www.vtk.org/Wiki/VTK_FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a moz-do-not-send="true"
href="http://www.vtk.org/mailman/listinfo/vtkusers"
target="_blank">http://www.vtk.org/mailman/listinfo/vtkusers</a><br>
</blockquote>
</div>
<br>
</div>
</blockquote>
<br>
</body>
</html>