Single TCP Socket Connection

Topics for the Eclipse Environment
Post Reply
tigger
Posts: 5
Joined: Wed Jun 01, 2016 8:22 am

Single TCP Socket Connection

Post by tigger »

Hello all,

I'm using Netburner MOD5270.
Here's the sample code that found from C:\nburn

Code: Select all

/**
    @file       main.cpp
    @brief      NetBurner Simple TCP/IP Server Example
    This program will create a TCP server task, which listens on port 23 by
    default. To test the application you can use Telnet. For example, from a
    windows commpan prompt, type `telent <ip address of netburner>`.
    This example uses a simple read() function to receive data from a TCP
    client.
**/

#include "predef.h"
#include <stdio.h>
#include <startnet.h>
#include <tcp.h>
#include <init.h>

const char *AppName = "Simple TCP Server Example";

#define TCP_LISTEN_PORT	23   // Telent port number
#define RX_BUFSIZE (4096)

//----- Global Vars -----
char RXBuffer[RX_BUFSIZE];

/*-------------------------------------------------------------------
 Convert IP address to a string
 -------------------------------------------------------------------*/
void IPtoString(IPADDR  ia, char *s)
{
    PBYTE ipb= (PBYTE)&ia;
    siprintf(s, "%d.%d.%d.%d",(int)ipb[0],(int)ipb[1],(int)ipb[2],(int)ipb[3]);
}

// Allocate task stack for UDP listen task
DWORD   TcpServerTaskStack[USER_TASK_STK_SIZE];

/*-------------------------------------------------------------------
 TCP Server Task
 -------------------------------------------------------------------*/
void TcpServerTask(void * pd)
{
    int ListenPort = (int) pd;

    // Set up the listening TCP socket
    int fdListen = listen(INADDR_ANY, ListenPort, 5);

    if (fdListen > 0)
    {
        IPADDR	client_addr;
        WORD	port;

        while(1)
        {
            // The accept() function will block until a TCP client requests
            // a connection. Once a client connection is accepting, the
            // file descriptor fdnet is used to read/write to it.
            iprintf( "Wainting for connection on port %d...\n", ListenPort );
            int fdnet = accept(fdListen, &client_addr, &port, 0);

            iprintf("Connected to: ");
            ShowIP(client_addr);
            iprintf(":%d\n", port);

            writestring(fdnet, "Welcome to the NetBurner TCP Server\r\n");
            char s[20];
            IPtoString(EthernetIP, s);
            siprintf(RXBuffer, "You are connected to IP Address %s, port %d\r\n",
                     s, TCP_LISTEN_PORT);
            writestring(fdnet, RXBuffer);

            while (fdnet > 0)
            {
                /* Loop while connection is valid. The read() function will return
                   0 or a negative number if the client closes the connection, so we
                   test the return value in the loop. Note: you can also use
                   ReadWithTimout() in place of read to enable the connection to
                   terminate after a period of inactivity.
                */
                int n = 0;
                do
                {
                    n = read( fdnet, RXBuffer, RX_BUFSIZE );
                    RXBuffer[n] = '\0';
                    iprintf( "Read %d bytes: %s\n", n, RXBuffer );
                }
                while ( n > 0 );

                // Don't foreget to close !
                iprintf("Closing client connection: ");
                ShowIP(client_addr);
                iprintf(":%d\n", port);
                close(fdnet);
                fdnet = 0;
            }
        } // while(1)
    } // while listen
}


/*-------------------------------------------------------------------
 User Main
 ------------------------------------------------------------------*/
extern "C" void UserMain(void * pd)
{
    init();

    // Create TCP Server task
    OSTaskCreate( TcpServerTask,
                  (void  *)TCP_LISTEN_PORT,
                  &TcpServerTaskStack[USER_TASK_STK_SIZE] ,
                  TcpServerTaskStack,
                  MAIN_PRIO - 1);	// higher priority than UserMain

    while (1)
    {
        OSTimeDly( TICKS_PER_SECOND * 5 );
    }
}
I used putty to telnet in and it's all working great...
However, when I leave the current connection open and tried another connection with putty, MOD5270 sends [SYN, ACK] to client side. I was able to open more than 10 connections with no problem and I confirmed thru wireshark that MOD5270 sends [SYN, ACK] messages.

I don't want to allow any other connections. Can anyone help me with this?
User avatar
dciliske
Posts: 624
Joined: Mon Feb 06, 2012 9:37 am
Location: San Diego, CA
Contact:

Re: Single TCP Socket Connection

Post by dciliske »

Uh... why? Also, when you say you opened 10 more corrections, was this 10 concurrent connections or did you close them in between?

-Dan
Dan Ciliske
Project Engineer
Netburner, Inc
sulliwk06
Posts: 118
Joined: Tue Sep 17, 2013 7:14 am

Re: Single TCP Socket Connection

Post by sulliwk06 »

Couldn't you just close the listen fd until you are ready to allow another?
User avatar
pbreed
Posts: 1088
Joined: Thu Apr 24, 2008 3:58 pm

Re: Single TCP Socket Connection

Post by pbreed »

Just close the listening socket.....
Be careful because TCP does not have keep alive so you will not ever know of the one connected TCP socket is connected to a dead machine and
thus you have stopped listening because you have a connection.... only you really don't have a valid connection anymore...

To fix this issue you need to use keepalive

there is an example

nburn\examples\standardstack\tcp\tcp_simple_keepalive
tigger
Posts: 5
Joined: Wed Jun 01, 2016 8:22 am

Re: Single TCP Socket Connection

Post by tigger »

Solved it. Thank you all!
Post Reply