socket通信实例

时间:2021-07-28 15:25:06

ref: http://www.cnblogs.com/xudong-bupt/archive/2013/12/29/3483059.html

http://blog.csdn.net/love_gaohz/article/details/43700411

http://blog.csdn.net/wesleyluo/article/details/6149071

server.cpp

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/time.h>
#define MYPORT  5566
#define QUEUE   20
#define BUFFER_SIZE 1024

int timeval_subtract(struct timeval* result, struct timeval* x, struct timeval* y)
{
    int nsec;

    if ( x->tv_sec>y->tv_sec )
        ;

    if ( (x->tv_sec==y->tv_sec) && (x->tv_usec>y->tv_usec) )
        ;

    result->tv_sec = ( y->tv_sec-x->tv_sec );
    result->tv_usec = ( y->tv_usec-x->tv_usec );

    )
    {
        result->tv_sec--;
        result->tv_usec+=;
    }

    ;
}

int main()
{
    ///定义sockfd
    );

    ///定义sockaddr_in
    struct sockaddr_in server_sockaddr;
    server_sockaddr.sin_family = AF_INET;
    server_sockaddr.sin_port = htons(MYPORT);
    server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);

    ///bind,成功返回0,出错返回-1
    )
    {
        perror("bind");
        exit();
    }

    ///listen,成功返回0,出错返回-1
    )
    {
        perror("listen");
        exit();
    }

    ///客户端套接字
    char buffer[BUFFER_SIZE];
    struct sockaddr_in client_addr;
    socklen_t length = sizeof(client_addr);

    ///成功返回非负描述字,出错返回-1
    int conn = accept(server_sockfd, (struct sockaddr*)&client_addr, &length);
    )
    {
        perror("connect");
        exit();
    }

#if 1
    int flags;
    )
    {
        ;
    }
    )
    {
        ;
    }
#endif
#if 1
    ; //设置为10M
    setsockopt( conn, SOL_SOCKET, SO_RCVBUF, ( const char* )&nRecvBufLen, sizeof( int ) );
#endif

    struct timeval start,stop,diff;
    ;
    )
    {
            )
            {
                gettimeofday(&start,);
                printf( "CAPABILITY_1_SECS_%010u_USEC_%010u\n", start.tv_sec, start.tv_usec );
            }    

            );
            i++;
            )
            {
                gettimeofday(&stop,);
                printf( "CAPABILITY_2_SECS_%010u_USEC_%010u\n", stop.tv_sec, stop.tv_usec );

                timeval_subtract(&diff,&start,&stop);

                printf( "CAPABILITY_3_SECS_%010u_USEC_%010u\n", diff.tv_sec, diff.tv_usec );
            }
    }

    close(conn);
    close(server_sockfd);
    ;
}

client.cpp

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/time.h>
#include <time.h>
#define MYPORT  5566
#define BUFFER_SIZE 1024

int timeval_subtract(struct timeval* result, struct timeval* x, struct timeval* y)
{
    int nsec;

    if ( x->tv_sec>y->tv_sec )
        ;

    if ( (x->tv_sec==y->tv_sec) && (x->tv_usec>y->tv_usec) )
        ;

    result->tv_sec = ( y->tv_sec-x->tv_sec );
    result->tv_usec = ( y->tv_usec-x->tv_usec );

    )
    {
        result->tv_sec--;
        result->tv_usec+=;
    }

    ;
}

int main()
{
    ///定义sockfd
    );

    ///定义sockaddr_in
    struct sockaddr_in servaddr;
    memset(&servaddr, , sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(MYPORT);  ///服务器端口
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");  ///服务器ip

    ///连接服务器,成功返回0,错误返回-1
    )
    {
        perror("connect");
        exit();
    }
#if 1
    int flags;
    )
    {
        ;
    }
    )
    {
        ;
    }
#endif
#if 1
    ; //设置为10M
    setsockopt( sock_cli, SOL_SOCKET, SO_SNDBUF, ( const char* )&nSendBufLen, sizeof( int ) );
#endif
    char sendbuf[BUFFER_SIZE];
    strcpy(sendbuf, "hello server");
    struct timeval start,stop,diff;
    ;
    )
    {
            )
            {
                gettimeofday(&start,);
                printf( "CAPABILITY_1_SECS_%010u_USEC_%010u\n", start.tv_sec, start.tv_usec );
            }

            send(sock_cli, sendbuf, strlen(sendbuf),); ///发送
            i++;
            )
            {
                gettimeofday(&stop,);
                printf( "CAPABILITY_2_SECS_%010u_USEC_%010u\n", stop.tv_sec, stop.tv_usec );

                timeval_subtract(&diff,&start,&stop);

                printf( "CAPABILITY_3_SECS_%010u_USEC_%010u\n", diff.tv_sec, diff.tv_usec );
            }
    }

    close(sock_cli);
    ;
}