TCP_server;TCP_client;

套接字编程 --TCP_server

/*========================================================
*	> 文 件 名 称: tcp_server.c
*	> 创  建  者: 清风徐来 
*	> 邮    箱: zhangsl_cd@hqyj.com
*	> 创 建 时 间: 2020年09月02日 星期三 15时33分46秒
==========================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define N 100
int main()
{
    int fd;
    if(-1 == (fd = socket(AF_INET, SOCK_STREAM, 0))){  //SOCK_STREAM:tcp
        perror("socket:");
        return -1;
    }

    /*man 7 ip*/
    struct sockaddr_in sddr, cddr;
    int len = sizeof sddr; 
    bzero(&cddr, len);
    sddr.sin_family      = AF_INET;
    sddr.sin_port        = htons(8888);
    sddr.sin_addr.s_addr = inet_addr("0.0.0.0");

    /*绑定本机地址和端口*/
    if(-1 == bind(fd, (void *)&sddr, len)){
        perror("bind");
        return -1;
    }

    /*监听*/
    if(-1 == listen(fd, 10)){
        perror("listen:");
        return -1;
    }
    printf("listen...
");

    /*建立新连接*/
    int newfd = accept(fd, (void *)&cddr, &len);
    if(-1 == newfd){
        perror("accept:");
        return -1;
    }
    printf("client> IP:%s PORT:%d connect...
", inet_ntoa(cddr.sin_addr), ntohs(cddr.sin_port));
    
    char buf[N] = {0};
    int ret;
    while(1){
        ret = read(newfd, buf, N-1);
        if(0 >= ret){
            close(newfd);
            break;
        }
        if(0 == strncmp(buf, "quit", 4)){
            close(newfd);
            break;
        }
        printf("recv> %s", buf);

        printf("send> ");
        fgets(buf, N, stdin);
        write(newfd, buf, N-1);
    }
    close(newfd);
    close(fd);
}

套接字编程 --TCP_client

/*========================================================
*	> 文 件 名 称: tcp_server.c
*	> 创  建  者: 清风徐来 
*	> 邮    箱: zhangsl_cd@hqyj.com
*	> 创 建 时 间: 2020年09月02日 星期三 15时33分46秒
==========================================================*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/ip.h>
#define N 100
int main(int argc, char *argv[])
{
    if(3 > argc){
        printf("Usage: %s <IP> <PORT>
", argv[0]);
        return -1;
    }
    int fd;
    if(-1 == (fd = socket(AF_INET, SOCK_STREAM, 0))){
        perror("socket:");
        return -1;
    }

    struct sockaddr_in sddr;
    int len = sizeof sddr; 
    bzero(&sddr, len);
    sddr.sin_family      = AF_INET;
    sddr.sin_port        = htons(atoi(argv[2]));
    sddr.sin_addr.s_addr = inet_addr(argv[1]);

    if(-1 == connect(fd, (void *)&sddr, len)){
        perror("connect:");
        return -1;
    } 
    char buf[N] = {0};
    int ret;
    while(1){
        printf("send> ");
        fgets(buf, N, stdin);
        write(fd, buf, N-1);
        if(0 == strncmp(buf, "quit", 4)){
            close(fd);
            break;
        }

        ret = read(fd, buf, N-1);
        if(0 >= ret){
            close(fd);
            break;
        }
        if(0 == strncmp(buf, "quit", 4)){
            close(fd);
            break;
        }
        printf("recv> %s", buf);
    }
    close(fd);
}
原文地址:https://www.cnblogs.com/thgpddl/p/13605936.html