P
Priack
Guest
I am trying to create an application that sends data from a program in C++ to another in Matlab. The size of the data is ~100 double @ 1000Hz and I am having problems with reaching that frequency (even in localhost).
Using the default parameters of the socket I get ~100Hz. Also I tried using the value TCP_NODELAY to false, but that make it worse. The processor is a i7-2600 @ 3.4 Hz so for sure that is not the problem. Also for the moment the only processing that I am doing with the data is save it, so also there is time consuming process.
Also I am sure that I can reach 1000 Hz because if I delete the send command, the output file has >1000 Hz.
The initialization code:
ConnectSocket = INVALID_SOCKET;
// Initialize Winsock
WSADATA wsaData;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
int iResult;
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
}
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo("localhost", DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
}
// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
}
// Connect to server.
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
}
Which is mainly (or completly) copy from the Microsoft web page. For the sending function we have:
if (RecordingFlag){
packet[51]=values[35];
packet[52]=numchDAQ;
for (i=0;i<values[35];i++){
fprintf(file,"%.3lf,",values);
packet =(double)values;
}
//DAQ
if (numchDAQ >0){
DAQmxReadAnalogF64(taskHandle,1,10.0,DAQmx_Val_GroupByChannel,data,16,&read,NULL);
for(i=0;i<numchDAQ;i++){
fprintf(file,"%.3lf,",data);
packet[(int)packet[51]+i]=(double)data;
}
}
//Timestamp
GetSystemTime(&st);
SystemTimeToFileTime(&st,&ft);
diftime= ((((ULONGLONG) ft.dwHighDateTime) << 32) + ft.dwLowDateTime)/10000 - MSEC_TO_UNIX_EPOCH;
fprintf(file,"%llu",diftime);
packet[0]=prueba;
prueba++;
send( ConnectSocket, (const char*)packet, sizeof(double)*53, 0 );
fprintf(file,"\n");
}
That function is called 1000 times per second. I guess that the problem is that Windows doesnt send sockets that fast, because is waiting for the ACK from the server. But again, the server in Matlab is also very simple:
import java.net.ServerSocket
import java.io.*
j=1;
i=0;
server_socket = [];
input_socket = [];
dataByte = int8(zeros (1,424));
dataDouble = zeros(10000,53);
while (1)
try
i= i+1;
fprintf(1, Trying to connect %d\n,i);
server_socket = ServerSocket(31415);
server_socket.setSoTimeout(5000);
input_socket = server_socket.accept;
fprintf(1, Client connected\n);
while (1)
input_stream = input_socket.getInputStream;
d_input_stream = DataInputStream(input_stream);
bytes_available = input_stream.available;
while(bytes_available>0 && j<10000)
%fprintf(1, Reading %d bytes\n, bytes_available);
for i =1:424
dataByte(i)= d_input_stream.readByte;
bytes_available = bytes_available-1;
end
dataDouble(j, = typecast(dataByte,double);
j=j+1;
end
end
% cleanup
input_socket.close;
catch
if ~isempty(server_socket)
server_socket.close;
end
if ~isempty(input_socket)
input_socket.close
end
% pause before retrying
%pause(1);
end
end
Even without make the typecast I get the same results.
Any ideas about how to solve this?
Continue reading...
Using the default parameters of the socket I get ~100Hz. Also I tried using the value TCP_NODELAY to false, but that make it worse. The processor is a i7-2600 @ 3.4 Hz so for sure that is not the problem. Also for the moment the only processing that I am doing with the data is save it, so also there is time consuming process.
Also I am sure that I can reach 1000 Hz because if I delete the send command, the output file has >1000 Hz.
The initialization code:
ConnectSocket = INVALID_SOCKET;
// Initialize Winsock
WSADATA wsaData;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
int iResult;
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
}
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo("localhost", DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
}
// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
}
// Connect to server.
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
}
Which is mainly (or completly) copy from the Microsoft web page. For the sending function we have:
if (RecordingFlag){
packet[51]=values[35];
packet[52]=numchDAQ;
for (i=0;i<values[35];i++){
fprintf(file,"%.3lf,",values);
packet =(double)values;
}
//DAQ
if (numchDAQ >0){
DAQmxReadAnalogF64(taskHandle,1,10.0,DAQmx_Val_GroupByChannel,data,16,&read,NULL);
for(i=0;i<numchDAQ;i++){
fprintf(file,"%.3lf,",data);
packet[(int)packet[51]+i]=(double)data;
}
}
//Timestamp
GetSystemTime(&st);
SystemTimeToFileTime(&st,&ft);
diftime= ((((ULONGLONG) ft.dwHighDateTime) << 32) + ft.dwLowDateTime)/10000 - MSEC_TO_UNIX_EPOCH;
fprintf(file,"%llu",diftime);
packet[0]=prueba;
prueba++;
send( ConnectSocket, (const char*)packet, sizeof(double)*53, 0 );
fprintf(file,"\n");
}
That function is called 1000 times per second. I guess that the problem is that Windows doesnt send sockets that fast, because is waiting for the ACK from the server. But again, the server in Matlab is also very simple:
import java.net.ServerSocket
import java.io.*
j=1;
i=0;
server_socket = [];
input_socket = [];
dataByte = int8(zeros (1,424));
dataDouble = zeros(10000,53);
while (1)
try
i= i+1;
fprintf(1, Trying to connect %d\n,i);
server_socket = ServerSocket(31415);
server_socket.setSoTimeout(5000);
input_socket = server_socket.accept;
fprintf(1, Client connected\n);
while (1)
input_stream = input_socket.getInputStream;
d_input_stream = DataInputStream(input_stream);
bytes_available = input_stream.available;
while(bytes_available>0 && j<10000)
%fprintf(1, Reading %d bytes\n, bytes_available);
for i =1:424
dataByte(i)= d_input_stream.readByte;
bytes_available = bytes_available-1;
end
dataDouble(j, = typecast(dataByte,double);
j=j+1;
end
end
% cleanup
input_socket.close;
catch
if ~isempty(server_socket)
server_socket.close;
end
if ~isempty(input_socket)
input_socket.close
end
% pause before retrying
%pause(1);
end
end
Even without make the typecast I get the same results.
Any ideas about how to solve this?
Continue reading...