编写一个基于Windows Socket的c语言程序。该程序允许多个用户同时在互联网上进行沟通。已经有了“server.cpp”和“client.cpp”,请补全这个程序的代码,并保证写入log到txt中。
时间: 2023-06-23 10:09:29 浏览: 96
以下是一个基于Windows Socket的c语言程序的代码示例,包括了“server.cpp”和“client.cpp”的补全部分,同时将聊天记录写入log.txt中。
server.cpp:
```c
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <windows.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#define BUF_SIZE 200
#define MAX_CLNT 256
void error_handling(char *message);
int main(int argc, char *argv[])
{
WSADATA wsaData;
SOCKET hServSock, hClntSock;
SOCKADDR_IN servAddr, clntAddr;
int szClntAddr;
char message[BUF_SIZE];
int i, strLen;
if(argc!=2) {
printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
if(WSAStartup(MAKEWORD(2,2), &wsaData)!=0)
error_handling("WSAStartup() error!");
hServSock=socket(PF_INET, SOCK_STREAM, 0);
if(hServSock == INVALID_SOCKET)
error_handling("socket() error");
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family=AF_INET;
servAddr.sin_addr.s_addr=htonl(INADDR_ANY);
servAddr.sin_port=htons(atoi(argv[1]));
if(bind(hServSock, (SOCKADDR*)&servAddr, sizeof(servAddr))==SOCKET_ERROR)
error_handling("bind() error");
if(listen(hServSock, 5)==SOCKET_ERROR)
error_handling("listen() error");
while(1)
{
szClntAddr=sizeof(clntAddr);
hClntSock=accept(hServSock, (SOCKADDR*)&clntAddr, &szClntAddr);
if(hClntSock==INVALID_SOCKET)
continue;
while((strLen=recv(hClntSock, message, BUF_SIZE, 0))!=0)
{
fwrite(message, 1, strLen, stdout); //将收到的消息打印到控制台
fwrite(message, 1, strLen, logfile); //将收到的消息写入log文件
send(hClntSock, message, strLen, 0); //将收到的消息原封不动地发送回去
}
closesocket(hClntSock);
}
closesocket(hServSock);
WSACleanup();
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
```
client.cpp:
```c
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <windows.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#define BUF_SIZE 200
void error_handling(char *message);
int main(int argc, char *argv[])
{
WSADATA wsaData;
SOCKET hSocket;
SOCKADDR_IN servAddr;
char message[BUF_SIZE];
int strLen;
if(argc!=3) {
printf("Usage : %s <IP> <port>\n", argv[0]);
exit(1);
}
if(WSAStartup(MAKEWORD(2,2), &wsaData)!=0)
error_handling("WSAStartup() error!");
hSocket=socket(PF_INET, SOCK_STREAM, 0);
if(hSocket == INVALID_SOCKET)
error_handling("socket() error");
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family=AF_INET;
servAddr.sin_addr.s_addr=inet_addr(argv[1]);
servAddr.sin_port=htons(atoi(argv[2]));
if(connect(hSocket, (SOCKADDR*)&servAddr, sizeof(servAddr))==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;
send(hSocket, message, strlen(message), 0);
strLen=recv(hSocket, message, BUF_SIZE-1, 0);
message[strLen]=0;
fwrite(message, 1, strLen, stdout); //将收到的消息打印到控制台
fwrite(message, 1, strLen, logfile); //将收到的消息写入log文件
}
closesocket(hSocket);
WSACleanup();
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
```
阅读全文