资源简介
服务器端采用select函数,主进程采用select判断并调用相应函数,服务器实现了私聊群聊查看聊天记录等功能。使用具体内容详见博客。
代码片段和文件信息
#include “main.h“
#include “ClientFunction.h“
char MyID[24];
char ToOther[24];
int State = NORMAL;
FILE *fp;
int i = 0;
int main(int argc char *argv[])
{
int sockfd ret;
struct sockaddr_in sever_addr;
struct UserInfo Info;
char cmd[100] = {0};
linklist OtherUserInfo;
Chatlink ChatInformation;
if(2 != argc)
{
printf(“Error!\n“);
exit(4);
}
ret = ListInit(&OtherUserInfo);
if(1 == ret)
{
printf(“Init ListInit failure!\n“);
exit(4);
}
ret = ChatlinkInit(&ChatInformation);
if(1 == ret)
{
printf(“Init ChatlinkInit failure!\n“);
exit(4);
}
sockfd = socket(AF_INET SOCK_STREAM 0);
if(-1 == sockfd)
{
perror(“socket“);
exit(1);
}
memset(&sever_addr 0 sizeof(struct sockaddr_in));
sever_addr.sin_family = AF_INET;
sever_addr.sin_port = htons(PORT);
sever_addr.sin_addr.s_addr = inet_addr(argv[1]);
ret = connect(sockfd (struct sockaddr *)&sever_addr sizeof(sever_addr));
if(-1 == ret)
{
perror(“connect“);
exit(1);
}
memset(&Info 0 sizeof(Info));
WelcomInterface();
while(1)
{
memset(cmd 0 sizeof(cmd));
LOGINInterface();
scanf(“%s“cmd);
// gets(cmd);
if(1 != strlen(cmd))
{
continue;
}
switch(cmd[0])
{
case ‘1‘:
printf(“adas\n“);
ret = UserLogin(&sockfd OtherUserInfo ChatInformation);
if(ret == 10)
{
return 0;
}
else if(ret)
{
printf(“Error in UserLogin().\n“);
}
break;
case ‘2‘:
ret = NewUser(&sockfd);
if(ret == 1)
{
printf(“Error in NewUser().\n“);
}
break;
case ‘3‘:
ret = FindPasswd(&sockfd);
if(ret == 1)
{
printf(“Error in FindPasswd().\n“);
}
break;
default:
printf(“hi\n“);
printf(“Error\n“);
break;
}
}
return 0;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-12-26 20:28 ChatRoom\
文件 1754 2017-12-26 20:28 ChatRoom\Client.c
文件 25793 2017-12-26 20:28 ChatRoom\ClientFunction.c
文件 729 2017-12-26 20:28 ChatRoom\ClientFunction.h
文件 3931 2017-12-26 20:28 ChatRoom\li
文件 1092 2017-12-26 20:28 ChatRoom\li
文件 1435 2017-12-26 20:28 ChatRoom\main.h
文件 408 2017-12-26 20:28 ChatRoom\makefile
文件 2923 2017-12-26 20:28 ChatRoom\Sever.c
文件 15414 2017-12-26 20:28 ChatRoom\SeverFunction.c
文件 871 2017-12-26 20:28 ChatRoom\SeverFunction.h
评论
共有 条评论