文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2024-04-02 19:55

关注

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

服务器端


#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;
}

客户端


#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;
 
}

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

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