C++// 编译:g++ client.cpp -o client.exe -std=c++11 -lws2_32
// 兼容:Windows 7/8/10/11 | 编码:GBK
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#endif
#include <iostream>
#include <winsock2.h>
#include <thread>
#include <mutex>
#include <string>
#include <conio.h>
#include <windows.h>
#include <regex>
using namespace std;
#define kd(x) (GetAsyncKeyState(x)&0x8000)
HANDLE hCon; mutex mtx;
const int COL = 90, LINE = 30, INPUT_LINE = 27, STATUS_LINE = 28, MSG_START = 3, MSG_END = 26;
int msgLine = MSG_START;
SOCKET sock; string userName;
string ann = ""; int myColor = 10;
bool run = true;
const int COL_TITLE = 11, COL_ANN = 12, COL_SELF = 10, COL_OTHER = 9, COL_SYS = 14, COL_NORMAL = 7;
void color(int c) { SetConsoleTextAttribute(hCon, c); }
void gotoxy(int x, int y) { COORD p = {(short)x, (short)y}; SetConsoleCursorPosition(hCon, p); }
void HideCursor() { CONSOLE_CURSOR_INFO c = {1,0}; SetConsoleCursorInfo(hCon, &c); }
void ShowCursor() { CONSOLE_CURSOR_INFO c = {1,1}; SetConsoleCursorInfo(hCon, &c); }
void noedit() {
HANDLE h = GetStdHandle(STD_INPUT_HANDLE); DWORD m;
GetConsoleMode(h,&m); m &= ~ENABLE_QUICK_EDIT_MODE; SetConsoleMode(h,m);
}
void setFont(int size) {
if (size<8||size>32) return;
CONSOLE_FONT_INFOEX f = {sizeof(f)};
f.dwFontSize.Y = size; wcscpy_s(f.FaceName, L"SimSun");
SetCurrentConsoleFontEx(hCon, 0, &f);
}
void fontSize() {
static int s = 16;
if (kd(VK_CONTROL) && kd(VK_ADD)) { s+=2; setFont(s); Sleep(200); }
if (kd(VK_CONTROL) && kd(VK_SUBTRACT)) { s-=2; setFont(s); Sleep(200); }
if (kd(VK_CONTROL) && kd('0')) { s=16; setFont(s); Sleep(200); }
}
void drawBorder() {
system("cls");
color(COL_TITLE);
cout << "=======================================================" << endl;
cout << " 机房联机聊天室" << endl;
cout << "=======================================================" << endl;
gotoxy(0,MSG_END+1); cout << "=======================================================" << endl;
gotoxy(0,STATUS_LINE); cout << "=======================================================" << endl;
color(COL_NORMAL);
}
void showAnn() {
lock_guard<mutex> lock(mtx);
gotoxy(8,2); color(COL_ANN); cout << string(75,' '); gotoxy(8,2);
if (!ann.empty()) cout << ann;
color(COL_NORMAL);
gotoxy(10, INPUT_LINE);
}
void print(const string& txt, int c) {
lock_guard<mutex> lock(mtx);
if (msgLine > MSG_END) { drawBorder(); msgLine = MSG_START; }
gotoxy(2, msgLine); color(c); cout << string(85,' '); gotoxy(2, msgLine);
cout << txt; msgLine++; color(COL_NORMAL);
gotoxy(10, INPUT_LINE);
}
bool cmd(const string& c) {
if (c.find("/color ") == 0) { myColor = atoi(c.substr(7).c_str()); print("已设置发送颜色", COL_SELF); return 1; }
if (c == "/colorlist") { print("颜色表:0-15 分别为黑蓝绿青红紫黄白 高亮色", COL_SELF); return 1; }
if (c == "/clear") { drawBorder(); msgLine = MSG_START; return 1; }
if (c == "/help") { print("命令:/color 数字 /colorlist /clear /help", COL_TITLE); return 1; }
return 0;
}
void recvThread() {
char buf[2048] = {0};
while (run) {
memset(buf,0,2048);
int r = recv(sock, buf, 2048, 0);
if (r<=0) { print("与服务端断开连接", COL_SYS); run=0; break; }
string m = buf;
if (m.find("[ANN]") == 0) { ann = m.substr(5); showAnn(); continue; }
if (m == "[ANN_CLR]") { ann = ""; showAnn(); continue; }
if (m.find("[SYS]") == 0) { print(m.substr(5), COL_SYS); continue; }
regex re(R"(\[COL:(\d+)\](.*))"); smatch ma;
if (regex_search(m,ma,re)) print(ma[2], atoi(ma[1].str().c_str()));
else print(m, COL_OTHER);
}
}
int main() {
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
system("title 机房聊天客户端");
system("mode con cols=90 lines=30");
noedit(); setFont(16); drawBorder();
string ip; int p;
gotoxy(2,5); cout << "服务端IP:"; cin >> ip;
gotoxy(2,6); cout << "端口号(8888):"; cin >> p;
gotoxy(2,7); cout << "你的昵称:"; cin >> userName;
cin.ignore(); drawBorder();
WSADATA wsa;
WSAStartup(MAKEWORD(2,2), &wsa);
sock = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(p);
addr.sin_addr.s_addr = inet_addr(ip.c_str());
if (connect(sock, (sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) {
print("连接服务端失败", COL_SYS); system("pause"); return 0;
}
send(sock, userName.c_str(), userName.size(), 0);
thread(recvThread).detach();
print("欢迎你," + userName, COL_SELF);
string input;
while (run) {
fontSize(); ShowCursor(); gotoxy(10, INPUT_LINE);
if (_kbhit()) {
char c = _getch();
if (c == 27) { print("已退出聊天室", COL_SYS); run=0; break; }
if (c == '\r' && !input.empty()) {
if (!cmd(input)) {
string msg = "[COL:" + to_string(myColor) + "][" + userName + "] " + input;
send(sock, msg.c_str(), msg.size(), 0);
print("[" + userName + "] " + input, myColor);
}
input.clear(); gotoxy(10,INPUT_LINE); cout << string(75,' '); gotoxy(10,INPUT_LINE);
} else if (c == '\b' && !input.empty()) { input.pop_back(); cout << "\b \b";
} else if (c>=32 && c<=126 && input.size()<70) { input += c; cout << c; }
}
Sleep(10);
}
closesocket(sock); WSACleanup();
return 0;
}