68user's page 掲示板

Prev< No. 1509> Next  [最新発言に戻る] [過去ログ一覧]
No. 1509 # 68user 2001/01/05 (金) 12:56:19
> 最初の00がうまく行きません。
うまく行かないプログラムを (余計な部分は削った上で)
公開してください。

はい、C+select の超手抜きサンプル。バグありまくりですが
一応動きます。細部は参考にせず、おおまかな流れを見て下さい。

------------------
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <unistd.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define BUF_LEN 256 /* バッファのサイズ */

int main( int argc , char *argv[]){
    int connected_socket[100];
    int listening_socket;
    struct sockaddr_in sin;
    int sock_optval = 1;
    int port = 5000;
    char buf[BUF_LEN];
    struct timeval waitval;
    fd_set fd;
    fd_set org_fd;
    int max_sock = 0;
                                                                /* リスニングソケットを作成 */
    listening_socket = socket(AF_INET,SOCK_STREAM,0);

                                                                /* ソケットオプション設定 */
    if ( setsockopt(listening_socket,SOL_SOCKET,SO_REUSEADDR,
                                    &sock_optval,sizeof(sock_optval)) == -1 ){
        perror("setsockopt");
        exit(1);
    }
                                                                /* アドレスファミリ・ポート番号・IPアドレス設定 */
    sin.sin_family = AF_INET;
    sin.sin_port = htons(port);
    sin.sin_addr.s_addr = htonl(INADDR_ANY);

    if ( bind(listening_socket,(struct sockaddr *)&sin,sizeof(sin)) < 0 ){
        perror("bind");
        exit(1);
    }

    if ( listen(listening_socket, SOMAXCONN) == -1 ){
        perror("listen");
        exit(1);
    }
    printf("ポート %d を見張ります。\n",port);

    waitval.tv_sec = 1;
    waitval.tv_usec = 0;

    FD_ZERO(&org_fd);
    FD_SET(listening_socket, &org_fd);
    max_sock = listening_socket;
        
    while (1){
        int i;
        struct hostent *peer_host;
        struct sockaddr_in peer_sin;

        memcpy(&fd, &org_fd, sizeof(org_fd));

        select(max_sock+1, &fd, NULL, NULL, &waitval);

        for ( i=0 ; i<=max_sock ; i++ ){
            if ( FD_ISSET(i, &fd) ){
                if ( i == listening_socket ){
                    int len;
                    len = sizeof sin;
                    max_sock++;
                    printf("connected_socket[%d]\n",max_sock);
                    connected_socket[max_sock] =
                        accept(listening_socket, (struct sockaddr *)&sin, &len);

                    if ( connected_socket[max_sock] == -1 ){
                        perror("accept failed.\n");
                    }
                        
                    len = sizeof(peer_sin);
                    getpeername(connected_socket[max_sock], (struct sockaddr *)&peer_sin,&len);
                        
                    peer_host = gethostbyaddr((char *)&peer_sin.sin_addr.s_addr,
                                                                        sizeof(peer_sin.sin_addr),AF_INET);
                        
                    printf("接続: %s [%s] ポート %d\n",
                                  peer_host->h_name,
                                  inet_ntoa(peer_sin.sin_addr),
                                  ntohs(peer_sin.sin_port)
                                  );
                    FD_SET(max_sock, &org_fd);

                } else {
                    int read_size;
                    read_size = read(connected_socket[i], buf, sizeof(buf)-1);
                    if ( read_size == 0 ){
                        printf("接続が切れました。引き続きポート %d を見張ります。\n",port);
                        close(connected_socket[i]);
                        FD_CLR(i, &org_fd);
                    } else {
                        printf("メッセージ: %s",buf);
                        write(connected_socket[i],buf,strlen(buf));
                    }
                }
            }
        }
    }
    close(listening_socket);
    return 0;
}

Prev< No. 1509> Next  [最新発言に戻る] [過去ログ一覧]