客户端
// client.cpp : Defines the entry point for the console application.
//ws2_32.lib
#include "stdafx.h"
bool InitSock()
{
WSADATA wsaData;
long iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0)
{
return false;
}
return true;
}
SOCKET CreateTCPSocket() //返回真则成功
{
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
WSACleanup();
}
return s;
}
bool Connect(SOCKET s,char *ip,int port)
{
struct sockaddr_in hints;
hints.sin_family = AF_INET;
hints.sin_port = htons(port);
hints.sin_addr.S_un.S_addr=inet_addr(ip);
int r=connect(s,(struct sockaddr FAR* )&hints,sizeof(hints));
if(r!=0)
{
r=WSAGetLastError();
if(r==WSAEWOULDBLOCK)
{
printf("%d block\n",r);
r=WSAGetLastError();
return true;
}
return false;
}
return true;
}
int main(int argc, char* argv[])
{
if(!InitSock())
{
printf("初始化失败!\n");
return 0;
}
SOCKET s= CreateTCPSocket() ;
bool r=Connect(s,"127.0.0.1",2000);
if(!r)
{
printf("连接服务器失败!\n");
closesocket(s);
WSACleanup();
return 0;
}
char user[50]={0};
char pass[50]={0};
printf("请输入用户\n");
scanf("%s",user);
printf("请输入密码\n");
scanf("%s",pass);
printf("请选择功能\n");
printf("1 注册\n");
printf("2 登陆\n");
int n=0;
scanf("%d",&n);
char buf[100]={0};
buf[0]=n;
strcpy(buf+1,user);
strcpy(buf+50,pass);
int len=send(s,buf,100,0);
if(len!=100)
{
printf("发送失败!\n");
closesocket(s);;
WSACleanup();
return 0;
}
memset((void *)buf,0,100);
aecv:
len=recv(s,buf,100,0);
if(len>0)
{
int *cmd=(int *)buf;
switch(*cmd)
{
case 0:
printf("登录成功。\n");
break;
case 1:
printf("reg good,注册成功\n");
break;
case 2:
printf("reg bad,注册失败,该账户账号已存在,请选择登录\n");
break;
case 3:
printf("登录失败。账号或密码错误\n");
break;
}
}
else
{
printf("接收数据失败!\n");
}
getchar();
closesocket(s);;
WSACleanup();
return 0;
}
服务器
// server.cpp : Defines the entry point for the console application.
//ws2_32.lib
#include "stdafx.h"
#include <time.h>
bool InitSock()
{
WSADATA wsaData;
long iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0)
{
return false;
}
return true;
}
SOCKET CreateTCPSocket() //返回真则成功
{
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
WSACleanup();
}
return s;
}
char * GetTime()
{
time_t t=time(NULL);
return ctime(&t);
}
bool Bind(SOCKET s,char *Ip,int port)
{
struct sockaddr_in hints;
hints.sin_family = AF_INET;
hints.sin_port = htons(port);
hints.sin_addr.S_un.S_addr = inet_addr(Ip);
int r=bind(s,(const sockaddr *)&hints,sizeof(hints));
if(r==0) return true;
return false;
}
int GetAllIp(char ip[10][50])
{
char name[200];
int r=gethostname(name,200);
int i=0;
if(r==0)
{
LPHOSTENT p=gethostbyname(name);
for(i=0; p->h_addr_list[i]!=0; i++)
{
strcpy(ip[i],inet_ntoa( *(struct in_addr*)p->h_addr_list[i] ));
}
}
return i;
}
void WriteUser(char *user, char *pass)
{
char path[MAX_PATH+1]={0};
GetCurrentDirectory(MAX_PATH,path);
strcat(path,"\\user.ini");
::WritePrivateProfileString("user",user,pass,path);
}
void ReadUser(char *user, char *pass)
{
char path[MAX_PATH+1]={0};
GetCurrentDirectory(MAX_PATH,path);
strcat(path,"\\user.ini");
GetPrivateProfileString("user",user,"",pass,50,path);
}
bool ValidateUser(char *user, char *pass)
{
char path[MAX_PATH+1] = {0};
GetCurrentDirectory(MAX_PATH, path);
strcat(path, "\\user.ini");
char storedPass[50] = {0};
GetPrivateProfileString("user", user, "", storedPass, 50, path);
// 比较接收到的密码和存储的密码是否相等
if (strcmp(pass, storedPass) == 0) {
return true; // 密码验证通过
} else {
return false; // 密码验证失败
}
}
int main(int argc, char* argv[])
{
if(InitSock())
{
int port=4000;
SOCKET s=CreateTCPSocket() ;
int r=Bind(s,"127.0.0.1",port);
if(r)
{
printf("服务器启动\n");
}
listen(s,5);
struct sockaddr_in addr;
int len=sizeof(addr);
while(1)
{
char buf[100]={0};
SOCKET news=accept(s,(sockaddr *)&addr,&len);
memset((void *)buf,0,100);
int i=recv(news,buf,100,0);
if(i!=100)
{
printf("接收数据错误!\n");
closesocket(news);
continue;
}
char *user=buf+1;
char *pass=buf+50;
unsigned char cmd=buf[0];
if(cmd==1)
{
printf("reg: %s %s\n",user,pass);
//显示连接的ip和时间
printf("连接的IP为:%s\n",inet_ntoa(addr.sin_addr));
printf("连接的时间为:%s\n",GetTime());
char pass1[50]={0};
ReadUser(user,pass1);
if(pass1[0]==0)
{
WriteUser(user,pass);
int n=1;
send(news,(char *)&n,sizeof(int),0);
}
else
{
int n=2;
send(news,(char *)&n,sizeof(int),0);
}
}
else
{
if(ValidateUser(user,pass)){
int n=0;
printf("login: %s %s\n",user,pass);
//显示连接的ip和时间
printf("连接的IP为:%s\n",inet_ntoa(addr.sin_addr));
printf("连接的时间为:%s\n",GetTime());
send(news,(char *)&n,sizeof(int),0);
}
else {
int n=3;
send(news,(char *)&n,sizeof(int),0);
}
}
}
}
return 0;
}