[BACK]Return to mysocket.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_toolkit

Annotation of OpenXM/src/ox_toolkit/mysocket.c, Revision 1.3

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.3     ! ohara       2: /* $OpenXM: OpenXM/src/ox_toolkit/mysocket.c,v 1.2 2000/01/05 06:05:35 ohara Exp $ */
1.1       ohara       3: /*
1.3     ! ohara       4: Q: How to get a local port number?
        !             5: A: You do setsockopt() to set options and do socket(), bind().
        !             6: An OS set a local port for you.
        !             7: In order to get the local port, you need to do getsockname().
        !             8: (See [1] pp. 91, pp. 187 for detail)
        !             9:
        !            10: Reference
        !            11: [1] W. Richard Stevens, "UNIX Network Programming", 2nd ed. Vol. 1
1.1       ohara      12: */
                     13:
                     14: #include <stdio.h>
                     15: #include <stdlib.h>
                     16: #include <unistd.h>
                     17: #include <errno.h>
                     18: #include <netdb.h>
                     19: #include <netinet/in.h>
                     20: #include <sys/types.h>
                     21: #include <sys/socket.h>
                     22: #include <fcntl.h>
                     23:
                     24: #if defined(__sun__)
                     25: #include <arpa/inet.h>
                     26: #endif
                     27:
                     28: #include "mysocket.h"
                     29:
                     30: static int getsocket(struct sockaddr_in *mp, char *host, short port)
                     31: {
                     32:     struct hostent *ent = gethostbyname(host);
                     33:
                     34:     memset(mp, '\0', sizeof(struct sockaddr_in));
                     35:     mp->sin_family = AF_INET;
                     36:     mp->sin_port = htons(port);
                     37:     memcpy((char *)&mp->sin_addr, ent->h_addr, ent->h_length);
                     38:
                     39:     return socket(AF_INET, SOCK_STREAM, 0);
                     40: }
                     41:
                     42: int mysocketAccept(int s_waiting)
                     43: {
                     44:     int val = accept(s_waiting, NULL, NULL);
                     45:     return val;
                     46: }
                     47:
                     48: int mysocketListen(char *hostname, short *portp)
                     49: {
                     50:     int option;
                     51:     int tmp;
                     52:     struct sockaddr_in me;
                     53:
                     54:     int s_waiting = getsocket(&me, hostname, *portp);
                     55:
                     56:     option = 1;
                     57:     setsockopt(s_waiting, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
                     58:
                     59:     if (bind(s_waiting, (struct sockaddr *)&me, sizeof(me)) < 0) {
                     60:         fprintf(stderr, "bind: failed.\n");
                     61:         exit(1);
                     62:     }
                     63:
                     64:     tmp = sizeof(me);
                     65:     if (getsockname(s_waiting, (struct sockaddr *)&me, &tmp) < 0) {
                     66:         fprintf(stderr, "getsockname is failed.\n");
                     67:         exit(1);
                     68:     }
                     69:
                     70:     *portp = ntohs(me.sin_port);
                     71:
                     72:     if (listen(s_waiting, 1) < 0) {
                     73:         fprintf(stderr, "listen: failed.\n");
                     74:         exit(1);
                     75:     }
                     76:
                     77:     return s_waiting;
                     78: }
                     79:
                     80: int mysocketOpen(char* hostname, short port)
                     81: {
                     82:     struct sockaddr_in serv;
                     83:     int s = getsocket(&serv, hostname, port);
                     84:
                     85:     fprintf(stderr, "get socket address for port number %d.\n", port);
                     86:     if (connect(s, (struct sockaddr *)&serv, sizeof(serv)) != 0) {
                     87:         fprintf(stderr, "connect: fail! socket = %d, errno = %d\n", s, errno);
                     88:         exit(-1);
                     89:     }
                     90:     return s;
                     91: }
                     92:
                     93: #if 0
                     94: int mypipe(char *program, int fd1, int fd2)
                     95: {
                     96:     int sockfd[2];
                     97:     if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd) < 0) {
                     98:         fprintf(stderr, "socketpair: fail! errno = %d\n", errno);
                     99:     }
                    100:     if (fork() == 0) {
1.2       ohara     101:         /* child process */
1.1       ohara     102:         close(sockfd[0]);
                    103:         dup2(sockfd[1], fd1);
                    104:         dup2(sockfd[1], fd2);
                    105:         execl(program, program, NULL);
                    106:   }
1.2       ohara     107:   /* parent process */
1.1       ohara     108:     close(sockfd[1]);
                    109:     return sockfd[0];
                    110: }
                    111: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>