// void tcpsocket::connect(const char* host, unsigned short port)
//
// Purpose:
//		Open a connection
//
// Parameters:
//		host				Host name or TCP/IP address separated by dots
//		port				Port number
//
// Exceptions:
//		Throws a tcperror if any socket error occurs.
//
// Comments:
//		This function opens a connection with the specified host. The host may
//		be specified by name, or with an IP address which consists of four
//		numbers separated by dots.
//
void tcpsocket::connect(const char* host, unsigned short port)
{
	// Initialise the address structure
	SOCKADDR_IN addr;
	make_addr(host, port, addr);

	// Open the connection
	if (::connect(m_socket, reinterpret_cast<SOCKADDR*>(&addr), sizeof(addr)))
	{ throw tcperror(); }
}
// void tcpsocket::listen(unsigned short port)
//
// Purpose:
//		Make this socket listen for clients on the specified port
//
// Parameters:
//		port				Port number
//
// Exceptions:
//		Throws a tcperror if any socket error occurs.
//
// Comments:
//		This function puts the socket into listening mode, so that it can be
//		used in a call to accept().
//
void tcpsocket::listen(unsigned short port)
{
	// Initialise the address structure
	SOCKADDR_IN addr;
	make_addr(0, port, addr);

#ifdef	UNIX

	// Unix
	// Socket option enable
	int nSetValue = 1;

	// Reuse socket
	setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (char*) &nSetValue, sizeof(int));
#endif

	// Bind the address
	if (bind(m_socket, reinterpret_cast<SOCKADDR*>(&addr), sizeof(addr)))
	{ throw tcperror(); }

	// Make the socket listen
	if (::listen(m_socket, SOMAXCONN))
	{ throw tcperror(); }
}
Exemplo n.º 3
0
		.gateway = (gway) \
}

/*
 * ADDRESS DEFINITIONS
 * =================== ********************************************************
 */

/* RNet Common */
#define NETMASK IPv4(255, 255, 255, 0)
#define GATEWAY IPv4(10,  10, 10, 1)

/* Flight Computer */
#define FC_IP IPv4(10, 10, 10, 10)
#define FC_LISTEN_PORT 36000 // FC device listener
const struct sockaddr * FC_ADDR = make_addr(FC_IP, FC_LISTEN_PORT);

/* Sensor Node */
#define SENSOR_IP IPv4(10, 10, 10, 20)
#define SENSOR_MAC (uint8_t[6]){0xE6, 0x10, 0x20, 0x30, 0x40, 0x11}
#define ADIS_PORT 35020 // ADIS16405
#define MPU_PORT 35002  // MPU1950
#define MPL_PORT 35010  // MPL3115A2
#define BMP_PORT 35011  // BMP180

struct lwipthread_opts * SENSOR_LWIP = make_lwipopts(SENSOR_MAC, SENSOR_IP, NETMASK, GATEWAY);
const struct sockaddr *ADIS_ADDR = make_addr(SENSOR_IP, ADIS_PORT);
const struct sockaddr *MPU_ADDR = make_addr(SENSOR_IP, MPU_PORT);
const struct sockaddr *MPL_ADDR = make_addr(SENSOR_IP, MPL_PORT);
const struct sockaddr *BMP_ADDR = make_addr(SENSOR_IP, BMP_PORT);
Exemplo n.º 4
0
int make_netsocket(u_short port, char *host, int type)
{
    int s, flag = 1;
    struct sockaddr_in sa;
    struct in_addr *saddr;
    int socket_type;

    /* is this a UDP socket or a TCP socket? */
    socket_type = (type == NETSOCKET_UDP)?SOCK_DGRAM:SOCK_STREAM;

    bzero((void *)&sa,sizeof(struct sockaddr_in));

    if((s = socket(AF_INET,socket_type,0)) < 0)
        return(-1);
    if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(flag)) < 0)
        return(-1);

    saddr = make_addr(host);
    if(saddr == NULL && type != NETSOCKET_UDP)
        return(-1);
    sa.sin_family = AF_INET;
    sa.sin_port = htons(port);

    if(type == NETSOCKET_SERVER)
    {
        /* bind to specific address if specified */
        if(host != NULL)
            sa.sin_addr.s_addr = saddr->s_addr;

        if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0)
        {
            close(s);
            return(-1);
        }
    }
    if(type == NETSOCKET_CLIENT)
    {
        sa.sin_addr.s_addr = saddr->s_addr;
        if(connect(s,(struct sockaddr*)&sa,sizeof sa) < 0)
        {
            close(s);
            return(-1);
        }
    }
    if(type == NETSOCKET_UDP)
    {
        /* bind to all addresses for now */
        if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0)
        {
            close(s);
            return(-1);
        }

        /* if specified, use a default recipient for read/write */
        if(host != NULL && saddr != NULL)
        {
            sa.sin_addr.s_addr = saddr->s_addr;
            if(connect(s,(struct sockaddr*)&sa,sizeof sa) < 0)
            {
                close(s);
                return(-1);
            }
        }
    }


    return(s);
}