网络编程day2

TCP服务端

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

#define ERR_MSG(msg) do{\
    printf ("line : %d\n", __LINE__);\
    perror(msg);\
}while(0)
#define IP "192.168.8.100"  //IP地址
#define PORT 6666           //端口
int main(int argc,const char * argv[])
{
    //创建流式套接字
    int sfd = socket (AF_INET, SOCK_STREAM, 0);
    if (sfd < 0)
    {
        ERR_MSG("socket");
        return -1;
    }
    //允许端口快速重用
    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;                     //必须使用AF_INET
    sin.sin_port    = htons(PORT);                  //绑定端口号的网络字节序
    sin.sin_addr.s_addr = inet_addr(IP);           //绑定本机IP地址网络字节序
    if (bind (sfd, (struct sockaddr*)&sin, sizeof(struct sockaddr_in)) < 0)
    {
        ERR_MSG("bind");
        return -1;
    }
    //将套接字设置成监听模式
    if (listen (sfd, 128) < 0)
    {
        ERR_MSG("listen");
        return -1;
    }
    //获取连接后的套接字
    struct sockaddr_in cin;
    socklen_t addrlen = sizeof(cin);
    int cfd = accept (sfd, (struct sockaddr*)&cin, &addrlen);
    if (cfd < 0)
    {
        ERR_MSG("accept");
        return -1;
    }
    
    char buf[128] = "";
    ssize_t res = 0;
    while(1)
    {
        //接收客户端的信息
        bzero(buf, sizeof(buf));
        res = recv (cfd, buf, sizeof(buf), 0);
        if (res < 0)
        {
            ERR_MSG("recv");
            return -1;
        }else if (0 == res)
        {
            printf ("对端关闭\n");
            break;
        }
        printf ("[%s : %d] %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);
        //发送信息给客户端
        //bzero(buf, sizeof(buf));
        strcat (buf, "+ +\n");
        if (send (cfd, buf, sizeof(buf), 0) < 0)
        {
            ERR_MSG("recv");
            return -1;
        }
        printf ("发送给客户端成功\n");

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

TCP客户端

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

#define ERR_MSG(msg) do{\
    printf ("line : %d\n", __LINE__);\
    perror(msg);\
}while(0)
#define IP "192.168.8.100"  //IP地址
#define PORT 6666           //端口
int main(int argc,const char * argv[])
{
    //穿件流式套接字
    int cfd = socket (AF_INET, SOCK_STREAM, 0);
    if (cfd < 0)
    {
        ERR_MSG("socket");
        return -1;
    }
    //bind 非绑定 如果不绑定系统会自动随机绑定一个端口号和本机IP到套接字上
    //允许端口快速重用
    int reuse = 1;
    if (setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))< 0)
    {
        ERR_MSG("setsockopt");
        return -1;
    }
    printf ("允许端口快速重用成功\n");
    //连接到服务器
    struct sockaddr_in sin;
    sin.sin_family  = AF_INET;                     //必须使用AF_INET
    sin.sin_port    = htons(PORT);                  //绑定端口号的网络字节序
    sin.sin_addr.s_addr = inet_addr(IP);           //绑定本机IP地址网络字节序
    if (connect(cfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
    {
        ERR_MSG("connect");
        return -1;
    }
    char buf[128] = "";
    ssize_t res = 0;
    while(1)
    {
        //发送信息给服务器
        bzero (buf, sizeof(buf));
        printf ("请输入>>>");
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf)-1] = 0;
        if (send (cfd, buf, sizeof(buf), 0) < 0)
        {
            ERR_MSG("send");
            return -1;
        }
        printf ("发送成功\n");
        //接收服务器发送的消息
        bzero (buf, sizeof(buf));
        res = recv (cfd, buf, sizeof(buf), 0);
        if (res < 0)
        {
            ERR_MSG("recv");
            return -1;
        }
        printf ("读取成功\n");
        printf ("[%s : %d] %s", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), buf);
    }
    close(cfd);
    return 0;
}

运行结果

 UDP服务器

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

#define ERR_MSG(msg) do{\
    printf ("%d", __LINE__);\
    perror(msg);\
}while(0)
#define IP "192.168.8.100"  //本机IP地址
#define PORT 8888           //端口号
int main(int argc,const char * argv[])
{
    //创建报式套接字
    int sfd = socket (AF_INET, SOCK_DGRAM, 0);
    if (sfd < 0)
    {
        ERR_MSG("socket");
        return -1;
    }
    //绑定本机ip地址和端口
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port   = htons(PORT);
    sin.sin_addr.s_addr = inet_addr(IP);
    socklen_t addrlen1 = sizeof(sin);
    if (bind (sfd, (struct sockaddr*)&sin, addrlen1) < 0)
    {
        ERR_MSG("socket");
        return -1;
    }
    
    struct sockaddr_in cin;
    socklen_t addrlen2 = sizeof(cin);
    ssize_t res = 0;
    char buf[128] = "";
    while(1)
    {
        //接收数据并获取对方信息
        bzero(buf, sizeof(buf));
        res = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, &addrlen2);
        if (res < 0)
        {
            ERR_MSG("recvfrom");
            return -1;
        }
        printf ("接收成功\n");
        printf ("[%s : %d] sfd = %d  buf = %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), sfd, buf);
        //发送数据 发给指定的人
        strcat(buf, "-.-!!");
        if (sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, addrlen2) < 0)
        {
            ERR_MSG("sendto");
            return -1;
        }
        printf ("发送成功\n");
    }
    //关闭套接字
    close(sfd);
    return 0;
}

UDP客户端

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

#define ERR_MSG(msg) do{\
    printf ("%d", __LINE__);\
    perror(msg);\
}while(0)
#define IP "192.168.8.100"  //本机IP地址
#define PORT 8888           //端口号
int main(int argc,const char * argv[])
{
    //创建报式套接字
    int sfd = socket (AF_INET, SOCK_DGRAM, 0);
    if (sfd < 0)
    {
        ERR_MSG("socket");
        return -1;
    }
    //连接服务器
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port   = htons(PORT);
    sin.sin_addr.s_addr = inet_addr(IP);
    
    ssize_t res = 0;
    char buf[128] = "";
    socklen_t addrlen = sizeof(sin);
    while(1)
    {
        //发送数据 发给指定的人
        bzero(buf, sizeof(buf));
        printf ("请输入>>>");
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf)-1] = 0;
        if (sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, addrlen) < 0)
        {
            ERR_MSG("sendto");
            return -1;
        }
        printf ("发送成功\n");
        //接收数据并获取对方信息
        res = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, &addrlen);
        if (res < 0)
        {
            ERR_MSG("recvfrom");
            return -1;
        }
        printf ("接收成功\n");
        printf ("[%s : %d] sfd = %d  buf = %s\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), sfd, buf);
    }
    //关闭套接字
    close(sfd);
    return 0;
}

运行结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值