Part Number: TM4C1294NCPDT
Tool/software: TI C/C++ Compiler
We have successfully implemented TCP Client and it is getting connected to Server and sending data in regular intervals.
Now we are looking for TCP Server in the same application to receive data from Server.
Port numbers for Sending and Receiving are different.
Can you please suggest how to implement TCP Client and Server in one application.
Please find the attached code for reference, tcpHandler as Client, tcpHandler2 as Server.
Void tcpHandler(UArg arg0, UArg arg1)
{
SOCKET lSocket;
int status;
struct sockaddr_in sLocalAddr;
int clientfd;
int temp,temp1;
int txbytes;
int txlen;
int optval;
int optlen = sizeof(optval);
unsigned long rc;
struct linger lingerConfig;
WD_Flag ^= true;
fdOpenSession(TaskSelf());
lSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (lSocket < 0) {
System_printf("tcpHandler: socket failed\n");
Task_exit();
return;
}
memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));
sLocalAddr.sin_family = AF_INET;
sLocalAddr.sin_addr.s_addr = inet_addr(Server_IP_Address);
sLocalAddr.sin_port = htons(arg0);
while(clientfd = (connect(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr)) < 0)){
SysCtlDelay(g_ui32SysClock/100/3);
}
System_flush();
status = bind(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr));
if (status == -1) {
System_printf("Error: bind failed.\n");
}
optval = 1;
if (setsockopt(lSocket, SOL_SOCKET, SO_RCVTIMEO, &optval, optlen) < 0) {
System_printf("Error: setsockopt failed\n");
}
while (true) {
MAP_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_5,
(MAP_GPIOPinRead(GPIO_PORTN_BASE, GPIO_PIN_5) ^
GPIO_PIN_5));
if (Semaphore_getCount(semHandle) == 0) {
System_printf("Sem blocked in TCP Sender \n");
}
Semaphore_pend(semHandle, BIOS_NO_WAIT);
WD_Flag ^= true;
tcp_connected = 1;
connstatuscntr = 0;
if((recv_comp == 1)&&(tcp_connected == 1)){
txbytes = send(lSocket, (char *)TCP_Send_Buffer,strlen(TCP_Send_Buffer),0);
if(txbytes == strlen(TCP_Send_Buffer)){
recv_comp = 0;
Tare_Key = 0;
memset(T_Weight,0x00,10);
f2 = 0;
recv_comp = 0;
}
else;
}
else;
if (connect(lSocket, (struct sockaddr *) &sLocalAddr, sizeof(sLocalAddr)) < 0){
System_printf("DID NOT CONNECT \n");
System_printf("%d\n", fdError());
} else {
System_printf("CONNECTED \n");
}
resource = 0;
// /* Unlock resource */
//
Semaphore_post(semHandle);
Task_sleep(1);
}
}
Void tcpWorker(UArg arg0, UArg arg1)
{
int clientfd = (int)arg0;
int bytesRcvd;
int bytesSent;
char buffer[256];
System_printf("tcpWorker: start clientfd = 0x%x\n", clientfd);
while ((bytesRcvd = recv(clientfd, buffer, 256, 0)) > 0) {
bytesSent = send(clientfd, buffer, bytesRcvd, 0);
if (bytesSent < 0 || bytesSent != bytesRcvd) {
System_printf("Error: send failed.\n");
break;
}
}
System_printf("tcpWorker stop clientfd = 0x%x\n", clientfd);
}
Void tcpHandler2(UArg arg0, UArg arg1)
{
int socketl;
int status;
struct sockaddr_in sLocalAddr;
Error_Block eb;
int clientfd;
int nbytes;
int optval;
int optlen = sizeof(optval);
char ip_len=0;
// int iCnt,jCnt;
char trecvbuffer[20];
int c = 0;
char B_Rate[10];
unsigned int recv_ip;//,sIP;
char dlen,Receive_Fram_Len;
char *eptr;
uint32_t e2size,e2block,returnCode;
int server;
struct sockaddr_in localAddr;
struct sockaddr_in clientAddr;
socklen_t addrlen = sizeof(clientAddr);
Task_Handle taskHandle;
Task_Params taskParams;
WD_Flag ^= true;
fdOpenSession(TaskSelf());
server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (server == -1) {
System_printf("Error: socket not created.\n");
//goto shutdown;
}
memset(&localAddr, 0, sizeof(localAddr));
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
localAddr.sin_port = 5001;//htons(arg0);
status = bind(server, (struct sockaddr *)&localAddr, sizeof(localAddr));
if (status == -1) {
System_printf("Error: bind failed.\n");
//goto shutdown;
}
status = listen(server, NUMTCPWORKERS);
if (status == -1) {
System_printf("Error: listen failed.\n");
//goto shutdown;
}
optval = 1;
if (setsockopt(server, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
System_printf("Error: setsockopt failed\n");
//goto shutdown;
}
while ((clientfd =
accept(server, (struct sockaddr *)&clientAddr, &addrlen)) != -1) {
System_printf("tcpHandler: Creating thread clientfd = %d\n", clientfd);
/* Init the Error_Block */
Error_init(&eb);
/* Initialize the defaults and set the parameters. */
Task_Params_init(&taskParams);
taskParams.arg0 = (UArg)clientfd;
taskParams.stackSize = 1280;
taskHandle = Task_create((Task_FuncPtr)tcpWorker, &taskParams, &eb);
if (taskHandle == NULL) {
System_printf("Error: Failed to create new Task\n");
close(clientfd);
}
/* addrlen is a value-result param, must reset for next accept call */
addrlen = sizeof(clientAddr);
}
System_printf("Error: accept failed.\n");
}