tcp,udp服务器和客户端

(一)tcp

        1、tcp服务器

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>

#define ERR_MSG(msg) do{\
    fprintf(stderr, "line:%d\n", __LINE__);\
    fprintf(stderr, "fun:%s\n", __func__);\
    perror(msg);\
    }while(0)

#define PORT 8887
#define IP "192.168.124.51"
int main(int argc, const char *argv[])
{
    //创建流式套接字
    int sfd = socket(AF_INET, SOCK_STREAM, 0);
    if(-1 == sfd){
        ERR_MSG("socket");
        return -1;
    }
    printf("流式套接字创建成功\n");

    //允许端口快速被复用
    int reuse = 1;
    if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    {
        ERR_MSG("setsockopt");
        return -1;
    }
    printf("允许端口快速重用\n");

    //填充服务器的地址信息结构体,和bind使用
    //真实的地址信息结构体根据地址族来制定
    struct sockaddr_in sin;                                                                                                   
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);
    sin.sin_addr.s_addr = inet_addr(IP);

    //绑定服务器的地址信息,必须绑定
    if(-1 == bind(sfd, (struct sockaddr*)&sin, sizeof(sin))){
        ERR_MSG("bind");
        return -1;
    }
    printf("绑定成功\n");

    //将套接字设置为被动监听状态
    if(-1 == listen(sfd, 128)){
        ERR_MSG("listen");
        return -1;
    }
    printf("监听成功\n");

    //获取链接成功的客户端信息,生成一个新的套接字文件描述符
    struct sockaddr_in cin;
    socklen_t size = sizeof(cin);
    int newfd = accept(sfd, (struct sockaddr*)&cin, &size);
    if(-1 == newfd){
        ERR_MSG("accept");
        return -1;
    }
    printf("newfd=%d 客户端连接成功\n", newfd);

    char buf[128] = "";
    int res = 0;
    while(1){
        //收数据
        res = recvfrom(newfd, buf, sizeof(buf), 0, NULL, NULL);
        if(res < 0){
            ERR_MSG("recv");
            return -1;
        }else if(0 == res){
            fprintf(stderr, "[%s:%d] 退出\n", inet_ntoa(cin.sin_addr)\
                    , ntohs(cin.sin_port));
            return -1;
        }
        printf("[%s:%d] newfd=%d: %s\n", inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),\
                newfd, buf);
        memset(buf, 0, sizeof(buf));
        //发数据
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf) -1] = '\0'; //替换\n字符为\0字符
        if(-1 == sendto(newfd, buf, sizeof(buf), 0, NULL, 0)){
            ERR_MSG("send");
            return -1;
        }
        printf("发送成功: %s\n", buf);
        memset(buf, 0, sizeof(buf));
    }

    //关闭套接字
    close(newfd);
    close(sfd);

    return 0;
}
                                                                                                                              

       2、tcp客户端

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <sys/socket.h>
#include <string.h>

#define PORT 6666
#define IP "192.168.0.108"                                                                                   

#define MSG(msg)  do{\
    fprintf(stderr, "line: %d\n",__LINE__);\
    fprintf(stderr, "func: %s\n",__func__);\
    perror(msg);\
}while(0)

int main(int argc, const char *argv[])
{
    //创建流式套接字
    int cfd = socket(AF_INET, SOCK_STREAM, 0);
    if(-1 == cfd){
        MSG("socket");
        return -1;
    }
    printf("流式套接字创建完毕 cfd=%d\n", cfd);

    //允许端口快速被复用
    int reuse = 1;
    if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    {
        MSG("setsockopt");
        return -1;
    }
    printf("允许端口快速重用\n");

    //填充服务器的地址信息结构体,给bind函数使用
    //真实的地址信息结构体根据地址族制定
    struct sockaddr_in cin;
    cin.sin_family = AF_INET;
    cin.sin_port = htons(PORT);
    cin.sin_addr.s_addr = inet_addr(IP);

    //绑定客户端的地址信息,不是必须的
    if(-1 == bind(cfd, (struct sockaddr*)&cin, sizeof(cin))){
        MSG("bind");
        return -1;
    }
    printf("绑定成功\n");

    //连接服务器
    struct sockaddr_in cdata;
    cdata.sin_family = AF_INET;
    cdata.sin_port = htons(8890);
    cdata.sin_addr.s_addr = inet_addr(IP);
    if(-1 == connect(cfd, (struct sockaddr*)&cdata, sizeof(cdata))){
        MSG("connect");
        return -1;
    }
    printf("客户端连接服务器成功\n");

    int res;
    char buf[128] =  "";
    while(1){
        //客户端发送数据
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf)-1] = '\0';
        if(-1 == sendto(cfd, buf, sizeof(buf), 0, NULL, 0)){
            MSG("send");
            return -1;
        }
        printf("客户端写入数据成功:%s\n", buf);
        memset(buf, 0, sizeof(buf));
        //客户端收到数据
        res = recvfrom(cfd, buf, sizeof(buf), 0, NULL, NULL);
        if(-1 == res){
            MSG("recv");
            return -1;
        }else if(0 == res){
            fprintf(stderr, "服务器退出\n");
            return -1;
        }
        printf("cfd:%d %s\n",cfd, buf);
        memset(buf, 0, sizeof(buf));
    }

    //关闭文件描述符
    close(cfd);
    return 0;
}
                                                                                                             
                                                                                                             

