关闭

TCP服务端和客户端程序示例

标签: tcp服务端客户端
607人阅读 评论(0) 收藏 举报
分类:

服务端:

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#define BUFFER_SIZE 1024

int main( int argc, char* argv[] )
{
    if( argc <= 2)
    {
        printf( "usage: %s ip_address port_number \n", basename( argv[0] ) );
        return 1;
    }
    const char* ip = argv[1];
    int port = atoi( argv[2] );

    int sock_fd, conn_fd;
    int nbytes = 0;
    char buf[BUFFER_SIZE];
    struct sockaddr_in server_addr;
    
    bzero( &server_addr, sizeof( server_addr ) ); 
    server_addr.sin_family = AF_INET;
    inet_pton( AF_INET, ip, &server_addr.sin_addr );
    server_addr.sin_port = htons( port );
    
    sock_fd = socket( PF_INET, SOCK_STREAM, 0 );
    assert( sock_fd >= 0 );
    int ret = bind( sock_fd, ( struct sockaddr* )&server_addr, sizeof( server_addr ) );
    assert( ret != 1 );

    ret = listen ( sock_fd, 5 );
    assert( ret != -1 );

    struct sockaddr_in client_addr;
    socklen_t client_addrlength = sizeof( client_addr );
    while( 1 )
    {
        memset ( buf, '\0', BUFFER_SIZE );
        conn_fd = accept( sock_fd, ( struct sockaddr* )&client_addr, &client_addrlength );
        if( conn_fd < 0 )
        {
            printf( "accept failed, errno is: %d\n", errno );
            break;
        }
        printf( "Connect successed: %s\n", inet_ntoa( client_addr.sin_addr ) );
        if( (nbytes = recv( conn_fd, buf, BUFFER_SIZE-1, 0 )) == -1 )
        {
            printf( "recv failed, errno is: %d\n", errno );
            break;
        }
        printf( "Server received: %s\n", buf );
        close( conn_fd );
    }
    close( sock_fd );
    return 0;
}


客户端:

#include <sys/socket.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#define BUFFER_SIZE 512

int main( int argc, char* argv[] )
{
    if( argc <= 2 )
    {
        printf( "usage: %s ip_adderss port_number \n", basename( argv[0] ) );
        return 1;
    }

    const char* ip = argv[1];
    int port = atoi( argv[2] );

    char buf[BUFFER_SIZE];

    struct sockaddr_in server_addr;
    bzero( &server_addr, sizeof( server_addr ) );
    server_addr.sin_family = AF_INET;
    inet_pton( AF_INET, ip, &server_addr.sin_addr );
    server_addr.sin_port = htons( port );

    int sock_fd = socket( PF_INET, SOCK_STREAM, 0 );
    assert( sock_fd >= 0 );

    if( connect( sock_fd, (struct sockaddr* )&server_addr, sizeof( server_addr ) ) == -1 )
    {
        printf("cannot connect the server: %s\n", inet_ntoa(server_addr.sin_addr) );
        close( sock_fd );
        return -1;
    }
    memset( buf, '\0', BUFFER_SIZE );
    printf( "Please input message: \n" );
    fgets(buf, BUFFER_SIZE, stdin);
    send(sock_fd, buf, BUFFER_SIZE, 0 );

    close( sock_fd );
    return 0;
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:64123次
    • 积分:1225
    • 等级:
    • 排名:千里之外
    • 原创:50篇
    • 转载:39篇
    • 译文:1篇
    • 评论:5条
    最新评论