网络编程day7项目

网络聊天室

服务器:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define ERR_MSG(msg)                          \
    do                                        \
    {                                         \
        fprintf(stderr, "line:%d", __LINE__); \
        perror(msg);                          \
    } while (0)
#define IP "192.168.9.253"
#define PORT 6666
struct msg
{
    char type;
    char name[20];
    char buf[128];
} info;
struct dstinfo
{
    char dstip[128];
    int dstport;
};
typedef struct sockaddr_in datatype;
typedef struct Node
{
    //数据域
    union
    {
        int len;
        datatype data;
    };
    struct Node *next;
} * Linklist;
Linklist create_head()
{
    Linklist l = (Linklist)malloc(sizeof(struct Node));
    //失败
    if (l == NULL)
        return NULL;
    //成功则赋值
    l->len = 0;
    l->next = NULL;
    return l;
}
Linklist create_node()
{
    Linklist p = (Linklist)malloc(sizeof(struct Node));
    if (p == NULL)
        return NULL;

    p->data.sin_addr.s_addr = 0;
    p->data.sin_family = AF_INET;
    p->data.sin_port = 0;
    p->next = NULL;
    return p;
}
int insert_head(Linklist l, datatype e)
{
    if (l == NULL)
    {
        printf("插入失败\n");
        return -1;
    }
    //创建新的节点
    Linklist p = create_node();
    //赋值
    //
    p->data = e;
    //p的指针域
    //
    p->next = l->next;
    l->next = p;
    //自增
    //
    l->len++;
}
int reg(Linklist p, datatype e, struct msg info, int sfd);
int chat(Linklist l, datatype e, struct msg info, int sfd);
int quit(Linklist l, datatype e, struct msg info, int sfd);
int main(int argc, const char *argv[])
{
    //申请堆区空间;
    Linklist l = create_head();
    int sfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sfd < 0)
    {
        ERR_MSG("socket");
        return -1;
    }
    printf("socket success __%d__\n", __LINE__);
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);
    sin.sin_addr.s_addr = inet_addr(IP);
    datatype e;
    if (bind(sfd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
    {
        ERR_MSG("bind");
        return -1;
    }
    printf("bind success __%d__\n", __LINE__);
    struct sockaddr_in cin;
    socklen_t addrlen = sizeof(cin);
    char buf[128] = "";
    ssize_t res = 0;
    pid_t cpid;
    cpid = fork();
    if (cpid == 0)
    {
        
        char text[128];
        struct msg system;
         fgets(text, sizeof(text), stdin);
        text[strlen(text) - 1] = 0;
        strcpy(system.name,"system");
        strcpy(system.buf,text);
        system.type='b';
        if (sendto(sfd, &system, sizeof(buf), 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
        {
            perror("sento");
            return -1;
        }
    }
    if (cpid > 0)
    {
        while (1)
        {
            res = recvfrom(sfd, &info, sizeof(info), 0, (struct sockaddr *)&cin, &addrlen);
            e = cin;
            if (res < 0)
            {
                perror("recvfrom");
                return -1;
            }
            switch (info.type)
            {
            case 'a':
                reg(l, e, info, sfd);
                break;
            case 'b':
                chat(l, e, info, sfd);
                break;
            case 'c':
                quit(l, e, info, sfd);
                break;
            }
        }
    }
    return 0;
}
int reg(Linklist l, datatype e, struct msg info, int sfd)
{
    insert_head(l, e);
    Linklist p = l;
    struct sockaddr_in sin;
    sin = e;
    char buf[128] = "";
    strcpy(buf, "登录成功");
    if (sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
    {
        perror("sento");
        return -1;
    }
    while (p->next != NULL)
    {
        p = p->next;
        if (memcmp(&sin,&p->data,sizeof(sin)))
        {
            sin = p->data;
            sprintf(buf, "%s已上线", info.name);
            if (sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
            {
                perror("sento");
                return -1;
            }
        }
    }
}
int chat(Linklist l, datatype e, struct msg info, int sfd)
{
    Linklist p = l;
    char text[256];
    struct sockaddr_in sin;
    
    while (p->next != NULL)
    {
        p = p->next;
        if (memcmp(&e,&p->data,sizeof(sin)))
        {
            sin = p->data;
            sprintf(text, "%s:%s", info.name, info.buf);
            if (sendto(sfd, text, sizeof(text), 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
            {
                perror("sendto");
                return -1;
            }
        }
    }
}

int quit(Linklist l, datatype e, struct msg info, int sfd)
{
    Linklist p = l;
    struct sockaddr_in sin;
    char buf[128];
    while (p->next != NULL)
    {
        if (!memcmp(&e,&p->next->data,sizeof(sin)))
        {
            Linklist temp = p->next;
            printf("%d",temp->data.sin_family);
            p->next = temp->next;
            free(temp);
        }
        else
        {
            p = p->next;
            sin = p->data;
            sprintf(buf, "%s:已下线", info.name);
            if (sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr *)&p->data ,sizeof(p->data)) < 0)
            {
                perror("sento");
                return -1;
            }
        }
    }
}

客户端

include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/wait.h>
#define ERR_MSG(msg)                          \
    do                                        \
    {                                         \
        fprintf(stderr, "line:%d", __LINE__); \
        perror(msg);                          \
    } while (0)
#define IP "192.168.9.253"
#define PORT 6666

struct node
{
    char type;
    char name[20];
    char buf[128];
};
void handler(int sig)
{

    while (waitpid(-1, NULL, WNOHANG) > 0)
        ;
}
int main(int argc, const char *argv[])
{
    __sighandler_t s = signal(17, handler);
    int sfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sfd < 0)
    {
        ERR_MSG("socket");
        return -1;
    }
    printf("socket success __%d__\n", __LINE__);
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);
    sin.sin_addr.s_addr = inet_addr(IP);

    struct sockaddr_in rcvaddr;
    socklen_t addrlen = sizeof(sin);

    char buf[128] = "";
    ssize_t res = 0;
    struct node user;
    printf("请输入用户名>>>");

    scanf("%s", user.name);
    user.type = 'a';
    while (getchar() != 10)
        ;
    if (sendto(sfd, &user, sizeof(user), 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
    {
        perror("sento");
        return -1;
    }
    pid_t cpid;
    cpid = fork();
    if (cpid > 0)
    {
        while (1)
        {
            res = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr *)&sin, &addrlen);
            printf("%s\n", buf);
        }
    }
    if (cpid == 0)
    {

        char text[128] = "";
        while (1)
        {

            bzero(text, sizeof(text));

            fgets(text, sizeof(text), stdin);
            text[strlen(text) - 1] = 0;
            if (strcmp(text, "quit"))
            {
                strcpy(user.buf, text);
                user.type = 'b';
                if (sendto(sfd, &user, sizeof(user), 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
                {
                    perror("sento");
                    return -1;
                }
            }
            else if (!strcmp(text, "quit"))
            {
                user.type = 'c';
                if (sendto(sfd, &user, sizeof(user), 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
                {
                    perror("sento");
                    return -1;
                }

                exit(0);
            }
        }
        return 0;
    }
}

 将dict.txt导入数据库

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main()
{

    FILE *fp = fopen("./dict.txt", "r");
    sqlite3 *db;
    if (sqlite3_open("./s1.db", &db) != SQLITE_OK)
    {
        fprintf(stderr, "err:%d %s", sqlite3_errcode(db), sqlite3_errmsg(db));
    }
    printf("db create success\n");
    char buf[128] = "create table if not exists dict(word char ,chinese char);";
    char *errmsg = NULL;
    sqlite3_exec(db, buf, NULL, NULL, &errmsg);
    char eng[30];
    char chn[30];
    int i = 0, j = 0;
    int num = 1;
    char cmd[128];


    while (1)
    {
        bzero(eng, sizeof(eng));
        bzero(chn, sizeof(chn));
        i = 0;
        j = 0;
        if (fgets(buf, sizeof(buf), fp) == NULL)
        {
            break;
        }
        while (buf[i] != ' ')
        {
            eng[0] = '"';

            eng[i + 1] = buf[i];
            i++;
        }
        eng[i + 1] = '"';
        j = i;
        i = 0;
        while (buf[j] == ' ')
            j++;
        while (buf[j] != '\n')
        {
            chn[i] = buf[j];
            i++;
            j++;
        }
        chn[i] = 0;

        sprintf(cmd, "insert into dict values (%s,'%s');", eng, chn);

        if (sqlite3_exec(db, cmd, NULL, NULL, &errmsg) != SQLITE_OK)
        {
            fprintf(stderr, "%s\n", errmsg);
            return -1;
        }
        num++;
    }
    printf("copy 完成\n");
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值