8月15日 网络编程作业

tcp多进程并发服务器

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


typedef void (*sighandler_t)(int);
int rcvandsend(int newfd, struct sockaddr_in cin); //函数申明

//打印错误的宏函数
#define ERR_MSG(msg)  do{\
        fprintf(stderr,"__%d__",__LINE__);\
        perror(msg);\
}while(0)

#define PORT 8888            //1024~49151
#define IP "192.168.31.126"  //本机IP,用ifconfig查看   
void handler(int sig)
{
    while(waitpid(-1, NULL, WNOHANG) > 0);  //循环回收僵尸进程
}
int main(int argc, const char *argv[])
{
    //捕获17号信号
    sighandler_t s = signal(17, handler);
    if(s == SIG_ERR)
    {
        ERR_MSG("singal");
        return -1;
    }
    //创建流式套接字
    int sfd = socket(AF_INET, SOCK_STREAM, 0);
    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;
    }

    //填充地址信息结构体,真实的地址信息结构体与协议族相关
    //AT_INET,所以详情请看 man 7 ip
    struct sockaddr_in sin;
    sin.sin_family       = AF_INET;
    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("band");
        return -1;
    }

    //将套接字设置为被动监听状态,让内核去监听是否有客户端连接
    if(listen(sfd, 128) < 0)
    {
        ERR_MSG("listen");
        return -1;
    }
    printf("设置为被监听状态成功\n");

    struct sockaddr_in cin;
    socklen_t addrlen = sizeof(cin);
    int newfd = 0;
    pid_t pid = 0;
    while(1)
    {
        //从已完成连接的队列头中,取出一个客户端信息,创建生成一个新的套接字文件描述符
        //该文件描述符才是与客户端通信的文件描述符!!!
        newfd = accept(sfd, (struct sockaddr *)&cin, &addrlen);
        if(newfd < 0)
        {
            ERR_MSG("accept");
            return -1;
        }

        //网络字节序IP---->点分十进制  网络字节序的port---->本机字节序
        printf("[%s : %d]  newfd = %d\n",inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);

        //一旦连接成功,就需要创建一个子进程用于交互
        pid = fork();
        if(pid > 0)
        {
            //父进程运行
            close(newfd);
        }
        else if(pid == 0)
        {
            close(sfd);
            rcvandsend(newfd, cin);
            close(newfd);
            exit(0);
        }else
        {
            ERR_MSG("fork");
            return -1;
        }

    }
        close(sfd);

    return 0;
}

int rcvandsend(int newfd, struct sockaddr_in cin)                                                                                                       
{
    char buf[128] = "";
    ssize_t res = 0;
    while(1)
    {
        bzero(buf, sizeof(buf));
        //循环接收
        res = recv(newfd, buf, sizeof(buf), 0);
        if(res < 0)
        {
            ERR_MSG("recv");
            return -1;
        }
        else if(0 == res)
        {
            printf("[%s : %d] newfd = %d 客户端退出\n",inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);
            break;
        }
        printf("[%s : %d] newfd = %d : %s\n",inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, buf);

        char a[30] = "我收到了";
        send(newfd, a, sizeof(a), 0);
    }
    return 0;
}
                                                                                                                                                        

 

 tcp多进程并发服务器

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


//打印错误的宏函数
#define ERR_MSG(msg)  do{\
        fprintf(stderr,"__%d__",__LINE__);\
        perror(msg);\
}while(0)
//typedef void (*sighandler_t)(int);
void *callBack(void *arg);
//需要传入到线程的成员
struct clect
{
    int newfd;
    struct sockaddr_in cin;
};

void *callBack(void *arg)
{
    pthread_detach(pthread_self());  //分离分支线程
    int newfd = (*(struct clect *)arg).newfd;
    struct sockaddr_in cin = (*(struct clect *)arg).cin;
    char buf[128] = "";
    ssize_t res = 0;
    while(1)
    {
        bzero(buf, sizeof(buf));
        //循环接收
        res = recv(newfd, buf, sizeof(buf), 0);
        if(res < 0)
        {
            ERR_MSG("recv");                                                                                                                        
            break;;
        }
        else if(0 == res)
        {
            printf("[%s : %d] newfd = %d 客户端退出\n",inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);
            break;
        }
        printf("[%s : %d] newfd = %d : %s\n",inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, buf);

        char a[30] = "我收到了";
        send(newfd, a, sizeof(a), 0);
    }

    close(newfd);
    pthread_exit(NULL);

}

