#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);
}
void Read()
{
}
void Write()
{
}
void MessageRound()
{
}
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