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 socket problem : connect and bind


Hello, I have the code folowing , it works fine under a cygwin
environement, it compiles fine with the ecos tools and I load it fine
on my viper card.

after a 'load -v -m ymodem'
I run: 'go'
and I have an error of connect() and of bind().

Furthermore I can ping the viper card thanks to my computer, but the
command 'ping -n 3 -h 1.1.1.2' doesn't succeed on the redboot
environement (where 1.1.1.2 is the ip of my computer), my computer is
linked with a ethernet cable to the card directly.

=> is there some functions to call before using the network ?
in my repository I have an ecos environnement build arround the 'net'
package.


**
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define SOCKET_ERROR -1

int OpenPort(int port)
{
struct sockaddr_in sin;
int sock;
unsigned char val=1;

    if((sock=socket(AF_INET,SOCK_STREAM,0)) == SOCKET_ERROR) return 0;
    sin.sin_port=htons((short)port);
    sin.sin_addr.s_addr=0;
    sin.sin_family=AF_INET;
    //Allow the socket to be bound to an address which is already in
use
//   
if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&val,sizeof(val))==SOCKET_E
RROR) return 0;
// (By default, a socket cannot be bound  to a local address which is
already in use.)

    if(bind(sock,(struct sockaddr *)&sin,sizeof(sin))==SOCKET_ERROR)
return 0;

    if(listen(sock,SOMAXCONN)==SOCKET_ERROR) return 0;

    return sock;
}

int connecta(short port)
{
struct sockaddr_in sin;
int sock;

    if((sock=socket(AF_INET,SOCK_STREAM,0)) == SOCKET_ERROR) return 0;
    sin.sin_port=htons((short)port);
    sin.sin_addr.s_addr = inet_addr("147.215.40.139");
    sin.sin_family=AF_INET;
    //Allow the socket to be bound to an address which is already in
use
//   
if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&val,sizeof(val))==SOCKET_E
RROR) return 0;
// (By default, a socket cannot be bound  to a local address which is
already in use.)

   // if(bind(sock,(struct sockaddr *)&sin,sizeof(sin))==SOCKET_ERROR)
return 0;

    connect(sock, (struct sockaddr *)&sin, sizeof(sin));
    //if(listen(sock,SOMAXCONN)==SOCKET_ERROR) return 0;

    return sock;
}

int main()
{
    int sock;
    int sock_client;
    
    connecta(90);
    
    sock = OpenPort(90);
    sock_client = accept(sock, NULL, 0);
    printf("client connected");
    return 0;
    
    
    
}
**eof

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]