⽤C语⾔实现简单的聊天室功能⽤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 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;
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;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论