tcp 一对一双向通信

//serve(服务器)
#include <stdio.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>

int con_fd = -1;

void* client_to_serve(void* arg)
{
    char* c_buf = malloc(100);
    while(1)
    {
        bzero(c_buf,100);

        int recv_ret = recv(con_fd,c_buf,100,0);
        if(0 == recv_ret)
        {
            printf("断开连接\n");
            break;
        }
        printf("client:%s\n",c_buf);

    }


}

int main(int argc,char **argv)
{
    //1.创建套接字
    int sock_fd = socket(AF_INET,SOCK_STREAM,0);
    //2.绑定

    struct sockaddr_in serve_addr, cilent_addr;//
    serve_addr.sin_family = AF_INET;
    serve_addr.sin_port = htons(atoi(argv[2]));//网络字节序-----主机字节序
    serve_addr.sin_addr.s_addr = inet_addr(argv[1]);//网络字节序

    socklen_t serve_len = sizeof(serve_addr);
    socklen_t cilent_len = sizeof(cilent_addr);

    bind(sock_fd, (struct sockaddr *)&serve_addr,serve_len);                                                                   

    //3.监听
    listen(sock_fd,4);

    printf("等待连接...\n");
    con_fd = accept(sock_fd, (struct sockaddr *)&cilent_addr, &cilent_len);//阻塞运行
    printf("连接成功>>>\n");



    //创建线程
    pthread_t tid;

    pthread_create(&tid,NULL,client_to_serve, NULL);



    char *r_buf = malloc(100);
    //5.接收消息
    while(1)
    {
        bzero(r_buf,100);
        fgets(r_buf,100,stdin);
        send(con_fd,r_buf,100,0);

    }

//client(客户端)
#include <stdio.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>

int sock_fd = -1;

void *client_to_serve(void *arg)
{
    char* r_buf = malloc(100);
    while(1)
    {
        bzero(r_buf,100);
        int ret = recv(sock_fd,r_buf,100,0);
        printf("serve:%s\n",r_buf);
        if(0 == ret)
        {
            printf("断开连接\n");                                                                  
            break;
        }
    }

}


int main(int argc,char **argv)
{
    //1.创建套接字
    sock_fd = socket(AF_INET,SOCK_STREAM,0);
    //2.绑定

    struct sockaddr_in serve_addr;
    serve_addr.sin_family = AF_INET;
    serve_addr.sin_port = htons(atoi(argv[2]));//网络字节序-----主机字节序
    serve_addr.sin_addr.s_addr = inet_addr(argv[1]);//网络字节序

    socklen_t serve_len = sizeof(serve_addr);

    bind(sock_fd, (struct sockaddr *)&serve_addr,serve_len);


    //3.发起连接请求
    connect(sock_fd,(struct sockaddr *)&serve_addr,serve_len);

    //
    pthread_t tid;

    pthread_create(&tid,NULL, client_to_serve ,NULL );

    char *buf = malloc(100);

    while(1)
    {
        memset(buf,0,100);
        //4.发消息
        fgets(buf,100,stdin);
        send(sock_fd,buf,100,0);

    }




}
                                                                                                   
                                                                                                   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值