#define PORT 8888            //1024~49151
#define IP "192.168.31.126"  //本机IP,用ifconfig查看   
int main(int argc, const char *argv[])
{
    //创建流式套接字
    int sfd = socket(AF_INET, SOCK_STREAM, 0);
    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;
    }

    //填充地址信息结构体,真实的地址信息结构体与协议族相关
    //AT_INET,所以详情请看 man 7 ip
    struct sockaddr_in sin;
    sin.sin_family       = AF_INET;
    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("band");
        return -1;
    }

    //将套接字设置为被动监听状态,让内核去监听是否有客户端连接
    if(listen(sfd, 128) < 0)
    {
        ERR_MSG("listen");
        return -1;
    }
    printf("设置为被监听状态成功\n");

    struct sockaddr_in cin;
    socklen_t addrlen = sizeof(cin);
    int newfd = 0;
    pthread_t tid = 0;
    struct clect msg;
    while(1)
    {
        //主线程主要用于连接新的客户端
        //从已完成连接的队列头中,取出一个客户端信息,创建生成一个新的套接字文件描述符
        //该文件描述符才是与客户端通信的文件描述符!!!
        newfd = accept(sfd, (struct sockaddr *)&cin, &addrlen);
        if(newfd < 0)
        {
            ERR_MSG("accept");
            return -1;
        }

        //网络字节序IP---->点分十进制  网络字节序的port---->本机字节序
        printf("[%s : %d]  newfd = %d\n",inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);

        msg.newfd = newfd;
        msg.cin = cin;
        //一旦连接成功,就需要创建一个子线程用于交互
        pthread_create(&tid, NULL, callBack, (void *)&msg);

    }
        close(sfd);

    return 0;
}

  tcp本地服务器

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

//打印错误的宏函数
#define ERR_MSG(msg)  do{\
        fprintf(stderr,"__%d__",__LINE__);\
        perror(msg);\
}while(0)

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

    //判断要绑定的文件是否存在
    //如果存在,就删除该文件
    if(access("./unix", F_OK) == 0)   //文件存在
    {
        //删除文件
        if(unlink("./unix") < 0)
        {
            ERR_MSG("unlink");
            return -1;
        }

    }

    //填充地址信息结构体,真实的地址信息结构体与协议族相关
    //AT_UNIX,所以详情请看 man 7 ip
    struct sockaddr_un sun;
    sun.sun_family       = AF_UNIX;
    strcpy(sun.sun_path, "./unix");
    //将地址信息结构体绑定到套接字上
    if(bind(sfd, (struct sockaddr *)&sun, sizeof(sun)) < 0)
    {
        ERR_MSG("bind");
        return -1;
    }

    //将套接字设置为被动监听状态,让内核去监听是否有客户端连接
    if(listen(sfd, 128) < 0)
    {
        ERR_MSG("listen");
        return -1;
    }
    printf("设置为被监听状态成功\n");

    struct sockaddr_un cun;
    socklen_t addrlen = sizeof(cun);
    //从已完成连接的队列头中,取出一个客户端信息,创建生成一个新的套接字文件描述符
    //该文件描述符才是与客户端通信的文件描述符!!!
    int newfd = accept(sfd, (struct sockaddr *)&cun, &addrlen);                     
    if(newfd < 0)
    {
        ERR_MSG("accept");
        return -1;
    }

    printf("%s  newfd = %d\n", cun.sun_path, newfd);


    char buf[128] = "";
    ssize_t res = 0;
    while(1)
    {
        bzero(buf, sizeof(buf));
        //循环接收
        res = recv(newfd, buf, sizeof(buf), 0);
        if(res < 0)
        {
            ERR_MSG("recv");
            return -1;
        }
        else if(0 == res)
        {
            printf("%s newfd = %d 客户端退出\n", cun.sun_path ,newfd);
            break;
        }
        printf("%s newfd = %d : %s\n", cun.sun_path, newfd, buf);

        char a[30] = "我收到了";
        send(newfd, a, sizeof(a), 0);
    }

    close(sfd);
    close(newfd);

    return 0;
}
                                                                                    

   tcp本地客户端

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

//打印错误的宏函数
#define ERR_MSG(msg)  do{\
    fprintf(stderr,"__%d__",__LINE__);\
    perror(msg);\
}while(0)

#define PORT 8888            //1024~49151
#define IP "192.168.31.53"  //本机IP,用ifconfig查看

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


    //填充要连接的服务器的信息结构体
    //AT_UNIX,所以详情请看 man 7 ip
    struct sockaddr_un sun;
    sun.sun_family       = AF_UNIX;
    strcpy(sun.sun_path, "./unix");

    //连接服务器
    if(connect(sfd, (struct sockaddr *)&sun, sizeof(sun)) < 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(sfd, buf, sizeof(buf), 0) < 0)                      
        {
            ERR_MSG("send");
            return -1;
        }
        printf("发送成功\n");

        //接收
        bzero(buf, sizeof(buf));
        res = recv(sfd, buf, sizeof(buf), 0);
        if(res < 0)
        {
            ERR_MSG("recv");
            return -1;
        }
        else if(0 == res)
        {
            printf("服务器退出\n");
            break;
        }
        printf("%ld : %s\n",res, buf);
    }


    close(sfd);
    return 0;
}
                                                                    


