很水的MyChat2.0
  • 板块灌水区
  • 楼主hdkghc
  • 当前回复21
  • 已保存回复21
  • 发布时间2021/11/27 10:36
  • 上次更新2023/11/3 23:30:05
查看原帖
很水的MyChat2.0
346134
hdkghc楼主2021/11/27 10:36
// SocketBase.h
// Copyleft (c) peitongxin@outlook.com All rights Resurved.
// Luogu: hdkghc
// Phone: 187 58** **14

// In Web Chat核心源码 切勿随意更改

// TCP/IP Proto

// Ninghai, Zhejiang

// V2.0

/* Next Version:
 * 		V2.1
 *		启用文件读入输入端口
 *		以便因防火墙阻止而连接失败。
*/ 

#ifndef __SOCKET_H_XPT__
#define __SOCKET_H_XPT__
#include <sys/types.h>
#include <ws2tcpip.h>
#include <sys/stat.h>
#include <winsock2.h>
#include <windows.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fstream>
#include <conio.h>
#include <errno.h>
#include <fcntl.h>
#include <thread>
#pragma comment(lib, "wsock32.lib")
#define PORT 6666
#define SENDLEN 4096
using namespace std;
class SOCKETBASE{
	protected:
	 	SOCKET sock;
	 	SOCKET csock;
	 	sockaddr_in tsi;
	 	sockaddr_in csi;
	 	sockaddr_in ssi;
		WSADATA wsd;
		CONSOLE_SCREEN_BUFFER_INFO csbi;
		void GotoXY(int x, int y)
		{
			GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
			COORD coord = {x, y};
			SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
		}
		void BacktoXY()
		{
			SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), csbi.dwCursorPosition);
		}
		//Thread
		void Read()
		{
			
		}
		void Write()
		{
			
		}
		
		void MessageRound()
		{/*
			thread tRe(Read);
			thread tWr(Write);
			if(tRe.joinable() && tWr.joinable())
			{
				tRe.join();
				tWr.join();
			}*/
		}
	public:
		SOCKETBASE()
		{
			ifstream fin;
			fin.open("./MyChat.data/FirstUse.ini");
			int x;
			fin >> x;
			if(x == 1)
			{
				printf("检测到您是第一次使用,请输入你的昵称:");
				char name[256];
				scanf("%s", name);
				fin.close();
				ofstream fout;
				fout.open("./MyChat.data/FirstUse.ini");
				fout << 0;
				fout.close();
				fout.open("./MyChat.data/name.ini");
				fout << name;
				fout.close(); 
			}
			WSAStartup(MAKEWORD(1, 1), &wsd);
			sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
		}
		void bind()
		{
			memset(&tsi, 0, sizeof(tsi));
			tsi.sin_family = AF_INET;
			tsi.sin_port = htons(PORT);
			tsi.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
			::bind(sock, (struct sockaddr *)&tsi, sizeof(tsi));
		}
		void listen()
		{
			::listen(sock, 5);
		}
		void connect(char *ip)
		{
			memset(&ssi, 0, sizeof(ssi));
			ssi.sin_family = AF_INET;
			ssi.sin_port = htons(PORT);
			ssi.sin_addr.S_un.S_addr = htonl(ip);
			::connect(csock, (struct sockaddr *)&ssi, sizeof(ssi));
			char namebuff[256];
			::recv(csock, namebuff, 256, 0);
			ifstream fin;
			char name[256];
			fin.open("./MyChat.data/name.ini");
			fin >> name;
			fin.close();
			::send(csock, name, 256, 0);
			fin.open("./MyChat.data/friend.ini");
			char friends[256];
			while(fin.good())
			{
				fin >> friends;
				if(strcmp(friends, namebuff) == 0)
				{
					break;
				}
			}
			fin.close();
			printf("检测到你和%s不是好友,要发送好友请求吗?(弹窗选择)", namebuff);
			Sleep(1000);
			int choose = IDYES;
			if(strcmp(friends, namebuff) != 0) choose = MessageBox(NULL, "要发送好友请求吗?", "弹窗选择", MB_YESNO|MB_ICONQUESTION);
			if(choose == IDYES)
			{
				printf("正在发送...");
				::send(csock, "```con```", 10, 0);
				ofstream fout;
				fout.open("./MyChat.data/friend.ini", ios_base::out | ios_base::ate);
				fout << namebuff << endl;
				fout.close();
			}
			else
			{
				::send(csock, "```end```", 10, 0);
				closesocket(csock);
			}
		}
		void accept()
		{
			csock = ::accept(sock, (struct sockaddr*)&csi, sizeof(csi));
			if(csock == -1) return;
			ifstream fin;
			fin.open("./MyChat.data/name.ini");
			char name[256], namebuff[256];
			fin >> name;
			::send(csock, name, 256, 0);
			::recv(csock, namebuff, 256, 0);
			fin.close();
			fin.open("./MyChat.data/friend.ini");
			char friends[256];
			while(fin.good())
			{
				fin >> friends;
				if(strcmp(friends, namebuff) == 0)
				{
					break;
				}
			}
			fin.close();
			if(strcmp(friends, namebuff) != 0)
			{
				char cmd[10];
				::recv(csock, cmd, 10, 0);
				if(cmd == "```end```")
				{
					closesocket(csock);
					return;
				}
				if(cmd == "```con```")
				{
					ofstream fout;
					fout.open("./MyChat.data/friend.ini", ios_base::out | ios_base::ate);
					fout << namebuff << endl;
					fout.close();
					this->MessageRound();
					closesocket(csock);
					return;
				}
			}
		}
		void send(const char *str)
		{
			::send(csock, str, SENDLEN, 0);
		}
		char* get(char *str = NULL)
		{
			char buff[SENDLEN];
			::recv(csock, buff, SENDLEN, 0);
			if(str != NULL) str = buff;
			return (char*)buff;
		}
};
#endif

2021/11/27 10:36
加载中...