Annotation of OpenXM/src/ox_toolkit/mysocket.c, Revision 1.4
1.1 ohara 1: /* -*- mode: C; coding: euc-japan -*- */
1.4 ! ohara 2: /* $OpenXM: OpenXM/src/ox_toolkit/mysocket.c,v 1.3 2000/01/13 07:57:09 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>
1.4 ! ohara 19: #include <string.h>
1.1 ohara 20: #include <netinet/in.h>
21: #include <sys/types.h>
22: #include <sys/socket.h>
23: #include <fcntl.h>
24:
25: #if defined(__sun__)
26: #include <arpa/inet.h>
27: #endif
28:
29: #include "mysocket.h"
30:
31: static int getsocket(struct sockaddr_in *mp, char *host, short port)
32: {
33: struct hostent *ent = gethostbyname(host);
34:
35: memset(mp, '\0', sizeof(struct sockaddr_in));
36: mp->sin_family = AF_INET;
37: mp->sin_port = htons(port);
38: memcpy((char *)&mp->sin_addr, ent->h_addr, ent->h_length);
39:
40: return socket(AF_INET, SOCK_STREAM, 0);
41: }
42:
43: int mysocketAccept(int s_waiting)
44: {
1.4 ! ohara 45: return accept(s_waiting, NULL, NULL);
1.1 ohara 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>