This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

TCP: accept fails whereas connect succesful


Hello,
I am still on my TCP Server,
I managed to specify a static IP address and now I am able to connect
from my arcom viper card to my PC and send to my pc data via TCP
sockets.
However my final issue is to build a server on my viper card.

My function OpenPort return with 1 (succesful) and when I attempt to
connect (whith the correct ip and correct port) with a telnet
<viper_ip>  <port_opened> , I can see SYN request from my PC without
any ack from the viper.
Furthermore, it doesn't reply to arp request (ie for the moment I have
to set a arp -s ip mac to be able to send packets to the viper .. but
the viper announce itself (once)just after the go command )

I built my project with the eCos configuration Tool, selected the
'net' package, checked and filled the 'address setups for eth0' and
unchecked the DNS support (because only work whit ip addresses).

Here is the output of redBoot, lauching my eCos program

----------------------------------------------------------------

RedBoot>
RedBoot> go
[cyg_net_init] Init: mbinit(0x00000000)
[cyg_net_init] Init: cyg_net_init_devs(0x00000000)
Init device 'lan91cxx_eth0'
[cyg_net_init] Init: loopattach(0x00000000)
[cyg_net_init] Init: ifinit(0x00000000)
[cyg_net_init] Init: domaininit(0x00000000)
[cyg_net_init] Init: cyg_net_add_domain(0x0043d774)
New domain internet at 0x00000000
[cyg_net_init] Init: cyg_net_add_domain(0x0043d1fc)
New domain route at 0x00000000
[cyg_net_init] Init: call_route_init(0x00000000)
[cyg_net_init] Done
BOOTP[eth0] op: REPLY
       htype: Ethernet
        hlen: 6
        hops: 0
         xid: 0x0
        secs: 0
       flags: 0x0
       hw_addr: 00:80:66:10:14:ee
     client IP: 147.215.40.77
         my IP: 147.215.40.77
     server IP: 147.215.40.2
    gateway IP: 147.215.40.1
  options:
        subnet mask: 255.255.255.0
       IP broadcast: 147.215.40.255
            gateway: 147.215.40.1
[eth_drv_ioctl] Warning: Driver can't set multi-cast mode
[eth_drv_ioctl] Warning: Driver can't set multi-cast mode
[eth_drv_ioctl] Warning: Driver can't set multi-cast mode
Server Redboot !
Opening port 105..ok

----------------------------------------------------------------
is the fact that I have the message:
[eth_drv_ioctl] Warning: Driver can't set multi-cast mode
is normal ?

the code blocks at "Opening port 105..ok" :at the call of accept(),
but I doesn't handle any connections. nor reply to any arp reply ..
WHY ?
 



Here is the source code:

----------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <network.h>
#include <arpa/inet.h>

#define SOCKET_ERROR -1
#define DESIRED_BACKGROUND_LOAD 50

int OpenPort(unsigned short port)
{
    struct sockaddr_in sin;
    int sock;

    if((sock = socket(AF_INET, SOCK_STREAM, 0))== SOCKET_ERROR)
    {
        printf("error in OpenPort, socket()");
        return 0;
    }

    sin.sin_port = htons(port);
    sin.sin_addr.s_addr = 0;
//    sin.sin_addr.s_addr = INADDR_ANY;
    sin.sin_family = AF_INET;
    sin.sin_len    = sizeof(sin);
        
    if(SOCKET_ERROR == bind(sock, (struct sockaddr *)&sin,
sizeof(struct sockaddr_in) ))
    {
        printf("error in OpenPort, bind()");
        return 0;
    }

    if(SOCKET_ERROR == listen(sock, SOMAXCONN))
    {
        printf("error in OpenPort, listen()");
        return 0;
    }

    return sock;

}




int Connexion(unsigned short port)
{
    struct sockaddr_in sin;
    int sock;

    if((sock = socket(AF_INET, SOCK_STREAM, 0))== SOCKET_ERROR)
    {
        printf("error in OpenPort, socket()\n");
        return 0;
    }

    sin.sin_port = htons(port);
    sin.sin_addr.s_addr = inet_addr("147.215.40.139");
    sin.sin_family = AF_INET;
  
    if(SOCKET_ERROR == connect(sock, (struct sockaddr *)&sin,
sizeof(struct sockaddr_in) ))
    {
        printf("error in Connexion, connect()\n");
        return 0;
    }
    return sock;

}



// envoi les données pointées par data taille = len
// tout est envoyé grace à un appel à recv dans le while
// retourne le nombre d'octets envoyés
int SendData(int sock, char *data, int len)
{
    int total = 0;
    int ret;
    
    while((ret = send(sock, data, len, 0))>0) // envoi les données
    {
        total += ret;  // octets envoyés au total
        data  += ret;  // pointe plus loin sur les données non
envoyées
        len   -= ret;  // la taille restante diminue
    }
    
    return total;  // nombre d'octets envoyés
}

// envoie la chaine de caractère ascii, terminée par un 0.
// retourne le nombre d'octets envoyés (n'envoie pas le 0)
int SendString(int sock, char *str)
{
   return SendData(sock, str, strlen(str));
}

// recoit des données, les stocke dans buffer
// la réception s'achève oubien quand len octets ont été recus
// ou bien quand \n est recu.
// les caractères \r et \n sont enlevés de la fin du buffer avant de
retourner
// le nombre de caractères mis dans buffer est retourné.
int RecvLine(int sock, char *buffer, int len)
{
    char c;
    int total = 0;
    int ret;
    
    memset(buffer, 0 , len);
    
    while( (total<len) && ((ret = recv(sock, &c, 1, 0))>0) )
    {
        memcpy(&buffer[total], &c, ret ); // buffer[total] = c;
        total += ret; // total++;
        if(buffer[total-1] =='\n')
            break ; // sortie de la boucle si retour chariot recu.
    
    }
    
    len = strlen(buffer);
    while((buffer[len-1] =='\n') || (buffer[len-1] =='\r'))
    {
        buffer[len-1] = 0; // écrase \n ou \r par caractère de fin de
chaine
        len--;
        if(!len)   // si taille nulle
            break; // termine la boucle
    }
    
    return len;
    
}

//void cyg_user_start(void)
int main(void)
{
//int i;
int sock, client;
char buffer[1024];

    init_all_network_interfaces();
//    calibrate_load(DESIRED_BACKGROUND_LOAD);
    printf("Server Redboot !\n");
    
    /*
    if(sock = Connexion(90))
    {
        //(commented but this piece works fine)
        printf("Connected ... ");
        SendString(sock, "Hello Server, I am the Viper !!\r\n");
        SendString(sock, "quit\r\n");
        close(sock);
        printf("Disconnected!\r\n");
        
        
    }*/
        
        
    printf("Opening port 105..");
    if(!(sock = OpenPort(105)))
    {
        printf("error opening port !\n");
        return 0;
    }
    printf("ok\n");
    
    
    if(SOCKET_ERROR == (client = accept(sock, NULL, 0)))
    {
        printf("error of accept!");
        return 0;
    }
    printf("Client connected!");
    strcpy(buffer, "Hello you are on the Viper Server!\r\n");
    SendString(client, buffer);

    while(strncmp(buffer, "quit", 3))
    {
        RecvLine(client, buffer, 1024);
        printf("client sent: %s", buffer);
        SendString(client, "220 ok for");
        SendString(client, buffer);
        SendString(client, "\r\n");
    }    
    
    close(sock);
    close(client);

    puts("Server finished");
    return 0;
  
}
----------------------------------------------------------------

the connexion works fine but the open port blocks at accept and
doesn't seem to handle client connections.


Thank you
Thierry

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]