[ECOS] Problem with accept() on i386 pc.

Patrick O'Grady patrick@baymotion.com
Thu Apr 20 22:30:00 GMT 2006

Hi, all--

Curious if anyone else has seen this problem, or if I'm just doing something 
wrong.  If I compile the following program on cygwin, then it works as 
expected: I can 'telnet localhost 7227' and see my server there.  However, 
if I compile this program and run it on my i386 pc with an i82559 ethernet 
adapter, it doesn't work.  My target system is at IP address, and 
I can ping that with no problems.  But if I 'telnet 7227', I get 
response SYN response packets with RST--implying that the port isn't 
listening.  My target is configured using 'ecosconfig new pc net' with eCos 
from CVS as of today (4/20).  I've not tried this program on eCos before; 
but if I undefine ECOS_MODE, then it compiles and works under Cygwin as 
expected.  Thanks for your input!


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define ECOS_MODE

#ifdef ECOS_MODE
#include <network.h>

#define assert(c) \
        if (c) { } else { printf("Assert failed: \"%s\".\n", #c); exit(1); }

int main(void)
    int sockfd, newsockfd, portno, clilen;
    char buffer[256];
    struct sockaddr_in serv_addr, cli_addr;
    int n;

    printf("Starting.\n") ;
#ifdef ECOS_MODE
    init_all_network_interfaces() ;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    assert(sockfd >= 0);
    printf("sockfd=%d.\n", sockfd) ;

    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = 7227 ;
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
    int r = bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) 
    printf("bind returned %d.\n", r) ;
    assert(r >= 0) ;

    r = listen(sockfd, 5);
    printf("listen returned %d.\n", r) ;

    while (1)
        printf("Ready on port %d.\n", portno) ;

        clilen = sizeof(cli_addr);
        bzero((char *) &cli_addr, sizeof(cli_addr));
        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
        printf("Accept newsockfd=%d.\n", newsockfd) ;
        assert(newsockfd >= 0) ;

        n = read(newsockfd,buffer,255);
        printf("Read %d bytes.\n", n);
        assert(n >= 0) ;
        printf("Received: \"%*.*s\"\n", n, n, buffer);

        n = write(newsockfd,"I got your message",18);
        printf("Write wrote %d bytes.\n", n) ;
        assert(n >= 0) ;
        close(newsockfd) ;
    return 0;

Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

More information about the Ecos-discuss mailing list