version 1.1, 1999/12/09 22:44:55 |
version 1.4, 2000/10/10 05:23:20 |
|
|
/* -*- mode: C; coding: euc-japan -*- */ |
/* -*- mode: C; coding: euc-japan -*- */ |
/* $OpenXM$ */ |
/* $OpenXM: OpenXM/src/ox_toolkit/mysocket.c,v 1.3 2000/01/13 07:57:09 ohara Exp $ */ |
/* |
/* |
setsockopt()してオプションを設定後, |
Q: How to get a local port number? |
socket, bind |
A: You do setsockopt() to set options and do socket(), bind(). |
するとローカルポートが割り当てられる。 |
An OS set a local port for you. |
その後, getsockname によってポート番号が求まる. |
In order to get the local port, you need to do getsockname(). |
|
(See [1] pp. 91, pp. 187 for detail) |
|
|
|
Reference |
|
[1] W. Richard Stevens, "UNIX Network Programming", 2nd ed. Vol. 1 |
*/ |
*/ |
|
|
#include <stdio.h> |
#include <stdio.h> |
|
|
#include <unistd.h> |
#include <unistd.h> |
#include <errno.h> |
#include <errno.h> |
#include <netdb.h> |
#include <netdb.h> |
|
#include <string.h> |
#include <netinet/in.h> |
#include <netinet/in.h> |
#include <sys/types.h> |
#include <sys/types.h> |
#include <sys/socket.h> |
#include <sys/socket.h> |
Line 37 static int getsocket(struct sockaddr_in *mp, char *hos |
|
Line 42 static int getsocket(struct sockaddr_in *mp, char *hos |
|
|
|
int mysocketAccept(int s_waiting) |
int mysocketAccept(int s_waiting) |
{ |
{ |
int val = accept(s_waiting, NULL, NULL); |
return accept(s_waiting, NULL, NULL); |
return val; |
|
} |
} |
|
|
int mysocketListen(char *hostname, short *portp) |
int mysocketListen(char *hostname, short *portp) |
Line 94 int mypipe(char *program, int fd1, int fd2) |
|
Line 98 int mypipe(char *program, int fd1, int fd2) |
|
fprintf(stderr, "socketpair: fail! errno = %d\n", errno); |
fprintf(stderr, "socketpair: fail! errno = %d\n", errno); |
} |
} |
if (fork() == 0) { |
if (fork() == 0) { |
/* 子プロセス */ |
/* child process */ |
close(sockfd[0]); |
close(sockfd[0]); |
dup2(sockfd[1], fd1); |
dup2(sockfd[1], fd1); |
dup2(sockfd[1], fd2); |
dup2(sockfd[1], fd2); |
execl(program, program, NULL); |
execl(program, program, NULL); |
} |
} |
/* 親プロセス */ |
/* parent process */ |
close(sockfd[1]); |
close(sockfd[1]); |
return sockfd[0]; |
return sockfd[0]; |
} |
} |