Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New version of "tcp_recvdata" supporting the timeout feature #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 74 additions & 1 deletion SRC/simApplicationClient/c/tcp_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <stdlib.h>
#include <stdio.h>
#include <time.h> // added by Kiida Lai for the timeout feature of recvdata
#include <math.h>
#include <string.h>

Expand Down Expand Up @@ -518,6 +519,78 @@ void CALL_CONV tcp_recvdata(int *socketID, int *dataTypeSize, char data[], int *
}
}

/*
* tcp_recvdata_timeout() - function to receive data in blocking mode with timeout feature
* Note: this function for now only is tested on windows
* written by Kiida Lai ([email protected])
*
* input: int *socketID - socket identifier
* int *dataTypeSize - size of data type
* char *data - pointer to data to receive
* int *lenData - length of data
* double *timeout - if data didn't arrive on time, then return ierr = -3
* if a timeout < 0 is given, the function will not do the check
* unit: seconds
*
* return: int *ierr - 0 if successfull, negative number if not
*/
void CALL_CONV tcp_recvdata_timeout(int *socketID, int *dataTypeSize, char data[], int *lenData, int *ierr, double *timeout)
{
SocketConnection *theSocket = theSockets;
socket_type sockfd;
int nread, nleft;
double timer;
unsigned long nbMode = 0;
char *gMsg = data;
clock_t start_t = clock(); // from time.h

*ierr = 0;

// find the socket
while (theSocket != 0 && theSocket->socketID != *socketID)
theSocket=theSocket->next;
if (theSocket == 0) {
fprintf(stderr,"tcp_socket::recvdata() - could not find socket to receive data\n");
*ierr = -1;
return;
}
sockfd = theSocket->sockfd;

// turn on blocking mode
#ifdef _WIN32
if (ioctlsocket(sockfd, FIONBIO, &nbMode) != 0) {
fprintf(stderr,"tcp_socket::recvdata() - could not turn on blocking mode\n");
*ierr = -2;
return;
}
#else
if (ioctl(sockfd, FIONBIO, &nbMode) != 0) {
fprintf(stderr,"tcp_socket::recvdata() - could not turn on blocking mode\n");
*ierr = -2;
return;
}
#endif

// receive the data
// if o.k. get a pointer to the data in the message and
// place the incoming data there
nleft = *lenData * *dataTypeSize;

while (nleft > 0) {
// While loop will break when data isn't arrive on time
if (*timeout > 0) {
timer = (clock() - start_t) / CLOCKS_PER_SEC;
if (timer > *timeout) {
*ierr = -3;
fprintf(stderr,"tcp_socket::recvdata() - data didn't receive on time\n");
break;
}
}
nread = recv(sockfd, gMsg, nleft, 0);
nleft -= nread;
gMsg += nread;
}
}

/*
* tcp_recvnbdata() - function to receive data in nonblocking mode
Expand Down Expand Up @@ -612,4 +685,4 @@ void CALL_CONV tcp_getsocketid(unsigned int *port, const char machineInetAddr[],
}
fprintf(stderr,"tcp_socket::getsocketid() - could not find socket to get socketID\n");
*socketID = -2;
}
}
29 changes: 29 additions & 0 deletions SRC/simApplicationClient/simulink/Python_TCPserver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import socket
import struct

# STREAM for TCP/IP
portNum = 7200
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('localhost',portNum))
serversocket.listen(5)

print('Server start on Port '+str(portNum))
while True:
(connect, address) = serversocket.accept()
print("Connected to client IP: {}".format(address))

try:
while True:
msg_recv = connect.recv(24)
if not len(msg_recv):
break
else:
time = struct.unpack('<d', bytearray(msg_recv[ 0: 8]))[0]
data = struct.unpack('<d', bytearray(msg_recv[ 8:16]))[0]
flag = struct.unpack('<d', bytearray(msg_recv[16:24]))[0]
print('Client sent a data %.4f, %8.4f, %.4f' % (time, data, flag) + ', then do some calculations and return it.')
msg_send = struct.pack('<d', data*2+flag)
connect.send(msg_send+msg_send+msg_send+msg_send)
finally:
connect.close()
print('No data. Connection closed.')
Loading