TCP和UDP

TCP

服务器

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

#define IP "192.168.124.114"   // 服务器IP地址
#define PORT 8080              // 服务器端口号
#define ERR_MSG(msg) {fprintf(stderr,"line:%d %s\n",__LINE__, msg); perror(msg);}  // 错误消息宏定义

int main(int argc, const char *argv[])
{
    int sfd = socket(AF_INET, SOCK_STREAM, 0);  // 创建TCP套接字
    if (sfd < 0)
    {
        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");

    // 绑定IP地址和端口
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;            // 使用IPv4协议
    sin.sin_port = htons(PORT);          // 设置端口号,使用网络字节序
    sin.sin_addr.s_addr = inet_addr(IP); // 设置IP地址

    if (bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
    {
        ERR_MSG("bind");  // 绑定失败,打印错误消息和详细错误信息
        return -1;
    }
    printf("绑定成功\n");

    // 开始监听客户端连接
    if (listen(sfd, 128) < 0)
    {
        ERR_MSG("listen");  // 监听失败,打印错误消息和详细错误信息
        return -1;
    }
    printf("监听成功\n");

    struct sockaddr_in cin;
    socklen_t addrlen = sizeof(cin);
    int newsfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);  // 接受客户端连接
    if (newsfd < 0)
    {
        ERR_MSG("accept");  // 接受连接失败,打印错误消息和详细错误信息
        return -1;
    }
    printf("客户端连接成功\n");

    ssize_t res;
    char buf[128];
    while(1)
    {
        bzero(buf,sizeof(buf));  // 清空接收缓冲区
        res = recv(newsfd, buf, sizeof(buf), 0);  // 接收客户端消息
        if (res < 0)
        {
            ERR_MSG("recv");  // 接收消息失败,打印错误消息和详细错误信息
            return -1;
        }
        else if (res == 0)
        {
            printf("%s:%d newsfd:%d 客户端关闭连接\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newsfd);
            break;  // 客户端关闭连接,退出循环
        }
        printf("%s:%d newsfd:%d 接收到客户端消息:%s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newsfd, buf);

		if(send(newsfd,buf,sizeof(buf),0) < 0)
		{
			ERR_MSG("send");
			return -1;
		}
		printf("发送成功\n");
    }

    close(newsfd);  // 关闭与客户端的连接
    close(sfd);     // 关闭服务器套接字

    return 0;
}

 客户端

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

#define IP "192.168.124.114"
#define PORT 8080
#define ERR_MSG(msg) {fprintf(stderr,"line:%d %s\n",__LINE__, msg); perror(msg); return -1;}

int main(int argc, const char *argv[])
{
    int cfd = socket(AF_INET, SOCK_STREAM, 0);  // 创建TCP套接字
    if (cfd < 0)
    {
        ERR_MSG("socket");
    }
    printf("创建套接字成功\n");

    // 允许端口快速被复用
    int reuse = 1;
    if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    {
        ERR_MSG("setsockopt");
    }

    // 连接到服务器IP地址和端口
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);
    sin.sin_addr.s_addr = inet_addr(IP);

    if(connect(cfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
    {
        ERR_MSG("connect");
    }
    printf("连接成功\n");

    ssize_t res;
    char buf[128];
    while(1)
    {
        bzero(buf,sizeof(buf));  // 清空缓冲区
        printf("请输入\n");
        fgets(buf,sizeof(buf),stdin);  // 从标准输入读取消息
        buf[strlen(buf)-1] = '\0';  // 去掉fgets函数自动添加的换行符

        if(send(cfd,buf,sizeof(buf),0) < 0)  // 发送消息给服务器
        {
            ERR_MSG("send");
            return -1;
        }

        bzero(buf,sizeof(buf));  // 清空缓冲区
        res = recv(cfd, buf, sizeof(buf)-1, 0);  // 接收服务器的响应消息
        if (res < 0)
        {
            ERR_MSG("recv");
        }
        else if (res == 0)
        {
            printf("%s:%d cfd:%d 服务器端关闭连接\n", IP, PORT, cfd);
            return -1;
        }
        else
        {
            buf[res] = '\0';
            printf("%s:%d cfd:%d 接收到服务器消息:%s\n", IP, PORT, cfd, buf);
	    return -1;
        }
    }

    close(cfd);  // 关闭客户端套接字

    return 0;
}

 UDP

服务器

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

#define IP "192.168.124.114"  // 服务器IP地址
#define PORT 8888             // 监听端口号
#define ERR_MSG(msg) {fprintf(stderr,"line:%d %s\n",__LINE__, msg); perror(msg); return -1;}

int main(int argc, const char *argv[])
{
    int sfd = socket(AF_INET, SOCK_DGRAM, 0);  // 创建UDP套接字
    if (sfd < 0)
    {
        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");

    // 设置服务器地址结构
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);             // 将端口号转换为网络字节序
    sin.sin_addr.s_addr = inet_addr(IP);    // 设置IP地址

    // 将套接字绑定到指定的IP地址和端口
    if (bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
    {
        ERR_MSG("bind");
        return -1;
    }
    printf("绑定成功\n");

    char buf[128];
    struct sockaddr_in cin;
    socklen_t addrlen = sizeof(cin);

    while (1)
    {
        bzero(buf, sizeof(buf));  // 清空缓冲区

        // 接收来自客户端的消息
        if (recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, &addrlen) < 0)
        {
            ERR_MSG("recvfrom");
            return -1;
        }

        printf("收到来自 %s:%d 的消息:%s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);

        // 将处理后的消息发送回客户端
        if (sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, addrlen) < 0)
        {
            ERR_MSG("sendto");
            return -1;
        }
        printf("回复消息给 %s:%d\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port));
    }

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

    return 0;
}

 客户端

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

#define IP "192.168.124.114"  // 服务器IP地址
#define PORT 8888             // 服务器监听端口号
#define ERR_MSG(msg) {fprintf(stderr,"line:%d %s\n",__LINE__, msg); perror(msg); return -1;}

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

    // 设置对端地址结构
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);             // 将端口号转换为网络字节序
    sin.sin_addr.s_addr = inet_addr(IP);    // 设置服务器IP地址

    char buf[128];
    struct sockaddr_in cin;
    socklen_t addrlen = sizeof(cin);

    while (1)
    {
        printf("请输入消息:\n");
        fgets(buf, sizeof(buf), stdin);  // 从标准输入读取用户输入的消息
        buf[strlen(buf) - 1] = '\0';     // 去除末尾的换行符

        // 发送消息到服务器
        if (sendto(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
        {
            ERR_MSG("sendto");
            return -1;
        }

        // 接收服务器的响应
        if (recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, &addrlen) < 0)
        {
            ERR_MSG("recvfrom");
            return -1;
        }
        printf("收到服务器 %s:%d 的响应:%s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);
    }

    close(cfd);  // 关闭套接字

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值