udp本地服务器

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

//打印错误新的宏函数
#define ERR_MSG(msg)  do{\
    fprintf(stderr, " __%d__ ", __LINE__);\
    perror(msg);\
}while(0)


int main(int argc, const char *argv[])
{
    //判断要绑定的文件是否存在
    //如果存在,就删除该文件
    if(access("./unix", F_OK) == 0)   //文件存在
    {
        //删除文件                                                                                          
        if(unlink("./unix") < 0)
        {
            ERR_MSG("unlink");
            return -1;
        }

    }


    //创建报式套接字
    int sfd = socket(AF_UNIX, SOCK_DGRAM, 0);
    if(sfd < 0)
    {
        ERR_MSG("socket");
        return -1;
    }


    //填充服务器的IP地址以及端口号
    struct sockaddr_un sun;
    sun.sun_family      = AF_UNIX;
    strcpy(sun.sun_path, "./unix");

    //绑定服务器的地址信息结构体
    if(bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) < 0)
    {
        ERR_MSG("bind");
        return -1;
    }
    printf("bind success\n");

    struct sockaddr_un cun;     //存储接收到的数据包来自哪里
    socklen_t addrlen = sizeof(cun);

    char buf[128] = "";
    while(1)
    {
        bzero(buf, sizeof(buf));
        //接收
        if(recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cun, &addrlen) < 0)
        {
            ERR_MSG("recvfrom");
            return -1;
        }
        printf("%s:%s\n",cun.sun_path, buf);

        //发送
        strcpy(buf, "我收到了");
        if(sendto(sfd, buf, sizeof(buf) , 0, (struct sockaddr *)&cun, sizeof(cun) ) < 0)
        {
            ERR_MSG("sendto");
            return -1;
        }
    }
    //关闭套接字
    close(sfd);
    return 0;
}
                                                                                                            

udp本地客户端

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

//打印错误的宏函数
#define ERR_MSG(msg)  do{\
        fprintf(stderr,"__%d__",__LINE__);\
        perror(msg);\
}while(0)

#define PORT 8888            //1024~49151
#define IP "192.168.31.123"  //本机IP,用ifconfig查看   
int main(int argc, const char *argv[])
{
    //判断要绑定的文件是否存在
    //如果存在,就删除该文件
    if(access("./unix1", F_OK) == 0)   //文件存在
    {
        //删除文件
        if(unlink("./unix1") < 0)
        {
            ERR_MSG("unlink");
            return -1;
        }

    }
    //创建报式套接字
    int sfd = socket(AF_UNIX, SOCK_DGRAM, 0);
    if(sfd < 0)
    {
        ERR_MSG("socket");
        return -1;
    }
    printf("报式套接字创建成功\n");
    //自己必须绑定,不然服务器找不到自己
    //所以先填充自己的信息结构体                                                                         
    struct sockaddr_un sun1;
    sun1.sun_family         = AF_UNIX;
    strcpy(sun1.sun_path, "./unix1");

    //绑定的地址信息结构体
    if(bind(sfd, (struct sockaddr*)&sun1, sizeof(sun1)) < 0)
    {
        ERR_MSG("bind");
        return -1;
    }
    //填充服务器地址信息结构体,真实的地址信息结构体与协议族相关
    //AT_INET,所以详情请看 man 7 ip
    stru自己ct sockaddr_un sun;
    sun.sun_family      = AF_UNIX;
    strcpy(sun.sun_path, "./unix");

    char buf[128] = "";
    ssize_t res = 0;
    struct sockaddr_un rcv_addrmsg;  //存储接收到的数据包来自哪里
    socklen_t addrlen = sizeof(rcv_addrmsg);
    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 *)&sun, sizeof(sun) ) < 0)
        {
            ERR_MSG("sendto");
            return -1;
        }

        printf("发送成功\n");

        bzero(buf, sizeof(buf));
        //循环接收
        if(recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr *)&rcv_addrmsg, &addrlen) < 0)
        {
            ERR_MSG("recvfrom");
            return -1;
        }

        printf("%s : %s\n", rcv_addrmsg.sun_path, buf);

    }

    close(sfd);
    return 0;
}
                                                                                                         
                                                                                                         

 尝试抓包

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值