服务器之家

服务器之家 > 正文

C语言实现简单的聊天室功能

时间:2021-11-19 15:53     来源/作者:Ywrby

用C语言实现简单的聊天室功能,供大家参考,具体内容如下

服务器端

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<winsock.h>
#pragma comment(lib, "ws2_32.lib")
#define L 256
int main() {
    char SendBuf[L];
    char ReceiveBuf[L];
    int SendLen;
    int ReceiveLen;
    int Length;
 
    SOCKET socket_server;
    SOCKET socket_receive;
 
    SOCKADDR_IN ServerAdd;
    SOCKADDR_IN ClientAdd;
    WORD wVR;
    WSADATA WsaData;
    int error;
 
 
    //++++++++++++++++++++++++++++++++++++++++++++++++
    wVR = MAKEWORD(2, 2);
    error = WSAStartup(wVR, &WsaData);
    if (error != 0) {
        printf("加载套接字失败!");
        return 0;
    }
    if (LOBYTE(WsaData.wVersion) != 2 || HIBYTE(WsaData.wVersion) != 2) {
        printf("版本不符!");
        WSACleanup();
        return 0;
    }
    //++++++++++++++++++++++++++++++++++++++++++++++++
    ServerAdd.sin_family = AF_INET;
    ServerAdd.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    ServerAdd.sin_port = htons(5000);
    //++++++++++++++++++++++++++++++++++++++++++++++++
    socket_server = socket(AF_INET, SOCK_STREAM, 0);
    //++++++++++++++++++++++++++++++++++++++++++++++++
    if (bind(socket_server, (SOCKADDR*)&ServerAdd, sizeof(SOCKADDR)) == SOCKET_ERROR) {
        printf("绑定失败!\n");
    }
    //++++++++++++++++++++++++++++++++++++++++++++++++
    if (listen(socket_server, 5) < 0) {
        printf("监听失败!\n");
    }
    Length = sizeof(SOCKADDR);
    socket_receive = accept(socket_server, (SOCKADDR*)&ClientAdd, &Length);
    if (socket_receive == SOCKET_ERROR) {
        printf("接受连接失败!\n");
    }
    while (1) {
        ReceiveLen = recv(socket_receive, ReceiveBuf, L, 0);
        if (ReceiveLen < 0) {
            printf("接受失败\n程序退出!\n");
            break;
        }
        else {
            printf("client say:%s\n", ReceiveBuf);
        }
        //++++++++++++++++++++++++++++++++++++++++++++++++
        printf("please enter message:");
        scanf("%s", SendBuf);
        SendLen = send(socket_receive, SendBuf, L, 0);
        if (SendLen < 0) {
            printf("发送失败\n");
        }
    }
    closesocket(socket_receive);
    closesocket(socket_server);
    return 0;
}

客户端

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<winsock.h>
#pragma comment(lib, "ws2_32.lib")
#define L 256
int main() {
    char SendBuf[L];
    char ReceiveBuf[L];
    int SendLen;
    int ReceiveLen;
    int Length;
 
    SOCKET socket_send;
 
    SOCKADDR_IN ServerAdd;
 
    WORD wVR;
    WSADATA WsaData;
    int error;
 
 
    //++++++++++++++++++++++++++++++++++++++++++++++++
    wVR = MAKEWORD(2, 2);
    error = WSAStartup(wVR, &WsaData);
    if (error != 0) {
        printf("加载套接字失败!");
        return 0;
    }
    if (LOBYTE(WsaData.wVersion) != 2 || HIBYTE(WsaData.wVersion) != 2) {
        printf("版本不符!");
        WSACleanup();
        return 0;
    }
    //++++++++++++++++++++++++++++++++++++++++++++++++
    ServerAdd.sin_family = AF_INET;
    ServerAdd.sin_addr.S_un.S_addr = inet_addr("192.168.190.1");
    ServerAdd.sin_port = htons(5000);
    //++++++++++++++++++++++++++++++++++++++++++++++++
 
    socket_send = socket(AF_INET, SOCK_STREAM, 0);
 
 
    if (connect(socket_send, (SOCKADDR*)&ServerAdd, sizeof(SOCKADDR)) == SOCKET_ERROR) {
        printf("连接失败!\n");
    }
    //++++++++++++++++++++++++++++++++++++++++++++++++
    while (1) {
        printf("please enter message:");
        scanf("%s", SendBuf);
        SendLen = send(socket_send, SendBuf, L,0);
        if (SendLen < 0) {
            printf("发送失败!\n");
        }
        //++++++++++++++++++++++++++++++++++++++++++++++++
        ReceiveLen = recv(socket_send, ReceiveBuf, L, 0);
        if (ReceiveLen < 0) {    
            printf("接受失败!\n程序退出\n");
            break;
        }
        else {
            printf("Server say:%s\n", ReceiveBuf);
        }
    }
    closesocket(socket_send);
    WSACleanup();
    return 0;
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/renboyu010214/article/details/106896771

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部