Part Number: MSP432E401Y
The "tcpHandler" function within the "tcpEcho.c" source file from the example "tcpecho_MSP_EXP432E401Y_freertos_ccs" implements a tcp server function which allows socket connections requested by other network devices.
I need to modifiy tcpHandler function to implement a tcp client function which is passed the IP address and port number of a network host runing a tcp server.
Does anyone have an example of a suitable tcp client function for the MSP432 running under Freertos please?
The exisitng tcpHandler function code is as follows:
/*
* ======== tcpHandler ========
* Creates new Task to handle new TCP connections.
*/
void *tcpHandler(void *arg0)
{
pthread_t thread;
pthread_attr_t attrs;
struct sched_param priParam;
int retc;
int detachState;
int status;
int server = -1;
struct addrinfo hints;
struct addrinfo *res, *p;
struct sockaddr_in clientAddr;
int optval;
static int clientfd;
int optlen = sizeof(optval);
socklen_t addrlen = sizeof(clientAddr);
char portNumber[MAXPORTLEN];
fdOpenSession(TaskSelf());
Display_printf(display, 0, 0, "TCP Echo example started\n");
sprintf(portNumber, "%d", *(uint16_t *)arg0);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
/* Obtain addresses suitable for binding to */
status = getaddrinfo(NULL, portNumber, &hints, &res);
if (status != 0) {
Display_printf(display, 0, 0, "tcpHandler: getaddrinfo() failed: %s\n",
gai_strerror(status));
goto shutdown;
}
/* Open a socket */
for (p = res; p != NULL; p = p->ai_next) {
server = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (server == -1) {
continue;
}
status = bind(server, p->ai_addr, p->ai_addrlen);
if (status != -1) {
break;
}
close(server);
}
if (server == -1) {
Display_printf(display, 0, 0, "tcpHandler: failed to open socket\n");
goto shutdown;
} else if (p == NULL) {
Display_printf(display, 0, 0, "tcpHandler: could not bind to socket: %d\n", status);
goto shutdown;
} else {
freeaddrinfo(res);
res = NULL;
}
status = listen(server, NUMTCPWORKERS);
if (status == -1) {
Display_printf(display, 0, 0, "tcpHandler: listen failed\n");
goto shutdown;
}
optval = 1;
status = setsockopt(server, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen);
if (status == -1) {
Display_printf(display, 0, 0, "tcpHandler: setsockopt failed\n");
goto shutdown;
}
while ((clientfd =
accept(server, (struct sockaddr *)&clientAddr, &addrlen)) != -1) {
Display_printf(display, 0, 0,
"tcpHandler: Creating thread clientfd = %x\n", clientfd);
/* Set priority and stack size attributes */
pthread_attr_init(&attrs);
priParam.sched_priority = 3;
detachState = PTHREAD_CREATE_DETACHED;
retc = pthread_attr_setdetachstate(&attrs, detachState);
if (retc != 0) {
Display_printf(display, 0, 0,
"tcpHandler: pthread_attr_setdetachstate() failed");
while (1);
}
pthread_attr_setschedparam(&attrs, &priParam);
retc |= pthread_attr_setstacksize(&attrs, 2048);
if (retc != 0) {
Display_printf(display, 0, 0,
"tcpHandler: pthread_attr_setstacksize() failed");
while (1);
}
retc = pthread_create(&thread, &attrs, tcpWorker, (void *)&clientfd);
if (retc != 0) {
Display_printf(display, 0, 0,
"tcpHandler: pthread_create() failed");
while (1);
}
/* addrlen is a value-result param, must reset for next accept call */
addrlen = sizeof(clientAddr);
}
Display_printf(display, 0, 0, "tcpHandler: accept failed.\n");
shutdown:
if (res) {
freeaddrinfo(res);
res = NULL;
}
if (server != -1) {
close(server);
}
fdCloseSession(TaskSelf());
return (NULL);
}