#pragma hdrstop
#include "Unit1.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
#define Q 50
#define SIZE 2048
TForm1 *Form1;
HANDLE hMainThread;
HANDLE hGetMes;
WSADATA WSAData;
SOCKET WinSocket;
sockaddr_in SockStruct;
int Bind;
int Listen;
int Connect;
int Send;
int Recv;
AnsiString List = "";
struct User
{
bool active;
int Accept;
sockaddr_in AcpStruct;
int size;
DWORD id;
HANDLE hRecv;
AnsiString IP;
AnsiString Nick;
};
User user[Q];
void SendMsg()
{
int i = Form1->ListBox1->ItemIndex;
if (i >= 0)
{
AnsiString Message = "1";
Message += Form1->Edit4->Text;
Message += "#";
Message += Form1->ListBox1->Items->Strings[i];
Message += "#";
Message += Form1->Memo2->Text;
if (Form1->CheckBox1->Checked == true)
{
for (int j=0; jif (user[j].Nick == Form1->ListBox1->Items->Strings[i])
Send = send(user[j].Accept, Message.c_str(), Message.Length() + 1, 0);
}
else
Send = send(WinSocket, Message.c_str(), Message.Length() + 1, 0);
if (Send == SOCKET_ERROR)
{
Form1->Memo1->Lines->Add("XATO:Mumkin emas xabarni jo`natish!");
Form1->Memo1->Lines->Add("");
}
else
{
Form1->Memo1->Lines->Add("Xabar uchun '" + Form1->ListBox1->Items->Strings[i] + "':");
Form1->Memo1->Lines->Add(Form1->Memo2->Text);
Form1->Memo1->Lines->Add("");
Form1->Memo2->Clear();
}
}
else
{
Form1->Memo1->Lines->Add("XATO: Xabarni manzili tanlanmagan!");
Form1->Memo1->Lines->Add("");
}
}
void WINAPI ParserServer(int n)
{
char Buffer[SIZE];
while (true)
{
for (int i=0; iBuffer[i] = '\0';
Recv = recv(user[n].Accept, Buffer, SIZE, 0);
if (Recv == SOCKET_ERROR)
{
List = "2";
List += Form1->Edit4->Text;
List += "#";
user[n].active = false;
Form1->ListBox1->Items->Clear();
for (int i=0; i
if (user[i].active == true)
Form1->ListBox1->Items->Add(user[i].Nick);
for (int i=0; i
if (user[i].active == true)
{
List += user[i].Nick;
List += "#";
}
for (int i=0; i
if (user[i].active == true)
send(user[i].Accept, List.c_str(), List.Length() + 1, 0);
CloseHandle(user[n].hRecv);
closesocket(user[n].Accept);
Form1->Memo1->Lines->Add("Foydalanuvchi '" + user[n].Nick + "' dan ketdi...");
Form1->Memo1->Lines->Add("");
if (Form1->ListBox1->Items->Count == 0)
Form1->Button7->Enabled = false;
break;
}
else
{
if (Buffer[0] == '1')
{
AnsiString From = "";
AnsiString To = "";
AnsiString Message = Buffer;
Message = Message.Delete(1,1);
AnsiString Text = "";
int k = 0;
for (int i=1; i
if (Message[i] == '#')
k++;
else
if (k == 0)
From += Message[i];
else
if (k == 1)
To += Message[i];
else
if (k == 2)
Text += Message[i];
if (To == Form1->Edit4->Text)
{
Form1->Memo1->Lines->Add("Xabar '" + From + "':");
Form1->Memo1->Lines->Add(Text);
Form1->Memo1->Lines->Add("");
}
else
{
for (int i=0; iif (user[i].Nick == To)
Send = send(user[i].Accept, Buffer, strlen(Buffer) + 1, 0);
}
}
if (Buffer[0] == '6')
{
AnsiString Nick = "";
Nick = Buffer;
Nick = Nick.Delete(1,1);
user[n].Nick = Nick;
List = "2";
List += Form1->Edit4->Text;
List += "#";
for (int i=0; i
if (user[i].active == true)
{
List += user[i].Nick;
List += "#";
}
Form1->ListBox1->Clear();
for (int i=0; i
if (user[i].active == true)
Form1->ListBox1->Items->Add(user[i].Nick);
for (int i=0; i
if (user[i].active == true)
send(user[i].Accept, List.c_str(), List.Length() + 1, 0);
Form1->Memo1->Lines->Add("Foydalanuvchiga '" + user[n].Nick + "' keldi...");
Form1->Memo1->Lines->Add("");
if (Form1->ListBox1->Items->Count > 0)
{
Form1->Button1->Enabled = true;
Form1->Button7->Enabled = true;
}
else
{
Form1->Button1->Enabled = false;
Form1->Button7->Enabled = true;
}
}
}
}
}
void WINAPI ParserClient()
{
char Buffer[SIZE];
while (true)
{
for (int i=0; iBuffer[i] = '\0';
Recv = recv(WinSocket, Buffer, SIZE, 0);
if (Recv == SOCKET_ERROR)
{
Form1->Memo1->Lines->Add("Serverdan chiqarildi!");
Form1->Button4->Enabled = true;
Form1->Edit2->Enabled = true;
Form1->Edit3->Enabled = true;
Form1->Edit4->Enabled = true;
Form1->CheckBox1->Enabled = true;
Form1->ListBox1->Clear();
break;
}
else
{
if (Buffer[0] == '1')
{
AnsiString From = "";
AnsiString To = "";
AnsiString Message = Buffer;
Message = Message.Delete(1,1);
AnsiString Text = "";
int k = 0;
for (int i=1; i
if (Message[i] == '#')
k++;
else
if (k == 0)
From += Message[i];
else
if (k == 1)
To += Message[i];
else
if (k == 2)
Text += Message[i];
Form1->Memo1->Lines->Add("Xabar '" + From + "':");
Form1->Memo1->Lines->Add(Text);
Form1->Memo1->Lines->Add("");
}
if (Buffer[0] == '2')
{
Form1->ListBox1->Clear();
AnsiString Contacts = Buffer;
Contacts = Contacts.Delete(1,1);
AnsiString Nicks = "";
for (int i=1; i
if (Contacts[i] == '#')
{
if (Nicks != Form1->Edit4->Text)
Form1->ListBox1->Items->Add(Nicks);
Nicks = "";
}
else
Nicks += Contacts[i];
if (Form1->ListBox1->Items->Count > 0)
Form1->Button1->Enabled = true;
else
Form1->Button1->Enabled = false;
}
}
}
}
DWORD WINAPI MainThread()
{
Form1->Button4->Enabled = false;
Form1->Edit2->Enabled = false;
Form1->Edit3->Enabled = false;
Form1->Edit4->Enabled = false;
Form1->CheckBox1->Enabled = false;
if (Form1->CheckBox1->Checked == true)
{
if (WSAStartup(MAKEWORD(2,1), &WSAData) != 0)
{
Form1->Memo1->Lines->Add("XATO:Mumkin emas soket tashabuskormas!");
Form1->Memo1->Lines->Add("");
Form1->Button4->Enabled = true;
Form1->Edit2->Enabled = true;
Form1->Edit3->Enabled = true;
Form1->Edit4->Enabled = true;
Form1->CheckBox1->Enabled = true;
return 0;
}
else
{
WinSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (WinSocket == SOCKET_ERROR)
{
Form1->Memo1->Lines->Add("XATO: soket yaratib bo`lmaydi!");
Form1->Memo1->Lines->Add("");
Form1->Button4->Enabled = true;
Form1->Edit2->Enabled = true;
Form1->Edit3->Enabled = true;
Form1->Edit4->Enabled = true;
Form1->CheckBox1->Enabled = true;
return 0;
}
else
{
SockStruct.sin_family = AF_INET;
SockStruct.sin_port = StrToInt(Form1->Edit3->Text);
SockStruct.sin_addr.s_addr = INADDR_ANY;
Bind = bind(WinSocket, (sockaddr*)&SockStruct, sizeof(SockStruct));
if (Bind == SOCKET_ERROR)
{
Form1->Memo1->Lines->Add("XATO:Mumkin emas mahalliy tarmoq manzili va soketda!");
Form1->Memo1->Lines->Add("");
Form1->Button4->Enabled = true;
Form1->Edit2->Enabled = true;
Form1->Edit3->Enabled = true;
Form1->Edit4->Enabled = true;
Form1->CheckBox1->Enabled = true;
return 0;
}
else
{
Listen = listen(WinSocket, 5);
if (Listen == SOCKET_ERROR)
{
Form1->Memo1->Lines->Add("XATO:Mumkin emas yaratish soketda cheklangan!");
Form1->Memo1->Lines->Add("");
Form1->Button4->Enabled = true;
Form1->Edit2->Enabled = true;
Form1->Edit3->Enabled = true;
Form1->Edit4->Enabled = true;
Form1->CheckBox1->Enabled = true;
return 0;
}
else
{
Form1->Memo1->Lines->Add("Iltimoz kuting yoqilayabdi...");
Form1->Memo1->Lines->Add("");
Form1->Button5->Enabled = true;
for (int i=0; i
{
user[i].active = false;
user[i].size = sizeof(user[i].AcpStruct);
user[i].IP = "";
user[i].Nick = "";
}
int num = 0;
List = "";
while (true)
{
for (int i=Q; i>0; i--)
if (user[i].active == false)
num = i;
user[num].Accept = accept(WinSocket, (sockaddr*)&user[num].AcpStruct, (int*)&user[num].size);
if (user[num].Accept == SOCKET_ERROR)
{
Form1->Memo1->Lines->Add("XATO:Mumkin emas yoqish qabul qilinmagan");
Form1->Memo1->Lines->Add("");
}
else
{
user[num].active = true;
user[num].hRecv = CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)ParserServer,
(void*)num,
0,
&user[num].id);
}
}
}
}
}
}
}
else
{
if (WSAStartup(MAKEWORD(2,1), &WSAData) != 0)
{
Form1->Memo1->Lines->Add("XATO:Mumkin emas soket tashabuskormas!");
Form1->Memo1->Lines->Add("");
Form1->Button4->Enabled = true;
Form1->Edit2->Enabled = true;
Form1->Edit3->Enabled = true;
Form1->Edit4->Enabled = true;
Form1->CheckBox1->Enabled = true;
return 0;
}
else
{
WinSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (WinSocket == SOCKET_ERROR)
{
Form1->Memo1->Lines->Add("XATO: soket yaratilmaydi!");
Form1->Memo1->Lines->Add("");
Form1->Button4->Enabled = true;
Form1->Edit2->Enabled = true;
Form1->Edit3->Enabled = true;
Form1->Edit4->Enabled = true;
Form1->CheckBox1->Enabled = true;
return 0;
}
else
{
SockStruct.sin_family = AF_INET;
SockStruct.sin_port = StrToInt(Form1->Edit3->Text);
SockStruct.sin_addr.s_addr =inet_addr(AnsiString(Form1->Edit2->Text).c_str());
Form1->Memo1->Lines->Add("Yoqilmoqda...");
Form1->Memo1->Lines->Add("");
Connect = connect(WinSocket, (sockaddr*)&SockStruct, sizeof(SockStruct));
if (Connect == SOCKET_ERROR)
{
Form1->Memo1->Lines->Add("XATO: Server yoqilmagan!");
Form1->Memo1->Lines->Add("");
Form1->Button4->Enabled = true;
Form1->Edit2->Enabled = true;
Form1->Edit3->Enabled = true;
Form1->Edit4->Enabled = true;
Form1->CheckBox1->Enabled = true;
return 0;
}
else
{
Form1->Memo1->Lines->Add("Yoqildi!");
Form1->Memo1->Lines->Add("");
Form1->Button5->Enabled = true;
DWORD idgm;
hGetMes = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ParserClient,NULL,0,&idgm);
AnsiString Mynick = "6";
Mynick += Form1->Edit4->Text;
send(WinSocket, Mynick.c_str(), Mynick.Length() + 1, 0);
}
}
}
}
return 1;
}
void Stop()
{
if (Form1->CheckBox1->Checked == true)
{
int code1;
int code2;
for (int i=0; i
if (user[i].active == true)
{
TerminateThread(user[i].hRecv, code1);
closesocket(user[i].Accept);
}
TerminateThread(hMainThread, code2);
closesocket(WinSocket);
Form1->ListBox1->Clear();
Form1->Button5->Enabled = false;
Form1->Button4->Enabled = true;
Form1->Edit2->Enabled = true;
Form1->Edit3->Enabled = true;
Form1->Edit4->Enabled = true;
Form1->CheckBox1->Enabled = true;
Form1->Memo1->Lines->Add("Ishlayotgan server to`xtatildi...");
Form1->Memo1->Lines->Add("");
}
else
{
int code1;
int code2;
TerminateThread(hMainThread, code1);
TerminateThread(hGetMes, code2);
closesocket(WinSocket);
Form1->ListBox1->Clear();
Form1->Button5->Enabled = false;
Form1->Button4->Enabled = true;
Form1->Edit2->Enabled = true;
Form1->Edit3->Enabled = true;
Form1->Edit4->Enabled = true;
Form1->CheckBox1->Enabled = true;
Form1->Memo1->Lines->Add("Foal foydalanuvchi chiqdi...");
Form1->Memo1->Lines->Add("");
}
}
void Kick()
{
int j = Form1->ListBox1->ItemIndex;
int code;
for (int i=0; i
if (user[i].Nick == Form1->ListBox1->Items->Strings[j])
{
user[i].active = false;
CloseHandle(user[i].hRecv);
closesocket(user[i].Accept);
}
}
void Exit()
{
if (Form1->CheckBox1->Checked == true)
{
int code1;
int code2;
for (int i=0; i
if (user[i].active == true)
{
TerminateThread(user[i].hRecv, code1);
CloseHandle(user[i].hRecv);
closesocket(user[i].Accept);
}
TerminateThread(hMainThread, code2);
CloseHandle(hMainThread);
closesocket(WinSocket);
Form1->Close();
}
else
{
int code1;
int code2;
TerminateThread(hMainThread, code1);
TerminateThread(hGetMes, code2);
closesocket(WinSocket);
Form1->Close();
}
}
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Form1->Edit1->Text = "";
Form1->Edit2->Text = "127.0.0.1";
Form1->Edit3->Text = 5290;
Form1->Edit4->Text = "User";
Form1->Button1->Enabled = false;
Form1->Button2->Enabled = false;
Form1->Button3->Enabled = false;
Form1->Button5->Enabled = false;
Form1->Button7->Enabled = false;
Form1->Button8->Visible = false;
Form1->Button9->Visible = false;
}
void __fastcall TForm1::Button4Click(TObject *Sender)
{
DWORD id;
hMainThread = CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)MainThread,
NULL,
0,
&id);
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
SendMsg();
}
void __fastcall TForm1::Button6Click(TObject *Sender)
{
Exit();
}
void __fastcall TForm1::Button5Click(TObject *Sender)
{
Stop();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button7Click(TObject *Sender)
{
Kick();
}
//---------------------------------------------------------------------------
Do'stlaringiz bilan baham: