#include <winsock2.h>
#include <io.h>
#include <iostream>
#include <fstream>
#include <windows.h>
#include <thread>
#include <cstdio>
#include <cstring>
#include <list>
#include <time.h>
#pragma comment (lib,"ws2_32.lib")
#define STR_MAX_LEN 200
#define NAME_LEN 20
#define PWD_LEN 16
#define MML_LEN 10
#define SEND_LEN STR_MAX_LEN + NAME_LEN + 2
#define DK 5327
using namespace std;ofstream Sf;
const int MAX_ONLINE = 20;
const char Server_IP[] = "172.17.0.3";//"127.0.0.1";
const char Warn_Buf[] = "It's over Max connect!",
aok[2]="0",
anok[2]="1",
anok2[2]="2",
azok[2]="0",
aznok[2]="1",
ml[100]="";
list < SOCKET > online;
bool online_using = 0;
char name[NAME_LEN];
FILE *fp,*fp2;
void NewHandle(SOCKET &SockFrom);
void Wrong_exit(const char* Error_place, int _Code) {
printf("%s wrong!(at: %d)\n", Error_place, _Code);
system("pause");
exit(_Code);
}void show_time() {
static char time_l[20], time_n[20];
static time_t timep;
{
time(&timep);
strftime(time_n, sizeof(time_n), "%Y-%m-%d %H:%M", localtime(&timep));
if (strcmp(time_l, time_n)) {
strcpy(time_l, time_n);
printf("// %s\n", time_n);
}
}
}void broadcast(const char* name, const char* send_buf) {
static char ls_send[SEND_LEN]; strcpy(ls_send, name);
strcat(ls_send, ": ");
strcat(ls_send, send_buf);
freopen("rr.txt","a",stdout);
printf("%s\n",ls_send);
freopen("CON","a",stdout); while(online_using);
online_using = 1; for(auto i = online.begin();i != online.end();i++)
send(*i, ls_send, SEND_LEN, 0); online_using = 0;
//cout<<"aaas\n";
}void flash_online_num() {
static char title[100];
if(online.empty()) strcpy(title, "title=bb-xjrjyy-Server");
else sprintf(title, "title=bb-xjrjyy-Server online_now:: %d", online.size());
system(title);
}DWORD WINAPI child_thread(LPVOID V_sock) {
SOCKET hsock = (SOCKET) V_sock;
char pwd[PWD_LEN],pwdtmp[PWD_LEN],mml[MML_LEN];
char recvbuf[STR_MAX_LEN],tmpbuf[NAME_LEN+10],tmpcd[NAME_LEN+105];
bool fa=0,ydl=1;
int fhz=1;
while(ydl) {
while(online_using);
online_using = 1;
bool zcf=0,dlf=0;
fhz=recv(hsock, mml, MML_LEN, 0);
if(strcmp(mml,"tc")==0/*strcmp(mml,"zc")==0||strcmp(mml,"dl")==0*/) {
fhz=recv(hsock, mml, MML_LEN, 0);
}
if(strcmp(mml,"zc")==0) {
zcf=1;
}
if(strcmp(mml,"dl")==0) {
dlf=1;
}
fhz=recv(hsock, name, NAME_LEN, 0);
fhz=recv(hsock, pwd, PWD_LEN, 0);
//for(int iz=0;iz<strlen(pwd);iz++)cout<<int(pwd[iz])<<" ";
//cout<<strlen(pwd);
//cout<<1<<mml<<2<<name<<3<<pwd<<endl;
sprintf(tmpcd,"%s%s.txt",ml,name);
if(zcf) {
freopen(tmpcd,"r",stdin);
if(scanf("%s",pwdtmp)!=-1&&strcmp(pwdtmp,"")!=0){
send(hsock, aznok, strlen(aznok)+1, 0);
fclose(stdin);
online_using = 0;
continue;
}
fclose(stdin);
fp=fopen(tmpcd,"w");
fprintf(fp,"%s",pwd);
fclose(fp);
//freopen(tmpcd,"w",stdout);
//printf("%s",pwd);
//freopen("CON","a",stdout);
printf("%s register Successful!\n", name);
send(hsock, azok, strlen(azok)+1, 0);
//return 0;
online_using = 0;
continue;
} else
if(dlf) {
//send(hsock, aok, strlen(aok), 0);
freopen(tmpcd,"r",stdin);
if(scanf("%s",pwdtmp)==-1||strcmp(pwdtmp,"")==0) {
send(hsock, anok2, strlen(anok2)+1, 0);
fclose(stdin);
online_using = 0;
continue;
}
fclose(stdin);
if(strcmp(pwd,pwdtmp)==0) {
online_using = 0;
send(hsock, aok, strlen(aok), 0);
} else {
online_using = 0;
send(hsock, anok, strlen(anok)+1, 0);
continue;
}
} else {
online_using = 0;
continue;
}
//recv(SockFrom, recvbuf, STR_MAX_LEN, 0);
online.push_back(hsock);
//cout<<online.size()<<endl;
online_using = 0;
ydl=0;
}
show_time();
printf("%s login Successful!\n", name);
flash_online_num();
while(true) {
fhz=recv(hsock, mml, MML_LEN, 0);
if(!strcmp(recvbuf, "ord::EXIT")||fhz==0||fhz==SOCKET_ERROR) break;
if(strcmp(mml,"lt")==0) {
fhz=recv(hsock, recvbuf, STR_MAX_LEN, 0);
if(!strcmp(recvbuf, "ord::EXIT")||fhz==0||fhz==SOCKET_ERROR) break;
broadcast(name, recvbuf);
} else {
if(strcmp(mml,"tc")==0/*strcmp(mml,"zc")==0||strcmp(mml,"dl")==0*/) {
fa=1;break;
}
}
Sleep(10);
}
//cout<<fhz<<endl;
online_using = 1;
online.remove(hsock);
//cout<<online.size()<<endl;
online_using = 0; show_time();
printf("%s exited!\n", name);
flash_online_num();
if(fa) child_thread((LPVOID)hsock);
return 0;
}void NewHandle(SOCKET &SockFrom) {
HANDLE ls_handle;
DWORD ls_handle_id;
ls_handle = (HANDLE)::CreateThread(NULL, 0,
child_thread, (LPVOID)SockFrom, 0, &ls_handle_id);
}
int main() {
system("chcp 936>nul");
system("title=bb-xjrjyy-Server");
puts("bb-xjrjyy-Server");
puts(" powerd by xjrjyy"); WSADATA wsd;
WSAStartup(MAKEWORD(2, 2), &wsd);
SOCKET SockServer;
sockaddr_in ServerAddr, FromAddr; ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(DK);
ServerAddr.sin_addr.S_un.S_addr = inet_addr(Server_IP); SockServer = socket(AF_INET, SOCK_STREAM, 0); if(bind(SockServer, (sockaddr*)&ServerAddr, sizeof(ServerAddr)) == SOCKET_ERROR)
Wrong_exit("bind", 1); if(listen(SockServer, MAX_ONLINE) == SOCKET_ERROR)
Wrong_exit("listen", 2); int Socklen = sizeof(sockaddr);
while(true) {
SOCKET SockFrom;
SockFrom = accept(SockServer, (sockaddr*)&FromAddr, &Socklen);
if(SockFrom != INVALID_SOCKET) {
if(online.size() < MAX_ONLINE) {
NewHandle(SockFrom);
} else {
send(SockFrom, Warn_Buf, strlen(Warn_Buf), 0);
closesocket(SockFrom);
}
}
Sleep(10);
}
WSACleanup();
fclose(stdout);
return 0;
}
(服务端)
#include <io.h>
#include <iostream>
#include <fstream>
#include <windows.h>
#include <thread>
#include <cstdio>
#include <cstring>
#include <list>
#include <time.h>
#pragma comment (lib,"ws2_32.lib")
#define STR_MAX_LEN 200
#define NAME_LEN 20
#define PWD_LEN 16
#define MML_LEN 10
#define SEND_LEN STR_MAX_LEN + NAME_LEN + 2
#define DK 5327
using namespace std;ofstream Sf;
const int MAX_ONLINE = 20;
const char Server_IP[] = "172.17.0.3";//"127.0.0.1";
const char Warn_Buf[] = "It's over Max connect!",
aok[2]="0",
anok[2]="1",
anok2[2]="2",
azok[2]="0",
aznok[2]="1",
ml[100]="";
list < SOCKET > online;
bool online_using = 0;
char name[NAME_LEN];
FILE *fp,*fp2;
void NewHandle(SOCKET &SockFrom);
void Wrong_exit(const char* Error_place, int _Code) {
printf("%s wrong!(at: %d)\n", Error_place, _Code);
system("pause");
exit(_Code);
}void show_time() {
static char time_l[20], time_n[20];
static time_t timep;
{
time(&timep);
strftime(time_n, sizeof(time_n), "%Y-%m-%d %H:%M", localtime(&timep));
if (strcmp(time_l, time_n)) {
strcpy(time_l, time_n);
printf("// %s\n", time_n);
}
}
}void broadcast(const char* name, const char* send_buf) {
static char ls_send[SEND_LEN]; strcpy(ls_send, name);
strcat(ls_send, ": ");
strcat(ls_send, send_buf);
freopen("rr.txt","a",stdout);
printf("%s\n",ls_send);
freopen("CON","a",stdout); while(online_using);
online_using = 1; for(auto i = online.begin();i != online.end();i++)
send(*i, ls_send, SEND_LEN, 0); online_using = 0;
//cout<<"aaas\n";
}void flash_online_num() {
static char title[100];
if(online.empty()) strcpy(title, "title=bb-xjrjyy-Server");
else sprintf(title, "title=bb-xjrjyy-Server online_now:: %d", online.size());
system(title);
}DWORD WINAPI child_thread(LPVOID V_sock) {
SOCKET hsock = (SOCKET) V_sock;
char pwd[PWD_LEN],pwdtmp[PWD_LEN],mml[MML_LEN];
char recvbuf[STR_MAX_LEN],tmpbuf[NAME_LEN+10],tmpcd[NAME_LEN+105];
bool fa=0,ydl=1;
int fhz=1;
while(ydl) {
while(online_using);
online_using = 1;
bool zcf=0,dlf=0;
fhz=recv(hsock, mml, MML_LEN, 0);
if(strcmp(mml,"tc")==0/*strcmp(mml,"zc")==0||strcmp(mml,"dl")==0*/) {
fhz=recv(hsock, mml, MML_LEN, 0);
}
if(strcmp(mml,"zc")==0) {
zcf=1;
}
if(strcmp(mml,"dl")==0) {
dlf=1;
}
fhz=recv(hsock, name, NAME_LEN, 0);
fhz=recv(hsock, pwd, PWD_LEN, 0);
//for(int iz=0;iz<strlen(pwd);iz++)cout<<int(pwd[iz])<<" ";
//cout<<strlen(pwd);
//cout<<1<<mml<<2<<name<<3<<pwd<<endl;
sprintf(tmpcd,"%s%s.txt",ml,name);
if(zcf) {
freopen(tmpcd,"r",stdin);
if(scanf("%s",pwdtmp)!=-1&&strcmp(pwdtmp,"")!=0){
send(hsock, aznok, strlen(aznok)+1, 0);
fclose(stdin);
online_using = 0;
continue;
}
fclose(stdin);
fp=fopen(tmpcd,"w");
fprintf(fp,"%s",pwd);
fclose(fp);
//freopen(tmpcd,"w",stdout);
//printf("%s",pwd);
//freopen("CON","a",stdout);
printf("%s register Successful!\n", name);
send(hsock, azok, strlen(azok)+1, 0);
//return 0;
online_using = 0;
continue;
} else
if(dlf) {
//send(hsock, aok, strlen(aok), 0);
freopen(tmpcd,"r",stdin);
if(scanf("%s",pwdtmp)==-1||strcmp(pwdtmp,"")==0) {
send(hsock, anok2, strlen(anok2)+1, 0);
fclose(stdin);
online_using = 0;
continue;
}
fclose(stdin);
if(strcmp(pwd,pwdtmp)==0) {
online_using = 0;
send(hsock, aok, strlen(aok), 0);
} else {
online_using = 0;
send(hsock, anok, strlen(anok)+1, 0);
continue;
}
} else {
online_using = 0;
continue;
}
//recv(SockFrom, recvbuf, STR_MAX_LEN, 0);
online.push_back(hsock);
//cout<<online.size()<<endl;
online_using = 0;
ydl=0;
}
show_time();
printf("%s login Successful!\n", name);
flash_online_num();
while(true) {
fhz=recv(hsock, mml, MML_LEN, 0);
if(!strcmp(recvbuf, "ord::EXIT")||fhz==0||fhz==SOCKET_ERROR) break;
if(strcmp(mml,"lt")==0) {
fhz=recv(hsock, recvbuf, STR_MAX_LEN, 0);
if(!strcmp(recvbuf, "ord::EXIT")||fhz==0||fhz==SOCKET_ERROR) break;
broadcast(name, recvbuf);
} else {
if(strcmp(mml,"tc")==0/*strcmp(mml,"zc")==0||strcmp(mml,"dl")==0*/) {
fa=1;break;
}
}
Sleep(10);
}
//cout<<fhz<<endl;
online_using = 1;
online.remove(hsock);
//cout<<online.size()<<endl;
online_using = 0; show_time();
printf("%s exited!\n", name);
flash_online_num();
if(fa) child_thread((LPVOID)hsock);
return 0;
}void NewHandle(SOCKET &SockFrom) {
HANDLE ls_handle;
DWORD ls_handle_id;
ls_handle = (HANDLE)::CreateThread(NULL, 0,
child_thread, (LPVOID)SockFrom, 0, &ls_handle_id);
}
int main() {
system("chcp 936>nul");
system("title=bb-xjrjyy-Server");
puts("bb-xjrjyy-Server");
puts(" powerd by xjrjyy"); WSADATA wsd;
WSAStartup(MAKEWORD(2, 2), &wsd);
SOCKET SockServer;
sockaddr_in ServerAddr, FromAddr; ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(DK);
ServerAddr.sin_addr.S_un.S_addr = inet_addr(Server_IP); SockServer = socket(AF_INET, SOCK_STREAM, 0); if(bind(SockServer, (sockaddr*)&ServerAddr, sizeof(ServerAddr)) == SOCKET_ERROR)
Wrong_exit("bind", 1); if(listen(SockServer, MAX_ONLINE) == SOCKET_ERROR)
Wrong_exit("listen", 2); int Socklen = sizeof(sockaddr);
while(true) {
SOCKET SockFrom;
SockFrom = accept(SockServer, (sockaddr*)&FromAddr, &Socklen);
if(SockFrom != INVALID_SOCKET) {
if(online.size() < MAX_ONLINE) {
NewHandle(SockFrom);
} else {
send(SockFrom, Warn_Buf, strlen(Warn_Buf), 0);
closesocket(SockFrom);
}
}
Sleep(10);
}
WSACleanup();
fclose(stdout);
return 0;
}
(服务端)
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货