网络编程第一天

在这里插入图片描述

服务器

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

int client_num = 0;
const int max_client = 10;
int client_fd[10];
int sock_fd;
pthread_t tid[10];
char userinfo[10][3][100];

void server_accept(void);
void server_broadcast(void* sid);
void func(void);

int main(int argc, char const *argv[])
{
    
    signal(SIGINT, (void *)func);
    //1、创建套接字
    sock_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (sock_fd == -1)
    {
        perror("socket");
        exit(1);
    }
    printf("socket create success\n");

    //2、绑定地址
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8848);
    addr.sin_addr.s_addr = inet_addr("192.168.124.72");
    int ret = bind(sock_fd, (const struct sockaddr *)&addr, sizeof(addr));
    if (ret == -1)
    {
        perror("bind");
        exit(1);
    }

    //3、监听
    printf("server started\n");
    ret = listen(sock_fd, max_client); //最大连接数为10
    if (ret == -1)
    {
        perror("listen");
        exit(1);
    }
    printf("server listening\n");

    //4、等待连接
    pthread_t id;
    pthread_create(&id, NULL, (void *)server_accept, NULL);
    // 主线程等待
    pause();
    close(sock_fd);
    return 0;
}

void server_accept(void)
{
    struct sockaddr_in client_addr;
    socklen_t client_addr_len = sizeof(client_addr);
    
    while (1)
    {
        client_fd[client_num] = accept(sock_fd, (struct sockaddr *)&client_addr, &client_addr_len);
        snprintf(userinfo[client_num][0], sizeof(userinfo[client_num][0]), "%d", client_fd[client_num]);
        //获取IP地址和端口号
        snprintf(userinfo[client_num][1], sizeof(userinfo[client_num][1]), "%s", inet_ntoa(client_addr.sin_addr));
        snprintf(userinfo[client_num][2], sizeof(userinfo[client_num][2]), "%d", ntohs(client_addr.sin_port));
        //printf("client%d ip:%s,port:%s\n", atoi(userinfo[client_num][0]),userinfo[client_num][1], userinfo[client_num][2]);

        if (client_fd[client_num] >= 0)
        {
            int sid=client_num;
            printf("client%d connected\n", client_fd[client_num]);
            pthread_create(&tid[client_num], NULL, (void *)server_broadcast, &sid);
            client_num++;
        }
        else
        {
            perror("accept");
            exit(1);
        }
        if (client_num >= max_client)
        {
            break;
        }
    }
}

void server_broadcast(void* sid)
{
    int id = *((int *)sid);
    //printf("client%d start broadcast\n", id);
    int fd = client_fd[id];
    printf("client%d,ip:%s,port:%s\n", fd, userinfo[id][1],userinfo[id][2]);
    char buf[1000];
    while (1)
    {
        memset(buf, 0, sizeof(buf));
        int ret = recv(fd, buf, sizeof(buf), 0);
        if (ret == -1)
        {
            perror("read");
            exit(1);
        }
        else if (ret == 0) // 客户端断开
        {
            printf("client%d closed\n", fd);
            close(fd);
            client_num--;
            break;
        }
        printf("%s:%s-->client%d say:%s\n",userinfo[id][1],userinfo[id][2],fd, buf);
        for (int i = 0; i < client_num; i++)
        {
            if(client_fd[i]==fd) continue;
            send(client_fd[i], buf, strlen(buf), 0);
        }
    }
}

void func(void)
{
    close(sock_fd);
    for (int i = 0; i < max_client; i++)
    {
        if ((client_fd[i] >= 0) && (tid[i]!= 0))
        {
            close(client_fd[i]);
            pthread_cancel(tid[i]);
        }
    }
    exit(0);
}

客户端

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

int sock_fd;

void clent_recv(void);

int main(int argc, char const *argv[])
{
    //1、创建套接字
    sock_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (sock_fd == -1)
    {
        perror("socket");
        exit(1);
    }
    printf("socket create success\n");

    //2、连接服务器
    struct sockaddr_in addr;       
    addr.sin_family=AF_INET;
    addr.sin_port=htons(8848);
    addr.sin_addr.s_addr=inet_addr("192.168.124.72");
    int ret = connect(sock_fd,(const struct sockaddr *)&addr,sizeof(addr));
    if(ret == -1)
    {
        perror("connect");
        exit(1);
    }
    printf("connect success\n");

    //3、创建客户端接受线程
    pthread_t pid;
    pthread_create(&pid,NULL,(void *)clent_recv,NULL);
    char name[20];
    printf("输入你的名字\n");
    fgets(name,20,stdin);
    //4、通信
    char s_buf[1024];
    char buf[1000];
    while (1)
    {
        memset(s_buf,0,sizeof(s_buf));
        memset(buf,0,sizeof(buf));
        printf("请输入要发送的内容:\n");
        fgets(buf,sizeof(buf),stdin);
        strncpy(s_buf,name,strlen(name)-1);
        strcat(s_buf,":");
        strcat(s_buf,buf);
        ret = send(sock_fd,s_buf,strlen(s_buf)-1,0);
        if (ret == -1)
        {
            perror("send");
            exit(1);
        }
    }
    close(sock_fd);
    return 0;
}

void clent_recv(void)
{
    char buf[1024];
    while(1)
    {
        memset(buf,0,sizeof(buf));
        int ret = recv(sock_fd,buf,sizeof(buf),0);
        if (ret == -1)
        {
            printf("read error\n");
            perror("read");
            exit(1);
        }
        else if (ret == 0)
        {
            printf("client closed\n");
            break;
        }
        printf("%s\n", buf);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值