Panie i Panowie, nie mogę dojść do tego.
Jeżeli ktoś miał by chwilkę, to załączam pełne źródła, klienta oraz serwera.
Będę wdzięczny za wskazanie błędu (człowiek uczy się całe życie).
Oto adres:
http://rapidshare.com/files/71508217/ToBylGlupiPomysl.rar.html
Pisane pod Delphi 2007 ( celem kompatybilności z Vista)
Kod Serwera:
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp, DBCtrls, ComCtrls;
type
TForm3 = class(TForm)
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
ListBox1: TListBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Status: TLabel;
ServerSocket1: TServerSocket;
GroupBox1: TGroupBox;
Edit1: TEdit;
Edit2: TEdit;
Polacz: TButton;
Label5: TLabel;
Label6: TLabel;
Button1: TButton;
TabSheet3: TTabSheet;
Memo1: TMemo;
Label7: TLabel;
ListBox2: TListBox;
ListBox3: TListBox;
ListBox4: TListBox;
ListBox5: TListBox;
ListBox6: TListBox;
GroupBox2: TGroupBox;
Button2: TButton;
Button3: TButton;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
Edit7: TEdit;
Edit8: TEdit;
Button4: TButton;
Button5: TButton;
TabSheet4: TTabSheet;
ListBox7: TListBox;
TabSheet5: TTabSheet;
ListBox8: TListBox;
ListBox9: TListBox;
Edit9: TEdit;
procedure PolaczClick(Sender: TObject);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
// Procedury Obslugujace REKORDY !
type
TRekord = record
Login: string[250];
Haslo: string[250];
Hasz: string[250];
Status: string[250];
Komenda: string[250];
Tekst: string[250];
Dofaceta: string[250];
end;
var
Form3: TForm3;
k:TClientSocket;
s:TServerSocket;
Rekord:TRekord;
Odbior: TRekord;
// Procedury Obslugujace REKORDY !
// Procedury Obslugujace REKORDY !
type
TRekordnadawanie = record
Login: string[250];
Haslo: string[250];
Hasz: string[250];
Status: string[250];
Komenda: string[250];
Tekst: string[250];
Dofaceta: string[250];
end;
var
Rekordnadawanie:TRekord;
// Procedury Obslugujace REKORDY !
implementation
{$R *.dfm}
procedure TForm3.Button2Click(Sender: TObject);
begin
listbox1.items.savetofile('ID.txt');
listbox2.items.savetofile('IP.txt');
listbox3.items.savetofile('NICKI.txt');
listbox4.items.savetofile('HASLA.txt');
listbox5.items.savetofile('STATUSY.txt');
listbox5.items.savetofile('KODY.txt');
listbox7.items.savetofile('PORT.txt');
end;
procedure TForm3.Button3Click(Sender: TObject);
begin
listbox1.items.loadfromfile('ID.txt');
listbox2.items.loadfromfile('IP.txt');
listbox3.items.loadfromfile('NICKI.txt');
listbox4.items.loadfromfile('HASLA.txt');
listbox5.items.loadfromfile('STATUSY.txt');
listbox6.items.loadfromfile('KODY.txt');
listbox7.items.loadfromfile('PORT.txt');
end;
procedure TForm3.Button4Click(Sender: TObject);
begin
ListBox1.Items.add(edit3.text);
ListBox2.Items.add(edit4.text);
ListBox3.Items.add(edit5.text);
ListBox4.Items.add(edit6.text);
ListBox5.Items.add(edit7.text);
ListBox6.Items.add(edit8.text);
ListBox7.Items.add(edit9.text);
end;
procedure TForm3.Button5Click(Sender: TObject);
var itdel:integer;
begin
for itdel:=listbox1.items.count-1 downto 0 do
if listbox1.selected[ itdel ] then
begin
listbox1.items.delete(itdel);
listbox2.items.delete(itdel);
listbox3.items.delete(itdel);
listbox4.items.delete(itdel);
listbox5.items.delete(itdel);
listbox6.items.delete(itdel);
listbox7.items.delete(itdel);
end;
end;
procedure TForm3.FormCreate(Sender: TObject);
begin
button3.Click;
Polacz.click;
end;
procedure TForm3.PolaczClick(Sender: TObject);
begin
try
serversocket1.Port:=strtoint(edit1.text);
serversocket1.active:=true;
except
showmessage('Awaria w nasluchu');
end;
end;
procedure TForm3.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
var int: integer;
begin
//Glupia procedura, ukazujacy pieprzony ID klienta ktory sie podlaczyl
Int := 0;
repeat
if ServerSocket1.Socket.Connections[Int].SocketHandle = Socket.SocketHandle then
begin
memo1.lines.add('Użytkownik, ID:'+inttostr(int)+ ' IP : '+Socket.RemoteAddress + ' podlaczył sie. ');
listbox8.Items.add(serverSocket1.socket.connections[Int].RemoteAddress);
listbox9.Items.add(inttostr(serverSocket1.socket.connections[Int].RemotePort));
Exit;
end;
Inc(Int);
until Int = ServerSocket1.Socket.ActiveConnections;
// Glupia procedura, ukazujacy pieprzony ID klienta ktory sie podlaczyl
end;
procedure TForm3.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
var int,int2,int3,Intp,intp4,sexx: integer;
var wybor:integer;
var port:string;
Var portextra:integer;
var kutas:integer;
begin
begin
for wybor:=listbox8.items.count-1 downto 0 do
begin
if (socket.RemoteAddress=listbox8.items[wybor])and (inttostr(socket.RemotePort)=listbox9.items[wybor])
then
begin
listbox8.items.Delete(wybor);
listbox9.items.Delete(wybor);
// port:=listbox9.items[wybor];
// portextra:=strtoint(port);
//kutas:=portextra;
// the best chujnia code
//for Int3 := 0 to ServerSocket1.Socket.ActiveConnections-1 do
//begin
// if (ServerSocket1.Socket.RemoteAddress = listbox8.items[wybor])
// and
// (inttostr(ServerSocket1.Socket.RemotePort) = listbox9.items[wybor])
// then
// begin
// showmessage(' HWDP DLA KLIENTA'+inttostr(int3));
// end;
// the best chujnia code !!!!!!
// listbox9.items.insert(iii,odbior.status);
end;
end;
// Glupia procedura, ukazujacy pieprzony ID klienta ktory sie rozlaczyl
Intp := 0;
repeat
if ServerSocket1.Socket.Connections[Int].SocketHandle = Socket.SocketHandle then
begin
memo1.lines.add('Użytkownik, ID:'+inttostr(int)+ ' IP : '+Socket.RemoteAddress + ' rozlaczyl sie. ');
// Wyjebac mu status ofline - murzynowi buuu
for intp4:=listbox1.items.count-1 downto 0 do
begin
if (socket.RemoteAddress=listbox2.items[intp4])and (inttostr(socket.RemotePort)=listbox7.items[intp4])
then
begin
listbox5.items.Delete(intp4);
listbox5.items.insert(intp4,'Offline');
// listbox9.items.Delete(intp);
// Glupia procedura, ukazujacy pieprzony ID klienta ktory sie podlaczyl
// wyjebac mu status offline - murzynowi jednemu
end;
end;
end;
Inc(Int);
until Int = ServerSocket1.Socket.ActiveConnections;
end;
end;
// Glupia procedura, ukazujacy pieprzony ID klienta ktory sie rozlaczyl
procedure TForm3.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var iii,iiii,iiiii,Intps,Intpss,sexx,bubu,bubu1,kupka1 : integer;
var int,int3,wybor,autoryzowany: integer;
begin
autoryzowany:=0;
// Sprawdzamy czy zalogowal sie poprawnie
// Pobieramy dane z bufora
Socket.ReceiveBuf(Odbior,SizeOf(Odbior));
// Pobralismy
// Teraz kontrola Loginu i Hasla //
if odbior.Komenda='zaloguj' then
try
for iii:=listbox1.items.count-1 downto 0 do
begin
if (odbior.Login=listbox3.items[iii])and (odbior.Haslo=listbox4.items[iii])
then
begin
listbox5.items.Delete(iii);
listbox5.items.insert(iii,odbior.status);
// Nadajemy do klienta potwierdzenie nadania mu statusu
//
begin
for sexx:=listbox1.items.count-1 downto 0 do
begin
Intpss := 0;
repeat
if
(ServerSocket1.Socket.Connections[Intpss].RemoteAddress = listbox2.items[sexx]) and
(inttostr(ServerSocket1.Socket.Connections[Intpss].RemotePort) = listbox7.items[sexx])
then
begin
// serversocket1.Socket.Connections[int].SendBuf(Rekordnadawanie,SizeOf(Rekordnadawanie));
serversocket1.Socket.Connections[intpss].SendBuf(Rekordnadawanie,SizeOf(Rekordnadawanie));
end;
Inc(Intpss);
until Intpss = ServerSocket1.Socket.ActiveConnections;
// koniec nadawania
// IP klajenta
listbox2.items.Delete(iii);
listbox2.items.insert(iii,socket.RemoteAddress);
// ip klajenta
// ID KLIENTA
//Glupia procedura, ukazujacy pieprzony port klienta ktory sie podlaczyl
Int := 0;
repeat
if ServerSocket1.Socket.Connections[Int].SocketHandle = Socket.SocketHandle then
begin
listbox7.items.Delete(iii);
listbox7.Items.insert(iii,inttostr(serverSocket1.socket.connections[Int].RemotePort));
// wysylanie statusu
Rekordnadawanie.Komenda:='LogowaniePoprawne';
serversocket1.Socket.Connections[int].SendBuf(Rekordnadawanie,SizeOf(Rekordnadawanie));
autoryzowany:=1;
// wysylanie statusu
Exit;
end;
Inc(Int);
until Int = ServerSocket1.Socket.ActiveConnections;
// Glupia procedura, ukazujacy pieprzony port klienta ktory sie podlaczyl
// ID klienta
//listbox7.items.Delete(iii);
// listbox7.Items.insert(iii,inttostr(serverSocket1.socket.RemotePort));
memo1.lines.add(' Pomyslnie zalogowano użytkownika: '+ odbior.Login);
memo1.lines.add(' Użytkownik '+ odbior.Login+ ' ustawia swoj stan na: '+odbior.status);
end;
end end
// Odpowiadamy klientowi ze wszystko jest ok
// Glupia procedura, ukazujacy pieprzony ID klienta ktory sie podlaczyl
//Int := 0;
//repeat
// if ServerSocket1.Socket.Connections[Int].SocketHandle = Socket.SocketHandle then
// begin
//Rekordnadawanie.Komenda:='LogowaniePoprawne';
//Rekordnadawanie.Komenda:='Wiadomosc';
//Rekordnadawanie.tekst:='Siema murzynie';
// showmessage('jestem tu');
//serversocket1.Socket.Connections[int].SendBuf(Rekordnadawanie,SizeOf(Rekordnadawanie));
// listbox1.items.Delete(iii);
// listbox1.items.insert(iii,inttostr(int));
// Exit;
// end;
// Inc(Int);
//until Int = ServerSocket1.Socket.ActiveConnections;
// Glupia procedura, ukazujacy pieprzony ID klienta ktory sie podlaczyl
// Odpowiadamy klientowi ze wszystko jest ok
end;
finally
if autoryzowany=1
then
begin
bubu := 0;
repeat
if ServerSocket1.Socket.Connections[bubu].SocketHandle = Socket.SocketHandle then
begin
memo1.lines.add(' User : '+ odbior.Login+ ' - zostal autoryzowany poprawnie');
Rekordnadawanie.Komenda:='LogowaniePoprawne';
serversocket1.Socket.Connections[bubu].SendBuf(Rekordnadawanie,SizeOf(Rekordnadawanie));
end;
Inc(bubu);
until bubu = ServerSocket1.Socket.ActiveConnections;
end
else
begin
bubu1 := 0;
repeat
if ServerSocket1.Socket.Connections[bubu1].SocketHandle = Socket.SocketHandle then
begin
memo1.lines.add(' User : '+ odbior.Login+ ' - Wprowadzil bledne dane');
Rekordnadawanie.Komenda:='LogowanieBledne';
serversocket1.Socket.Connections[bubu1].SendBuf(Rekordnadawanie,SizeOf(Rekordnadawanie));
end;
Inc(bubu1);
until bubu1 = ServerSocket1.Socket.ActiveConnections;
end
end;
//memo1.lines.add(' Blad w autoryzacji, Zly login lub haslo : '+ odbior.Login);
//Rekordnadawanie.Komenda:='LogowanieBledne';
//kupka1 := 0;
//repeat
//if ServerSocket1.Socket.Connections[kupka1].SocketHandle = Socket.SocketHandle then
//begin
//memo1.lines.add(' User : '+ odbior.Login+ 'zostal wyjebany');
//showmessage('jebie usera');
//serversocket1.Socket.Connections[kupka1].SendBuf(Rekordnadawanie,SizeOf(Rekordnadawanie));
//Inc(kupka1);
//end
//until kupka1 = ServerSocket1.Socket.ActiveConnections;
// Sprawdzamy czy zalogowal sie poprawnie
if odbior.Komenda='WyslijWiadomoscDo' then
begin
for iii:=listbox1.items.count-1 downto 0 do
begin
if (odbior.Login=listbox3.items[iii])and (odbior.Haslo=listbox4.items[iii])
then
begin
listbox5.items.Delete(iii);
listbox5.items.insert(iii,odbior.status);
memo1.lines.add(' Pomyslnie zalogowano użytkownika: '+ odbior.Login);
memo1.lines.add(' Użytkownik '+ odbior.Login+ ' ustawia swoj stan na: '+odbior.status);
// Przypomnienie statusu
Rekordnadawanie.Komenda:='LogowaniePoprawne';
// test//
Rekordnadawanie.Komenda:='LogowaniePoprawne';
begin
for bubu:=0 to ServerSocket1.Socket.ActiveConnections-1 do
serversocket1.Socket.Connections[bubu].SendBuf(Rekordnadawanie,SizeOf(Rekordnadawanie));
end;
// test//
//serversocket1.Socket.SendBuf(Rekordnadawanie,SizeOf(Rekordnadawanie));
// Przypomnienie statusu
// Nadawanie wiadomosci do klienta //
// koniec nadawania wiadomosci do klienta //
// Odpowiadamy klientowi ze wszystko jest ok
// Glupia procedura, ukazujacy pieprzony ID klienta ktory sie podlaczyl
// chyba to wyjebac//
//Int := 0;
//repeat
// if ServerSocket1.Socket.Connections[Int].SocketHandle = Socket.SocketHandle then
/// begin
begin
for iiii:=listbox3.items.count-1 downto 0 do
if (odbior.dofaceta=listbox3.items[iiii]) then
begin
Intps := 0;
repeat
if
(ServerSocket1.Socket.Connections[Intps].RemoteAddress = listbox2.items[iiii]) and
(inttostr(ServerSocket1.Socket.Connections[Intps].RemotePort) = listbox7.items[iiii])
then
begin
// Efekt koncowy
Rekordnadawanie.Komenda:='Wiadomosc';
rekordnadawanie.Tekst:=odbior.Tekst;
serversocket1.Socket.Connections[intps].SendBuf(Rekordnadawanie,SizeOf(Rekordnadawanie));
//Efekt koncowy
end;
Inc(Intps);
until Intps = ServerSocket1.Socket.ActiveConnections;
end;
end;
end;
end;
end;
end;
end.
//Rekordnadawanie.tekst:=odbior.Tekst;
// chyba to wyjebac//
// tu procedura //
// the best chujnia code
// NIE WIEM CO TO !!!!!!!
//for wybor:=listbox8.items.count-1 downto 0 do
//begin
//if (socket.RemoteAddress=listbox8.items[wybor])and (inttostr(socket.RemotePort)=listbox9.items[wybor])
// then
// begin
// end;
// for Int3 := 0 to ServerSocket1.Socket.ActiveConnections-1 do
//begin
//if (ServerSocket1.Socket.RemoteAddress = listbox8.items[wybor])
//and
//(inttostr(ServerSocket1.Socket.RemotePort) = listbox9.items[wybor])
// then
// begin
// showmessage(' HWDP DLA KLIENTA'+inttostr(int3));
// end; end; end;
// NIE WIEM CO TO !!!!!!!
// the best chujnia code !!!!!!
// tu procedura //
//end;
//serversocket1.Socket.Connections[iiii].SendBuf(Rekordnadawanie,SizeOf(Rekordnadawanie));
//listbox1.items.Delete(iii);
// listbox1.items.insert(iii,inttostr(int));
// Exit;
// Inc(Int);
end.
//until Int = ServerSocket1.Socket.ActiveConnections;
// Glupia procedura, ukazujacy pieprzony ID klienta ktory sie podlaczyl
// Odpowiadamy klientowi ze wszystko jest ok
end.
//end;
//end;
// Sprawdzamy czy zalogowal sie poprawnie
// TEST PROCEDURY CZATU !!!
//Socket.ReceiveBuf(Odbior,SizeOf(Odbior));
//Memo1.Lines.Add(Odbior.Login+' '+Odbior.komenda); //dodajemy linie chata
// TEST PROCEDURY CZATU !!!
end.
end;
end;
end.
Kod klienta:
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp;
type
TForm3 = class(TForm)
ClientSocket1: TClientSocket;
Memo1: TMemo;
Edit1: TEdit;
Button1: TButton;
Edit2: TEdit;
Edit3: TEdit;
Button2: TButton;
Label1: TLabel;
Edit4: TEdit;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
type
TMojRekord = record
nick: String;
msg: String;
time: String;
end;
private
{ Private declarations }
public
{ Public declarations }
end;
// REKORDY DANE TAKTYCZNE
type
TRekord = record
Login: string[250];
Haslo: string[250];
Hasz: string[250];
Status: string[250];
Komenda: string[250];
Tekst: string[250];
Dofaceta: string[250];
adres:string[250];
end;
// REKORDY DANE TAKTYCZNE
var
Form3: TForm3;
Rekord:TRekord;
Odbior: TRekord;
// REKORDY DANE TAKTYCZNE
type
TRekordnadawanie = record
Login: string[250];
Haslo: string[250];
Hasz: string[250];
Status: string[250];
Komenda: string[250];
Tekst: string[250];
Dofaceta: string[250];
adres:string[250];
end;
// REKORDY DANE TAKTYCZNE
var
Rekordnadawanie:TRekord;
implementation
{$R *.dfm}
procedure TForm3.Button1Click(Sender: TObject);
begin
Rekord.login:=Edit2.text;
Rekord.haslo:=edit3.text;
Rekord.Status:='Online';
Rekord.Komenda:='WyslijWiadomoscDo';
Rekord.Dofaceta:=edit4.text;
Rekord.tekst:=edit1.text;
Rekord.adres:=edit4.text;
Rekord.adres:=edit4.text;
repeat until ClientSocket1.Socket.SendBuf(Rekord, SizeOf(Rekord))<>-1
end;
procedure TForm3.Button2Click(Sender: TObject);
begin
clientsocket1.Active:=true;
edit2.enabled:=false;
edit3.enabled:=false;
button2.enabled:=false;
Rekord.login:=Edit2.text;
Rekord.haslo:=edit3.text;
Rekord.Status:='Online';
Rekord.Komenda:='zaloguj';
Rekord.Hasz:='xxxxx';
repeat until ClientSocket1.Socket.SendBuf(Rekord, SizeOf(Rekord))<>-1
end;
procedure TForm3.ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
begin
Socket.ReceiveBuf(Odbior,SizeOf(Odbior));
if odbior.Komenda='LogowaniePoprawne' then
begin
label1.caption:='Zalogowano poprawnie do serwera';
edit2.enabled:=false;
edit3.enabled:=false;
button2.enabled:=false;
button1.enabled:=true;
edit1.enabled:=true;
end;
if odbior.Komenda='LogowanieBledne' then
begin
label1.caption:='Login / Haslo niepoprawne !';
edit2.enabled:=true;
edit3.enabled:=true;
button2.enabled:=true;
button1.enabled:=false;
edit1.enabled:=false;
end;
if odbior.Komenda='Wiadomosc' then
memo1.lines.add(odbior.Tekst);
if odbior.Komenda='TypekOffline' then
showmessage('Użytkownik jest offline lub wprowadziłeś nieprawne dane');
end;
procedure TForm3.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if Key=#13 then button1.Click;
end;
procedure TForm3.FormCreate(Sender: TObject);
begin
button1.enabled:=false;
edit1.enabled:=false;
end;
end.