Ejemplo n.º 1
0
SOCKET sim_connect_sock (int32 ip, int32 port)
{
SOCKET newsock;
struct sockaddr_in name;
int32 sta;

newsock = sim_create_sock ();                           /* create socket */
if (newsock == INVALID_SOCKET)                          /* socket error? */
    return newsock;

name.sin_family = AF_INET;                              /* name socket */
name.sin_port = htons ((unsigned short) port);          /* insert port */
name.sin_addr.s_addr = htonl (ip);                      /* insert addr */

sta = sim_setnonblock (newsock);                        /* set nonblocking */
if (sta == SOCKET_ERROR)                                /* fcntl error? */
    return sim_err_sock (newsock, "fcntl", 1);
sta = connect (newsock, (struct sockaddr *) &name, sizeof (name));
if ((sta == SOCKET_ERROR) && 
    (WSAGetLastError () != WSAEWOULDBLOCK) &&
    (WSAGetLastError () != WSAEINPROGRESS))
    return sim_err_sock (newsock, "connect", 1);

return newsock;                                         /* got it! */
}
Ejemplo n.º 2
0
SOCKET sim_accept_conn (SOCKET master, uint32 *ipaddr)
{
int32 sta, err;
#if defined (macintosh) || defined (__linux) || \
    defined (__APPLE__) || defined (__OpenBSD__)
socklen_t size;
#elif defined (_WIN32) || defined (__EMX__) ||\
     (defined (__ALPHA) && defined (__unix__))
int size;
#else 
size_t size; 
#endif
SOCKET newsock;
struct sockaddr_in clientname;

if (master == 0) return INVALID_SOCKET;                 /* not attached? */
size = sizeof (clientname);
newsock = accept (master, (struct sockaddr *) &clientname, &size);
if (newsock == INVALID_SOCKET) {                        /* error? */
    err = WSAGetLastError ();
    if (err != WSAEWOULDBLOCK)
        printf ("Sockets: accept error %d\n", err);
    return INVALID_SOCKET;
    }
if (ipaddr != NULL) *ipaddr = ntohl (clientname.sin_addr.s_addr);

sta = sim_setnonblock (newsock);                        /* set nonblocking */
if (sta == SOCKET_ERROR)                                /* fcntl error? */
    return sim_err_sock (newsock, "fcntl", 0);
return newsock;
}
Ejemplo n.º 3
0
SOCKET sim_master_sock (int32 port)
{
SOCKET newsock;
struct sockaddr_in name;
int32 sta;

newsock = sim_create_sock ();                           /* create socket */
if (newsock == INVALID_SOCKET)                          /* socket error? */
    return newsock;

name.sin_family = AF_INET;                              /* name socket */
name.sin_port = htons ((unsigned short) port);          /* insert port */
name.sin_addr.s_addr = htonl (INADDR_ANY);              /* insert addr */

sta = bind (newsock, (struct sockaddr *) &name, sizeof (name));
if (sta == SOCKET_ERROR)                                /* bind error? */
    return sim_err_sock (newsock, "bind", 1);
sta = sim_setnonblock (newsock);                        /* set nonblocking */
if (sta == SOCKET_ERROR)                                /* fcntl error? */
    return sim_err_sock (newsock, "fcntl", 1);
sta = listen (newsock, 1);                              /* listen on socket */
if (sta == SOCKET_ERROR)                                /* listen error? */
    return sim_err_sock (newsock, "listen", 1);
return newsock;                                         /* got it! */
}