(二)udp

        (1)udp服务器

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>

#define PORT 8890
#define IP "192.168.124.51"

#define ERR_MSG(msg) do{\
    fprintf(stderr, "line:%d\n", __LINE__);\
    fprintf(stderr, "func:%s\n", __func__);\
    perror(msg);\
}while(0);

int main(int argc, const char *argv[])
{
    //创建套接字
    int sfd = socket(AF_INET, SOCK_DGRAM,0);
    if(-1 == sfd){
        ERR_MSG("socket");
        return -1;
    }
    printf("创建套接字成功\n");

    //允许端口被重复复用
    int reuse = 1;
    if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    {
        ERR_MSG("setsockopt");
        return -1;
    }
    printf("允许端口快速重用\n");

    //填充服务器的地址信息结构体,和bind使用
    //真实的地址信息结构体根据地址族来制定
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);
    sin.sin_addr.s_addr = inet_addr(IP);
    //绑定服务器的地址信息,必须绑定
    if(-1 == bind(sfd, (struct sockaddr*)&sin, sizeof(sin))){
        ERR_MSG("bind");
        return -1;
    }
    printf("绑定成功\n");

    //将对端的IP和端口填充到套接字对应的内核中
    //此时当前UDP只能与记录的对端进行通信
    struct sockaddr_in cin;
    cin.sin_family = AF_INET;
    cin.sin_port = htons(2222);
    cin.sin_addr.s_addr = inet_addr(IP);
    if(-1 == connect(sfd, (struct sockaddr*)&cin, sizeof(cin))){
        ERR_MSG("connect");
        return -1;
    }
    printf("connect success [%s:%d]\n", IP, 2222);

    int res;
    char buf[128] = "";
    struct sockaddr_in sender;   //存储对端的地址信息
    socklen_t len = sizeof(sender);
    while(1){
        //服务收数据
        res = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&sender, &len);
        if(-1 == res){
            ERR_MSG("recvfrom");
            return -1;
        }                                                                                                  
        printf("[%s:%d] :%s\n", inet_ntoa(sender.sin_addr), ntohs(sender.sin_port), buf);
        memset(buf, 0, sizeof(buf));
        //服务器发送数据
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf)-1] = '\0';     //将\n字符替换为\0字符
        if(-1 == send(sfd, buf, sizeof(buf), 0)){
            ERR_MSG("sendto");
            return -1;
        }
        printf("send to [%s:%d] :%s\n", inet_ntoa(sender.sin_addr),\
                ntohs(sender.sin_port), buf);

    }

    //关闭套接字
    close(sfd);
    return 0;
}
                                                                                                           

        (2)udp客户端

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>

#define PORT 8890
#define IP "192.168.124.51"

#define ERR_MSG(msg) do{\
    fprintf(stderr, "line:%d\n", __LINE__);\
    fprintf(stderr, "func:%s\n", __func__);\
    perror(msg);\
}while(0);

int main(int argc, const char *argv[])
{
    //创建套接字
    int sfd = socket(AF_INET, SOCK_DGRAM,0);
    if(-1 == sfd){
        ERR_MSG("socket");
        return -1;
    }
    printf("创建套接字成功\n");

    //允许端口被重复复用
    int reuse = 1; 
    if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    {
        ERR_MSG("setsockopt");
        return -1;
    }
    printf("允许端口快速重用\n");
    
    int res;
    char buf[128] = "";
    struct sockaddr_in sender;   //存储对端的地址信息
    sender.sin_family = AF_INET;
    sender.sin_port = htons(PORT);
    sender.sin_addr.s_addr = inet_addr(IP);
    socklen_t len = sizeof(sender);
    while(1){
        //客户端发送数据
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf)-1] = '\0';     //将\n字符替换为\0字符
        if(-1 == sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&sender, len)){
            ERR_MSG("sendto");
            return -1;
        }
        printf("send to [%s:%d] :%s\n", inet_ntoa(sender.sin_addr),\
                ntohs(sender.sin_port), buf);

        //客户端收数据
        res = recvfrom(sfd, buf, sizeof(buf), 0, NULL, NULL);
        if(-1 == res){
            ERR_MSG("recvfrom");
            return -1;
        }
        printf("[%s:%d] :%s\n", inet_ntoa(sender.sin_addr), ntohs(sender.sin_port), buf);
        memset(buf, 0, sizeof(buf));
    }

    //关闭套接字
    close(sfd);
    return 0;
}                                                                                                                         

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值