Ahoj,
používám sadu MSP EXP432E401Y a tirtos_builds_MSP_EXP432E401Y_release_ccs_4_20_00_12
Příklad tcpecho_MSP_EXP432E401Y_tirtos_ccs nevykazuje žádný problém. Upravil jsem to tak, aby periodicky navazoval tcp spojení jako TCP klient a zase to ukončil. A po deseti připojeních a ukončeních mělo být navázáno další spojení. funkce lSocket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); kde lSocket == -1.
po chvíli bylo možné připojení obnovit a ukončit, ale pouze 10 spuštění. Můžete mi prosím poradit, co dělám špatně. Potřebuji mít možnost navázat a ukončit připojení na neurčito. Nepotřebuji více připojení najednou. Vše, co potřebuji, je jedna věc. Ale potřebuji, aby se po jejím dokončení dokázal bez problémů znovu obnovit.
ukázkový kód:
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <pthread.h>
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
/* BSD support */
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <ti/drivers/timer.h>
#include <netdb.h>
//#include <ti/ndk/inc/socketndk.h>
#include <ti/net/slnetutils.h>
#include <ti/display/Display.h>
#define TCPPACKETSIZE 256
#define NUMTCPWORKERS 3
#define MAXPORTLEN 6
extern Display_Handle display;
extern unsigned char count;
extern void fdOpenSession();
extern void fdCloseSession();
extern void *TaskSelf();
extern uint32_t inet_addr( const char * );
/*
* ======== tcpHandler ========
* Creates new Task to handle new TCP connections.
*/
void *tcpHandler(void *arg0)
{
int lSocket = -1;
struct sockaddr_in sLocalAddr;
int status;
while(1){
fdOpenSession(TaskSelf());
Display_printf(display, 0, 0, "TCP CLI Echo example started %u\n",(count+1));
lSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (lSocket < 0) {
Display_printf(display, 0, 0,"tcpHandler: socket failed (%d)\n",lSocket);
count = 0;
goto endsocket;
} else {
count++;
Display_printf(display, 0, 0," %d socket success\n", lSocket);
}
memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));
sLocalAddr.sin_family = AF_INET;
// sLocalAddr.sin_len = sizeof(sLocalAddr);
sLocalAddr.sin_addr.s_addr = inet_addr("192.168.1.42"); //IP of my computer
sLocalAddr.sin_port = htons(1000);//arg0);
if (status = connect (lSocket, (struct sockaddr *) & sLocalAddr, sizeof (sLocalAddr)) <0) {
Display_printf (display, 0, 0, "( %d; %d) DID NOT CONNECT \ n", lSocket, status );
počet = 0;
} else {
Display_printf (display, 0, 0, "(%d) CONNECTED \ n", stav);
Task_sleep (3000);
}
endsocket:
status = close (lSocket);
Display_printf (display, 0, 0, "(%d) DISCONNECTED SOC \ n", status);
Task_sleep (1000);
fdCloseSession (TaskSelf ());
}
return (NULL);
}
Děkuji za radu
Mirek
/* * Copyright (c) 2014-2019, Texas Instruments Incorporated * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name of Texas Instruments Incorporated nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* * ======== tcpEcho.c ======== * Contains BSD sockets code. */ #include <string.h> #include <stdint.h> #include <stdio.h> #include <pthread.h> #include <ti/sysbios/BIOS.h> #include <ti/sysbios/knl/Task.h> /* BSD support */ #include <netinet/in.h> #include <arpa/inet.h> #include <sys/socket.h> #include <ti/drivers/timer.h> #include <netdb.h> //#include <ti/ndk/inc/socketndk.h> #include <ti/net/slnetutils.h> #include <ti/display/Display.h> #define TCPPACKETSIZE 256 #define NUMTCPWORKERS 3 #define MAXPORTLEN 6 extern Display_Handle display; extern unsigned char count; extern void fdOpenSession(); extern void fdCloseSession(); extern void *TaskSelf(); extern uint32_t inet_addr( const char * ); //extern int inet_aton( const char *, struct in_addr * ); Display_Handle display; /* * ======== tcpWorker ======== * Task to handle TCP connection. Can be multiple Tasks running * this function. */ void *tcpWorker(void *arg0) { int clientfd = *(int *)arg0; int bytesRcvd; int bytesSent; char buffer[TCPPACKETSIZE]; fdOpenSession(TaskSelf()); Display_printf(display, 0, 0, "tcpWorker: start clientfd = 0x%x\n", clientfd); while ((bytesRcvd = recv(clientfd, buffer, TCPPACKETSIZE, 0)) > 0) { bytesSent = send(clientfd, buffer, bytesRcvd, 0); if (bytesSent < 0 || bytesSent != bytesRcvd) { Display_printf(display, 0, 0, "send failed.\n"); break; } } Display_printf(display, 0, 0, "tcpWorker stop clientfd = 0x%x\n", clientfd); close(clientfd); fdCloseSession(TaskSelf()); return (NULL); } /* * ======== 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]; */ int lSocket = -1; struct sockaddr_in sLocalAddr; int status; while(1){ fdOpenSession(TaskSelf()); Display_printf(display, 0, 0, "TCP CLI Echo example started %u\n",(count+1)); lSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (lSocket < 0) { Display_printf(display, 0, 0,"tcpHandler: socket failed (%d)\n",lSocket); count = 0; goto endsocket; } else { count++; Display_printf(display, 0, 0," %d socket success\n", lSocket); } memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr)); sLocalAddr.sin_family = AF_INET; // sLocalAddr.sin_len = sizeof(sLocalAddr); sLocalAddr.sin_addr.s_addr = inet_addr("192.168.1.42"); //IP of my computer sLocalAddr.sin_port = htons(1000);//arg0); if (status = connect(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr)) < 0){ Display_printf(display, 0, 0,"(%d ; %d) DID NOT CONNECT \n", lSocket,status); count = 0; } else { Display_printf(display, 0, 0,"(%d) CONNECTED\n",status); Task_sleep(3000); } endsocket: status = close(lSocket); Display_printf(display, 0, 0,"(%d) DISCONNECTED SOC\n",status); Task_sleep(1000); // System_flush(); fdCloseSession(TaskSelf()); } return (NULL); }