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

Annotation of OpenXM/src/ox_math/mysocket.c, Revision 1.2

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
                      2: /* $OpenXM$ */
                      3: /* $Id: mysocket.c,v 1.1 1999/09/30 08:25:12 ohara Exp $ */
                      4: /*
                      5: setsockopt()してオプションを設定後,
                      6: socket, bind
                      7: するとローカルポートが割り当てられる。
                      8: その後, getsockname によってポート番号が求まる.
                      9: */
                     10:
                     11: #include <stdio.h>
                     12: #include <stdlib.h>
                     13: #include <unistd.h>
                     14: #include <errno.h>
                     15: #include <netdb.h>
                     16: #include <netinet/in.h>
                     17: #include <sys/types.h>
                     18: #include <sys/socket.h>
                     19: #include <fcntl.h>
                     20:
                     21: #if defined(__sun__)
                     22: #include <arpa/inet.h>
                     23: #endif
                     24:
                     25: #include "mysocket.h"
                     26:
                     27: static int getsocket(struct sockaddr_in *mp, char *host, short port)
                     28: {
                     29:     struct hostent *ent = gethostbyname(host);
                     30:
                     31:     memset(mp, '\0', sizeof(struct sockaddr_in));
                     32:     mp->sin_family = AF_INET;
                     33:     mp->sin_port = htons(port);
                     34:     memcpy((char *)&mp->sin_addr, ent->h_addr, ent->h_length);
                     35:
                     36:     return socket(AF_INET, SOCK_STREAM, 0);
                     37: }
                     38:
                     39: int mysocketAccept(int s_waiting)
                     40: {
                     41:     int val = accept(s_waiting, NULL, NULL);
                     42:     return val;
                     43: }
                     44:
                     45: int mysocketListen(char *hostname, short *portp)
                     46: {
                     47:     int option;
                     48:     int tmp;
                     49:     struct sockaddr_in me;
                     50:
                     51:     int s_waiting = getsocket(&me, hostname, *portp);
                     52:
                     53:     option = 1;
                     54:     setsockopt(s_waiting, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
                     55:
                     56:     if (bind(s_waiting, (struct sockaddr *)&me, sizeof(me)) < 0) {
                     57:         fprintf(stderr, "bind: failed.\n");
                     58:         exit(1);
                     59:     }
                     60:
                     61:     tmp = sizeof(me);
                     62:     if (getsockname(s_waiting, (struct sockaddr *)&me, &tmp) < 0) {
                     63:         fprintf(stderr, "getsockname is failed.\n");
                     64:         exit(1);
                     65:     }
                     66:
                     67:     *portp = ntohs(me.sin_port);
                     68:
                     69:     if (listen(s_waiting, 1) < 0) {
                     70:         fprintf(stderr, "listen: failed.\n");
                     71:         exit(1);
                     72:     }
                     73:
                     74:     return s_waiting;
                     75: }
                     76:
                     77: int mysocketOpen(char* hostname, short port)
                     78: {
                     79:     struct sockaddr_in serv;
                     80:     int s = getsocket(&serv, hostname, port);
                     81:
                     82:     fprintf(stderr, "get socket address for port number %d.\n", port);
                     83:     if (connect(s, (struct sockaddr *)&serv, sizeof(serv)) != 0) {
                     84:         fprintf(stderr, "connect: fail! socket = %d, errno = %d\n", s, errno);
                     85:         exit(-1);
                     86:     }
                     87:     return s;
                     88: }
                     89:
                     90: int mypipe(char *program, int fd1, int fd2)
                     91: {
                     92:     int sockfd[2];
                     93:     if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd) < 0) {
                     94:         fprintf(stderr, "socketpair: fail! errno = %d\n", errno);
                     95:     }
                     96:     if (fork() == 0) {
                     97:         /* 子プロセス */
                     98:         close(sockfd[0]);
                     99:         dup2(sockfd[1], fd1);
                    100:         dup2(sockfd[1], fd2);
                    101:         execl(program, program, NULL);
                    102:   }
                    103:   /* 親プロセス */
                    104:     close(sockfd[1]);
                    105:     return sockfd[0];
                    106: }

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