网络编程day2

完成TCP服务器、客户端模型的搭建

server

#include<stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>   
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define ip "192.168.31.223"
#define MSG_ERR(msg) do{perror(msg);\
    fprintf(stderr,"%d\n",__LINE__);\
} while(0)
int main(int argc,const char* argv[])
{   

   int sfd = socket(AF_INET, SOCK_STREAM, 0);
    
    if(sfd < 0)
    {
        MSG_ERR("socket");
        return -1;
    }

    printf("socket created\n");
    struct sockaddr_in addr;
    socklen_t addrlen = sizeof(addr);
    addr.sin_family = AF_INET;
    addr.sin_port = htons(6666);
    addr.sin_addr.s_addr = inet_addr(ip);
    if(bind(sfd , (struct sockaddr *)&addr , addrlen)<0)
    {
        MSG_ERR("bind");
        return -1;
    }
    printf("bind done\n");

    if( listen(sfd,128) < 0)
    {
        MSG_ERR("listen");
        return -1;
    }
    printf("listen done\n");

    struct sockaddr_in clientaddr;
    socklen_t clientaddrlen = sizeof(clientaddr);
    int newfd = accept(sfd,(struct sockaddr *)&clientaddr,&clientaddrlen);
    if(newfd < 0)
    {
        MSG_ERR("accept");
        return -1;
    }
    printf("[%s %d]accept successful\n",inet_ntoa(clientaddr.sin_addr),ntohs(clientaddr.sin_port));
    char buf [1024] = "";
    ssize_t res;
    while (1)
    {   
        bzero(buf, sizeof(buf));
        res = recv(newfd, buf, sizeof(buf), 0);
        if(res < 0)
        {
            MSG_ERR("recv");
            return -1;
        }
        else if(res == 0)
        {
            printf("client unconnected");
            break;
        }
        else
        {
            printf("clients: %s \n",buf);
        }
        send(newfd, buf,sizeof(buf),0);
    }

    close(newfd);
    close(sfd);

    return 0;        
    

}

 

client

#include<stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>   
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define ip "192.168.31.223"
#define MSG_ERR(msg) do{perror(msg);\
    fprintf(stderr,"%d\n",__LINE__);\
} while(0)
int main(int argc,const char* argv[])
{   

   int cfd = socket(AF_INET, SOCK_STREAM, 0);
    
    if(cfd < 0)
    {
        MSG_ERR("socket");
        return -1;
    }

    printf("socket created\n");
    struct sockaddr_in addr;
    socklen_t addrlen = sizeof(addr);
    addr.sin_family = AF_INET;
    addr.sin_port = htons(6666);
    addr.sin_addr.s_addr = inet_addr(ip);
    if(connect(cfd,(struct sockaddr *)&addr,sizeof(addr)) < 0)
    {
        MSG_ERR("connect");
        return -1;
    }
    printf("connected\n");

    char buf [1024] = "";
    ssize_t res;
    while (1)
    {   
        bzero(buf, sizeof(buf));
        fgets(buf,sizeof(buf),stdin);
        if(send(cfd,buf,sizeof(buf),0) < 0)
        {
            MSG_ERR("send");
            return -1;
        }
        printf("send success\n");

        res = recv(cfd, buf, sizeof(buf), 0);
        if(res < 0)
        {
            MSG_ERR("recv");
            return -1;
        }
        else if(res == 0)
        {
            printf("client unconnected\n");
            break;
        }
        else
        {
            printf("server: %s \n",buf);
        }
        
    }

    
    close(cfd);

    return 0;        
    

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值