威望0
积分7946
贡献0
在线时间763 小时
UID1
注册时间2021-4-14
最后登录2024-11-21
管理员
- UID
- 1
- 威望
- 0
- 积分
- 7946
- 贡献
- 0
- 注册时间
- 2021-4-14
- 最后登录
- 2024-11-21
- 在线时间
- 763 小时
|
[mw_shl_code=cpp,true]//server.c
#include <winsock2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#pragma comment (lib, "ws2_32.lib")
#define Max_client 8 ///The number of max clients connected.
#define Max_idlength 10 ///The biggest length of user id
#define Max_buff 256
#define Max_name 10
#define Max_pwd 10
#define Max_id 10
SOCKET clientSocket[Max_client];
int login_voucher[Max_client];
char User_id[Max_client][Max_idlength];
int k = 1;
char *account_file = "D:/code/C/socket/basin/file_transmission/account.txt";
char buff[Max_buff];
char user_name[Max_name];
void cleanbuff();
void communication(LPVOID n);
void add_s_n_s(char *s1,int id,char *s2);
void Login(LPVOID n);
int main()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData) != 0;
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
printf("The version of WSADATA is wrong!\n");
return -1;
}
printf("The version of WSADATA is available!\n");
SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSocket == INVALID_SOCKET)
{
printf("Create the Socket unsuccessfully!\n");
return -1;
}
printf("Create the Socket successfully!\n");
SOCKADDR_IN addr = { 0 };
addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addr.sin_family = AF_INET;
addr.sin_port = htons(6000);
int r = bind(serverSocket, (SOCKADDR*)&addr, sizeof(addr));
if (r == -1)
{
printf("Fail to bind!\n" );
return -1;
}
printf("Binded!\n" );
r = listen(serverSocket, 10);
if (r == -1)
{
printf("Fail to listen!\n" );
return -1;
}
printf("Listened!\n" );
SOCKADDR_IN cAddr = { 0 };
int len = sizeof(cAddr);
int i = 0;
while (i < Max_client)
{
clientSocket[i++] = accept(serverSocket, (SOCKADDR*)&cAddr, &len);
k++;
if (clientSocket[i - 1] == SOCKET_ERROR)
{
printf("The client is not available!\n");
closesocket(serverSocket);
WSACleanup();
return -1;
}
printf("The client %s has connected with server!\n",inet_ntoa(cAddr.sin_addr));
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)communication, (LPVOID)i, NULL, NULL);
}
return 0;
}
void communication(LPVOID n)
{
Login(n);
int r;
int i = (int)n;
if(login_voucher[i-1]==1)
{
return ;
}
cleanbuff();
strcpy(buff,User_id[i-1]);
send(clientSocket[i-1],buff,strlen(buff),NULL);
while (1)
{
cleanbuff();
r = recv(clientSocket[i - 1], buff, Max_buff-1, NULL);
if (r > 0)
{
printf("%s\n",buff);
for (int j = 0; j < k; j++)
{
if(j!=i-1)
send(clientSocket[j], buff, strlen(buff), NULL);
}
}
}
}
void Login(LPVOID n)
{
FILE *fp;
int read_fp=0;
int i= (int)n;
char user_id[Max_id],user_pwd[Max_pwd],user_name[Max_name];
fp = fopen(account_file,"rb");
if(fp==NULL)
{
printf("The file %s does not exit!",account_file);
fclose(fp);
return;
}
recv(clientSocket[i-1],buff,Max_buff-1,0);
printf("The client want to login the account whose ID is %s\n",buff);
while(1)
{
read_fp=fscanf(fp,"%s %s %s",user_id,user_pwd,user_name);
if(read_fp==-1)
{
cleanbuff();
sprintf(buff,"N");
send(clientSocket[i-1],buff,strlen(buff),0);
cleanbuff();
sprintf(buff,"Can't find the account!");
send(clientSocket[i-1],buff,strlen(buff),0);
fclose(fp);
return 0;
}
if(strcmp(user_id,buff)==0)
{
cleanbuff();
sprintf(buff,"Y");
send(clientSocket[i-1],buff,strlen(buff),0);
cleanbuff();
sprintf(buff,"Please input the password of the account!");
send(clientSocket[i-1],buff,strlen(buff),0);
fclose(fp);
break;
}
}
int pwd_time =1;
while(pwd_time<4)
{
cleanbuff();
recv(clientSocket[i-1],buff,Max_buff-1,0);
printf("%s\n",buff);
if (strcmp(buff,user_pwd)==0)
{
cleanbuff();
sprintf(buff,"Y");
send(clientSocket[i-1],buff,strlen(buff),0);
cleanbuff();
sprintf(buff,"Login the account successfully!");
send(clientSocket[i-1],buff,strlen(buff),0);
cleanbuff();
recv(clientSocket[i-1],buff,Max_buff-1,0); ///As a buffer between two transmissions
printf("%s\n",buff);
printf("One account has login,ID:%s PWD:%s NAME:%s\n",user_id,user_pwd,user_name);
cleanbuff();
strcpy(buff,user_name);
send(clientSocket[i-1],buff,strlen(buff),0);
break;
}
cleanbuff();
sprintf(buff,"N");
send(clientSocket[i-1],buff,strlen(buff),0);
pwd_time += 1;
cleanbuff();
sprintf(buff,"Please input the password again!");
send(clientSocket[i-1],buff,strlen(buff),0);
}
if(pwd_time<4)
{
return 1;
}
sprintf(buff,"Login the account unsuccessfully!");
send(clientSocket[i-1],buff,strlen(buff),0);
return 0;
}
void cleanbuff()
{
memset(buff,0,Max_buff);
return ;
}
[/mw_shl_code] |
|