一、客户端代码
#include
#include
#include
#include
#pragma comment(lib,"ws2_32.lib")
char receiveBuf[1024];
char sendBuf[1024];
int main()
{
int err;
WORD versionRequired;
WSADATA wsaData;
versionRequired=MAKEWORd(2,2);
err=WSAStartup(versionRequired,&wsaData);
if (!err)
{
printf("The socket of Client has opened!n");
}
else
{
printf("The socket of Client failed to open!n");
return 0;
}
SOCKET clientSocket=socket(AF_INET,SOCK_STREAM,0);
char ip_s[100];
printf("Please input the IP of Server!n");
scanf("%s",ip_s);
SOCKADDR_IN clientsock_in;
clientsock_in.sin_addr.S_un.S_addr=inet_addr(ip_s);
clientsock_in.sin_family=AF_INET;
clientsock_in.sin_port=htons(6000);
connect(clientSocket,(SOCKADDR*)&clientsock_in,sizeof(SOCKADDR));
char r_or_s;
while(1)
{
printf("Input the operation char! Input ; to close the Client!n");
fflush(stdin);
scanf("%c",&r_or_s);
printf("The client input %c!n",r_or_s);
if(r_or_s=='r'||r_or_s=='R')
{
recv(clientSocket,receiveBuf,sizeof(char)*1024,0);
printf("%sn",receiveBuf);
}
else if (r_or_s=='s'||r_or_s=='S')
{
printf("Input the information to Server!n");
scanf("%s",sendBuf);
send(clientSocket,sendBuf,sizeof(char)*1024,0);
}
else if(r_or_s==';')
{
printf("Close the cilent");
break;
}
}
closesocket(clientSocket);
WSACleanup();
return 0;
}
二、服务端代码
#include
#include
#pragma comment(lib,"ws2_32.lib")
char sendBuf[1024];
char receiveBuf[1024];
char *account_file = "D:/code/C/socket/basin/file_transmission/account.txt";
int Login_account(SOCKET serConn)
{
FILE *fp;
int read_fp=0;
char account_id[10],account_pwd[10];
fp = fopen(account_file,"rb");
if(fp==NULL)
{
printf("The file %s does not exit!",account_file);
fclose(fp);
return 0;
}
sprintf(sendBuf,"Please input the ID of the account!n");
send(serConn,sendBuf,strlen(sendBuf)+1,0);
recv(serConn,receiveBuf,sizeof(char)*1024,0);
printf("The client want to login the account whose ID is %sn",receiveBuf);
while(1)
{
read_fp=fscanf(fp,"%s %s",account_id,account_pwd);
if(read_fp==-1)
{
sprintf(sendBuf,"Can't find the account!n");
send(serConn,sendBuf,strlen(sendBuf)+1,0);
fclose(fp);
return 0;
}
if(strcmp(account_id,receiveBuf)==0)
{
sprintf(sendBuf,"Please input the password of the account!n");
send(serConn,sendBuf,strlen(sendBuf)+1,0);
fclose(fp);
break;
}
}
int pwd_time =1;
while(pwd_time<4)
{
recv(serConn,receiveBuf,sizeof(char)*1024,0);
if (strcmp(receiveBuf,account_pwd)==0)
{
sprintf(sendBuf,"Login the account successfully!n");
send(serConn,sendBuf,strlen(sendBuf)+1,0);
printf("One account has login,ID:%s PWD:%sn",account_id,account_pwd);
return 1;
}
}
sprintf(sendBuf,"Login the account unsuccessfully!n");
send(serConn,sendBuf,strlen(sendBuf)+1,0);
return 0;
}
void Register_account(SOCKET serConn)
{
FILE *fp;
int read_fp = 0;
char account_id[10],account_pwd[10];
fp = fopen(account_file,"rb");
if(fp==NULL)
{
printf("The file %s does not exit!",account_file);
fclose(fp);
return 0;
}
sprintf(sendBuf,"Please input the ID of the account!n");
send(serConn,sendBuf,strlen(sendBuf)+1,0);
recv(serConn,receiveBuf,sizeof(char)*1024,0);
printf("The client input the ID:%sn",receiveBuf);
while(1)
{
read_fp=fscanf(fp,"%s %s",account_id,account_pwd);
if(read_fp==-1)
{
sprintf(sendBuf,"Please input the password of the account!n");
send(serConn,sendBuf,strlen(sendBuf)+1,0);
fclose(fp);
break;
}
if(strcmp(account_id,receiveBuf)==0)
{
sprintf(sendBuf,"The account has exited!Please change the ID!n");
send(serConn,sendBuf,strlen(sendBuf)+1,0);
fclose(fp);
return;
}
}
strcpy(account_id,receiveBuf);
recv(serConn,receiveBuf,sizeof(char)*1024,0);
printf("The Client input the password:%s",receiveBuf);
strcpy(account_pwd,receiveBuf);
fp = fopen(account_file,"a");
fprintf(fp,"n%s %s",account_id,account_pwd);
fclose(fp);
sprintf(sendBuf,"One new account register successfully!n ID:%s password:%s",account_id,account_pwd);
send(serConn,sendBuf,strlen(sendBuf)+1,0);
return ;
}
int main()
{
WORD myVersionRequest;
WSADATA wsaData;
myVersionRequest=MAKEWORd(2,2);
int err;
err=WSAStartup(myVersionRequest,&wsaData);
if (!err)
{
printf("The socket of Server has opened!n");
}
else
{
printf("The socket of Server failed to open!n");
return 0;
}
SOCKET serSocket=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addr;
addr.sin_family=AF_INET;
addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addr.sin_port=htons(6000);
bind(serSocket,(SOCKADDR*)&addr,sizeof(SOCKADDR));
listen(serSocket,5);
SOCKADDR_IN clientsocket;
int len=sizeof(SOCKADDR);
SOCKET serConn=accept(serSocket,(SOCKADDR*)&clientsocket,&len);
char L_or_R;
char exit_C;
int login_judge = 0;
while(1)
{
sprintf(sendBuf,"Please first to login or register!n L/l(login) R/r(Register)n");
send(serConn,sendBuf,strlen(sendBuf)+1,0);
recv(serConn,receiveBuf,sizeof(char)*1024,0);
printf("The client input the %s!n",receiveBuf);
printf("The Client %s choose to %c!n",inet_ntoa(clientsocket.sin_addr),receiveBuf[0]);
L_or_R = receiveBuf[0];
if(L_or_R=='L'||L_or_R == 'l')
login_judge=Login_account(serConn);
else if (L_or_R=='R'||L_or_R == 'r')
{
Register_account(serConn);
login_judge=Login_account(serConn);
}
else
{
sprintf(sendBuf,"Stop to login account!n");
send(serConn,sendBuf,strlen(sendBuf)+1,0);
printf("Stop to server? Y/y N/n n");
scanf("%s",&exit_C);
if(exit_C=='Y'||exit_C=='y')
break;
}
if(login_judge==0)
continue;
}
printf("Stop the server successfully!n");
sprintf(sendBuf,"The server have stopped");
send(serConn,sendBuf,strlen(sendBuf)+1,0);
closesocket(serConn);
WSACleanup();
return 0;
}
三、实现功能和使用方法
1、注册用户:注意修改服务端的账号保存文件路径
2、登录用户:会提示没有注册的用户
3、服务端:不需要多余输入操作
4、客户端:输入S/s向服务端发射信息,R/r接收服务端信息,输入;关闭客户端,如果客户端窗口没有服务端的提示,则选择接收信息,否则则选择发射信息。
四、可能存在的问题
1、客户端输入的IP地址为服务端的IP地址,通常为在CMD窗口输入ipconfig后的WLAN的IP地址。
2、客户端在服务端没有发射信息时选择接收信息,会导致卡死,需要重新操作。
五、参考文章
1、socket技术详解(看清socket编程)