使用socket使用c语言写一个网络聊天室代码,在windows下运行
时间: 2024-06-12 14:03:56 浏览: 104
抱歉,我是一个语言模型AI,无法提供桌面应用程序。但是,下面是一个使用socket编写网络聊天室的示例代码,您可以参考一下:
Server端代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#define BUF_SIZE 100
#define MAX_CLNT 256
void error_handling(char *message);
int main(int argc, char *argv[])
{
WSADATA wsaData;
SOCKET serv_sock, clnt_sock;
SOCKADDR_IN serv_adr, clnt_adr;
int clnt_adr_sz;
char message[BUF_SIZE];
int str_len, i;
int clnt_cnt = 0;
SOCKET clnt_socks[MAX_CLNT];
fd_set reads, cpy_reads;
if (argc != 2) {
printf("Usage: %s <port>\n", argv[0]);
exit(1);
}
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
error_handling("WSAStartup() error!");
serv_sock = socket(PF_INET, SOCK_STREAM, 0);
if (serv_sock == INVALID_SOCKET)
error_handling("socket() error!");
memset(&serv_adr, 0, sizeof(serv_adr));
serv_adr.sin_family = AF_INET;
serv_adr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_adr.sin_port = htons(atoi(argv[1]));
if (bind(serv_sock, (SOCKADDR*)&serv_adr, sizeof(serv_adr)) == SOCKET_ERROR)
error_handling("bind() error!");
if (listen(serv_sock, 5) == SOCKET_ERROR)
error_handling("listen() error!");
FD_ZERO(&reads);
FD_SET(serv_sock, &reads);
while (1) {
cpy_reads = reads;
select(0, &cpy_reads, 0, 0, NULL);
for (i = 0; i < clnt_cnt; i++) {
if (FD_ISSET(clnt_socks[i], &cpy_reads)) {
str_len = recv(clnt_socks[i], message, BUF_SIZE, 0);
if (str_len == 0) {
FD_CLR(clnt_socks[i], &reads);
closesocket(clnt_socks[i]);
for (int j = i; j < clnt_cnt - 1; j++)
clnt_socks[j] = clnt_socks[j + 1];
clnt_cnt--;
printf("Client %d disconnected\n", i + 1);
}
else {
message[str_len] = 0;
printf("From client %d: %s\n", i + 1, message);
for (int j = 0; j < clnt_cnt; j++)
send(clnt_socks[j], message, str_len, 0);
}
}
}
if (FD_ISSET(serv_sock, &cpy_reads)) {
clnt_adr_sz = sizeof(clnt_adr);
clnt_sock = accept(serv_sock, (SOCKADDR*)&clnt_adr, &clnt_adr_sz);
if (clnt_sock == INVALID_SOCKET)
error_handling("accept() error!");
FD_SET(clnt_sock, &reads);
clnt_socks[clnt_cnt++] = clnt_sock;
printf("Connected client IP: %s, Port: %d\n", inet_ntoa(clnt_adr.sin_addr), ntohs(clnt_adr.sin_port));
}
}
closesocket(serv_sock);
WSACleanup();
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
```
Client端代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#define BUF_SIZE 100
void error_handling(char *message);
int main(int argc, char *argv[])
{
WSADATA wsaData;
SOCKET sock;
SOCKADDR_IN serv_adr;
char message[BUF_SIZE];
int str_len;
if (argc != 3) {
printf("Usage: %s <ip> <port>\n", argv[0]);
exit(1);
}
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
error_handling("WSAStartup() error!");
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET)
error_handling("socket() error!");
memset(&serv_adr, 0, sizeof(serv_adr));
serv_adr.sin_family = AF_INET;
serv_adr.sin_addr.s_addr = inet_addr(argv[1]);
serv_adr.sin_port = htons(atoi(argv[2]));
if (connect(sock, (SOCKADDR*)&serv_adr, sizeof(serv_adr)) == SOCKET_ERROR)
error_handling("connect() error!");
while (1) {
fputs("Input message(Q to quit): ", stdout);
fgets(message, BUF_SIZE, stdin);
if (!strcmp(message, "q\n") || !strcmp(message, "Q\n"))
break;
str_len = send(sock, message, strlen(message), 0);
printf("Sent message length: %d\n", str_len);
str_len = recv(sock, message, BUF_SIZE - 1, 0);
message[str_len] = 0;
printf("Received message: %s\n", message);
}
closesocket(sock);
WSACleanup();
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
```
阅读全文