[En-Nut-Discussion] TCP server

Valentin Dodov valdodov at gmail.com
Mon Jul 18 13:41:20 CEST 2011


Hello,

I'm trying to create and test simple TCP server.

My program starts one thread which listens for incoming connections on
port 21 and one thread which connects to port 21.

When second thread tries to creates socket there are 2 sockets and the
first one is allays in  SYNSENT state:

                                 Socket
                                  Typ
                                 Local
                                 Remote
                                 State
                                0x202b64
                                  TCP
                             10.0.0.2:4633
                              10.0.0.2:21
                                SYNSENT
                                0x203144
                                  TCP
                               0.0.0.0:21
                               0.0.0.0:0
                                 LISTEN

and the connection never occurs.

Can someone help me?

This is my code:

#define MYMAC   0x00, 0x0A, 0x59, 0x02, 0x00, 0x00 /* free MAC address
from Web51 project */
#define MYIP    "10.0.0.2"
#define MYMASK  "255.255.0.0"
#define MYGATEWAY  "10.0.0.1"

u_char mac[] = { MYMAC };
THREAD (telnet_listener, arg){
    TCPSOCKET *sock;
    FILE *stream;
    for(;;){
        if ((sock = NutTcpCreateSocket()) == 0)
            puts ("Error creating listening socket");
        else
            puts ("Waiting for client to connect...");
        NutTcpAccept (sock, 21);
        puts ("Client connected\n");
        stream = _fdopen((int) sock, "r+w");
        fclose (stream);
        NutTcpCloseSocket (sock);
        printf ("Client disonnected\n");
    }
}
THREAD (telnet_client, arg){
    TCPSOCKET *socket;
    static char buff[128];
    int got=0;
    for (;;){
        if ((socket = NutTcpCreateSocket()) == 0)
                puts ("Error creating socket");
        if (NutTcpConnect(socket, confnet.cdn_ip_addr, 21)) {
            puts("Error while connecting to server!\n");
        }else {
            for (;;) {
                puts("Receiving...");
                got = NutTcpReceive(socket, buff, sizeof(buff));
                if (got <= 0) {
                    puts("broken");
                    break;
                } else {
                    printf("%d bytes\n", got);
                }
            }
        }
        NutTcpCloseSocket (socket);
    }
}

void printSockets(void){
    extern TCPSOCKET *tcpSocketList;
    TCPSOCKET *ts;
    prog_char *st_P;
     for (ts = tcpSocketList; ts; ts = ts->so_next) {
        switch (ts->so_state) {
            case TCPS_LISTEN:
            st_P = "listen";
            break;
            case TCPS_SYN_SENT:
            st_P = "synsent";
            break;
            case TCPS_SYN_RECEIVED:
            st_P = "synrcvd";
            break;
            case TCPS_ESTABLISHED:
            st_P = "estab";
            break;
            case TCPS_FIN_WAIT_1:
            st_P = "finwait1";
            break;
            case TCPS_FIN_WAIT_2:
            st_P = "finwait2";
            break;
            case TCPS_CLOSE_WAIT:
            st_P = "closewait";
            break;
            case TCPS_CLOSING:
            st_P = "closing";
            break;
            case TCPS_LAST_ACK:
            st_P = "lastack";
            break;
            case TCPS_TIME_WAIT:
            st_P = "timewait";
            break;
            case TCPS_CLOSED:
            st_P = "closed";
            break;
            default:
            st_P = "unknown";
            break;
        }
        printf("%04X TCP Local=%s:%u ", (u_int) ts,
inet_ntoa(ts->so_local_addr), ntohs(ts->so_local_port));
        printf("Remote=%s:%u ", inet_ntoa(ts->so_remote_addr),
ntohs(ts->so_remote_port));
        printf("STATE=%s\n",st_P);
     }
}

int main(void){    
    NutRegisterDevice(&DEV_DEBUG, 0, 0);
    freopen(DEV_DEBUG_NAME, "w", stdout);
    if (NutRegisterDevice(&DEV_ETHER, 0x8300, 5))
        printf("Registering device failed");
    memcpy(confnet.cdn_mac, mac, sizeof(confnet.cdn_mac));
    memcpy(confnet.cd_name, "eth0", sizeof(confnet.cd_name));
    confnet.cdn_ip_addr = inet_addr(MYIP);
    confnet.cdn_cip_addr = inet_addr(MYIP);
    confnet.cdn_ip_mask = inet_addr(MYMASK);
    confnet.cdn_gateway = inet_addr(MYGATEWAY);
    NutNetIfConfig("eth0", confnet.cdn_mac, confnet.cdn_ip_addr,
confnet.cdn_ip_mask);
    if (confnet.cdn_ip_addr && confnet.cdn_gateway) {

NutIpRouteAdd(inet_addr("10.0.0.0"),inet_addr("255.255.0.0"),inet_addr("0.0.0.0"),&DEV_ETHER);
        NutIpRouteAdd(0, 0, confnet.cdn_gateway, &DEV_ETHER);
    } 
    NutThreadCreate ("telnet_listener", telnet_listener, 0, 256);
    NutThreadCreate ("telnet_client",telnet_client,0,512);
    for (;;)
    {
        NutDumpSocketList(stdout);
        NutSleep (3000);
    }        
}



More information about the En-Nut-Discussion mailing list