C语言聊天室代码(服务端)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
/* server.c */
#include
#include
#include
#include
#include
#pragma comment(lib,"ws2_32.lib")
#include
#define REG 1 //表示注册
#define LOGIN 2 //表示登陆
#define TALK_PRIVATE 3 //表示私聊
#define TALK_PUBLIC 4 //表示群聊
#define LIST 11 //表示显示所有在线不隐身用户
#define EXIT 12 //退出客户端
struct user
{
char username[50];
char password[50];
int cmd;
char target_user[20];
char content[50];
};
struct user user;
char *file_name(char *p,struct user user) //生成登陆名.txt文件函数
{
int i = 0;
int username_len;
username_len = strlen(ername);
strcpy(p,ername);
strcat(p,".txt");
return p;
}
void resign(SOCKET sockfd,struct user user) //注册函数
{
int username_len;
char buf[50];
char buf_fp[50];
FILE *fp;
if((fp=fopen("resign.txt","a+"))==NULL)
{
perror("open resign.txt failed !\n");
exit(1);
}
username_len = strlen(ername);
while(1)
{
if ((fgets(buf_fp,sizeof(buf_fp),fp)) == NULL)
{
send(sockfd,"right",5,0);
strcpy(buf,ername);
buf[strlen(buf)]='\0';
int i = fprintf(fp,"%s",buf);
memset(buf,0,sizeof(buf));
strcpy(buf,user.password);
printf("%s",buf);
i=fprintf(fp,":%s\n",buf);
memset(buf,0,sizeof(buf));
fclose(fp);
break;
}
if (strncmp(ername,buf_fp,strlen(ername)) == 0)
{
send(sockfd,"wrong",5,0);
break;
}
}
}
void login(SOCKET sockfd,struct user user) //登陆函数
{
char buf_fp[100];
char buf[100];
int username_len = strlen(ername);
strcpy(buf,ername);
buf[username_len] = ':';
buf[username_len+1] = '\0';
char *p=strcat(buf,user.password);
p[strlen(p)]='\0';
FILE *fp;
if((fp=fopen("resign.txt","r+"))==NULL)
{
perror("open resign.txt failed !\n");
exit(1);
}
//printf("%s",p);
while(1)
{
if ((fgets(buf_fp,sizeof(buf_fp),fp)) == NULL)
{
send(sockfd,"wrong",5,0);
fclose(fp);
break;
}
if (strncmp(p,buf_fp,strlen(buf)) == 0)
{
send(sockfd,"ok",2,0);
fclose(fp);
break;
}
}
}
struct Client_userName
{
SOCKET client;
char username[50];
};
int main()
{
printf("\n\t -----------------------------------------------服务器-----------------------------------------------\n\n\n");
WSAData wsaData;
int err=WSAStartup(WINSOCK_VERSION,&wsaData);
if(err!=0)
{
return -1;
}
SOCKET ListenSocket=socket(AF_INET,SOCK_STREAM,0);
if(INVALID_SOCKET==ListenSocket)
{
printf("socket() Failed:%d\n",WSAGetLastError());
WSACleanup()
;
return -1;
}
sockaddr_in service;
service.sin_family=AF_INET;
service.sin_port=htons(20000);
service.sin_addr.s_addr=htonl(INADDR_ANY);
err=bind(ListenSocket,(sockaddr*)&service,sizeof(service));
if(SOCKET_ERROR==err)
{
printf("bind() Failed:%d\n:",WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return -1;
}
err=listen(ListenSocket,1);
if(SOCKET_ERROR==err)
{
printf("listen() Failed:%d\n",WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return -1;
}
#define MAX_CLIENT_NUM 63
struct Client_userName Client[MAX_CLIENT_NUM];
//SOCKET client[MAX_CLIENT_NUM];
for(int i=0;i
Client[i].client=INVALID_SOCKET;
memset(Client[i].username,0,sizeof(Client[i].username));
// client[i]=INVALID_SOCKET;
}
fd_set rset,allset;
FD_ZERO(&allset);
FD_SET(ListenSocket,&allset);
#define MSG_MAX_SIZE 1024*2 //接收的最大值
while(1)
{
rset=allset;
int ret=select(0,&rset,NULL,NULL,NULL);
if(ret==SOCKET_ERROR)
{
printf("select() failed:%Id\n",WSAGetLastError());
break;
}
if(ret==0)
{
continue;
}
if(FD_ISSET(ListenSocket,&rset))
{
struct sockaddr_in cliaddr;
int clilen=sizeof(cliaddr);
SOCKET sockConn=accept(ListenSocket,(sockaddr*)&cliaddr,&clilen);
if(INVALID_SOCKET==sockConn)
{
printf("accept() Failed:%d\n",WSAGetLastError());
break;
}
for(int i=0;i
if(Client[i].client==INVALID_SOCKET)
{
Client[i].client=sockConn;
break;
}
}
if(i
FD_SET(sockConn,&allset);
}
else{
printf("too many clients");
closesocket(sockConn);
}
}
for(i=0;i
if(Client[i].client!=INVALID_SOCKET&&FD_ISSET(Client[i].client,&rset))
{
memset(&user,0,1);
int nByte=recv(Client[i].client,(char*)&user,sizeof(user),0);
strcpy(Client[i].username,ername);
printf("Have Message From Client[%d]:%s\n",i,Client[i].username);
if(user.cmd==REG)
{
resign(Client[i].client,user);
}
else if(user.cmd==LOGIN)
{
login(Client[i].client,user);
}
else if(user.cmd==TALK_PUBLIC)
{
struct user Send;
strcpy(ername,ername);
strcpy(Send.content,user.content);
Send.cmd=TALK_PUBLIC;
for(int j=0;j
if(Client[j].client!=INVALID_SOCKET&&j!=i)
{
send(Client[j].client,(char*)&Send,sizeof(Send),0);
}
}
}
else if(user.cmd==TALK_PRIVATE)
{
for(int j=0;j
if(strcmp(Client[j].username,user.target_user)==0&&j!=i)
{
struct user Send;
strcpy(ername,ername);
strcpy(Send.content,user.content);
Send.cmd=TALK_PRIVATE;
send(Client[j].client,(char*)&Send,size
of(Send),0);
}
}
}
else if(user.cmd==LIST)
{
for(int j=0;j
if(Client[j].client!=INVALID_SOCKET&&FD_ISSET(Client[i].client,&rset)&&j!=i)
{
struct user Send;
strcpy(ername,Client[j].username);
Send.cmd=LIST;
send(Client[i].client,(char*)&Send,sizeof(Send),0);
}
}
}
else if(user.cmd==EXIT)
{
struct user Send;
Send.cmd=EXIT;
send(Client[i].client,(char*)&Send,sizeof(Send),0);
FD_CLR(Client[i].client,&allset);
closesocket(Client[i].client);
Client[i].client=INVALID_SOCKET;
memset(Client[i].username,0,sizeof(Client[i].username));
continue;
}
if(nByte==0)
{
FD_CLR(Client[i].client,&allset);
closesocket(Client[i].client);
Client[i].client=INVALID_SOCKET;
memset(Client[i].username,0,sizeof(Client[i].username));
continue;
}
if(nByte==SOCKET_ERROR)
{
printf("recv() failed:%d\n",WSAGetLastError());
FD_CLR(Client[i].client,&allset);
closesocket(Client[i].client);
Client[i].client=INVALID_SOCKET;
memset(Client[i].username,0,sizeof(Client[i].username));
continue;
}
}
}
}
closesocket(ListenSocket);
WSACleanup();
return -1;
}