Chat Em Delphi! - Client + Servidor
Página 1 de 1
Chat Em Delphi! - Client + Servidor
Depois de muita falta de sucesso no chat em Visual Basic, cá estou com um chat em Delphi.
CLIENTE:
1 - Adicione 2 Memos, 3 Edits e um Client Socket, dê os nomes e posicione de acordo com a imagem abaixo:
1.5 - Mude a Port do Client Socket para um valor que não esteja sendo usado ( eu uso 7171 ).
2 - Códigos:
2.1 - Evento KeyDown do edit Text:
begin
if Key = VK_Return then
begin
if Text.Text <> '' then
begin
ClientSocket1.Socket.SendText(Text.Text + '::::' + Name.Text);
Text.Text := '';
end;
end;
end;
2.2 - Evento OnClick do botão Connect:
begin
if ClientSocket1.Active then
begin
ClientSocket1.Active := False;
Connect.Caption := 'Conectar';
end
else begin
ClientSocket1.Host := Host.Text;
ClientSocket1.Active := True;
end;
end;
2.3 - Evento Connect do ClientSocket1:
begin
Status.Lines.Add('Cliente ::> Conectado a: ' + ClientSocket1.Host);
Connect.Caption := 'Desconectar';
Name.Enabled := False;
ClientSocket1.Socket.SendText('NICK::::' + Name.Text);
end;
2.4 - Evento Disconnect do ClientSocket1:
begin
Status.Lines.Add('Cliente ::> Desconectado ');
Connect.Caption := 'Conectar';
Name.Enabled := True;
end;
2.5 - Evento Error do ClientSocket1:
begin
Status.Lines.Add('Cliente ::> ERRO ao tentar conectar a: ' + ClientSocket1.Host);
end;
2.6 - Evento Read do Client Socket1:
begin
Messages.Lines.Add(Socket.ReceiveText);
end;
2.7 - Evento OnClick do botão Send:
begin
if Text.Text <> '' then
begin
ClientSocket1.Socket.SendText(Text.Text + '::::' + Name.Text);
Text.Text := '';
end;
end;
SERVIDOR:
1 - Adicione 2 Memos, 1 Edits, um Server Socket e um Client Socket, dê os nomes e posicione de acordo com a imagem abaixo:
1.5.1 - Mude a Port do Client Socket para um valor que não esteja sendo usado ( eu uso 7171 ).
1.5.2 - Mude a Port do Server Socket para o valor do ClientSocket ( eu uso 7171 ).
2 - Códigos:
2.1 - Evento KeyDown do edit Text:
begin
if Key = VK_Return then
begin
if Text.Text <> '' then
begin
ClientSocket1.Socket.SendText(Text.Text + '::::' + 'Admin');
Text.Text := '';
end;
end;
end;
2.2 - Evento Connect do ClientSocket1:
begin
Status.Lines.Add('Cliente ::> Conectado a: ' + ClientSocket1.Host);
ClientSocket1.Socket.SendText('NICK::::' + 'Admin');
end;
2.3 - Evento Disconnect do ClientSocket1:
begin
Status.Lines.Add('Cliente ::> Desconectado ');
end;
2.4 - Evento Error do ClientSocket1:
begin
Status.Lines.Add('Cliente ::> ERRO ao tentar conectar a: ' + ClientSocket1.Host);
end;
2.5 - Evento Read do ClientSocket1:ClientSocket1
begin
Messages.Lines.Add(Socket.ReceiveText);
end;
2.6 - Evento onClick do botão Start:
begin
if ServerSocket1.Active = True then
begin
ServerSocket1.Active := False;
Status.Lines.Add('Servidor ::> Servidor Desligado!');
Start.Caption := 'Iniciar Servidor';
ClientSocket1.Active := False;
end
else begin
ServerSocket1.Active := True;
Start.Caption := 'Parar Servidor';
ClientSocket1.Host := '127.0.0.1';
ClientSocket1.Active := True;
end;
end;
2.7 - Evento Listen do ServerSocket1:
begin
Status.Lines.Add('Servidor ::> Servidor Ligado!');
end;
2.8 - Evento ClientConnect do ServerSocket1:
begin
Status.Lines.Add('Servidor ::> Usuário Conectado => '+ Socket.RemoteAddress);
end;
2.9 - Evento ClientDisconnect do ServerSocket1:
begin
Status.Lines.Add('Servidor ::> Usuário Desconectado => '+ Socket.RemoteAddress);
end;
3 - Evento ClientRead do ServerSocket1:
var texto: array[0..1] of string;
temptexto: string;
Index: integer;
begin
temptexto := Socket.ReceiveText;
texto[0] := Copy(temptexto, 1,Pos('::::', temptexto) -1);
texto[1] := Copy(temptexto, Pos('::::', temptexto) + Length('::::'),Length(temptexto));
if texto[0] = 'NICK' then {Verifica se a mensagem eh de entrada}
begin
WITH ServerSocket1.Socket DO BEGIN {Se a msg for de entrada avisa a todos os clientes quem entrou }
FOR Index := 0 TO ActiveConnections-1 DO BEGIN
Connections[Index].SendText(texto[1] + ' entrou na sala: ');
END;
END;
end
else
begin
WITH ServerSocket1.Socket DO BEGIN {Se nao for de entrada, então eh msg normal, no caso passa para todos a msg}
FOR Index := 0 TO ActiveConnections-1 DO BEGIN
Connections[Index].SendText('(' + texto[1] + ') escreveu: ' + texto[0]);
END;
END;
Status.Lines.Add('Servidor ::> ' + texto[1] + ' (' + Socket.RemoteAddress + ') escreveu: '+ texto[0]);
end;
end;
3.1 - Evento onClick do botão Send:
begin
if Text.Text <> '' then
begin
ClientSocket1.Socket.SendText(Text.Text + '::::' + 'Admin');
Text.Text := '';
end;
end;
IMPORTANTE:
QuoteDesativar o firewall ?
Não, só adicionar a porta utilizada em exceções.
CLIENTE:
1 - Adicione 2 Memos, 3 Edits e um Client Socket, dê os nomes e posicione de acordo com a imagem abaixo:
1.5 - Mude a Port do Client Socket para um valor que não esteja sendo usado ( eu uso 7171 ).
2 - Códigos:
2.1 - Evento KeyDown do edit Text:
begin
if Key = VK_Return then
begin
if Text.Text <> '' then
begin
ClientSocket1.Socket.SendText(Text.Text + '::::' + Name.Text);
Text.Text := '';
end;
end;
end;
2.2 - Evento OnClick do botão Connect:
begin
if ClientSocket1.Active then
begin
ClientSocket1.Active := False;
Connect.Caption := 'Conectar';
end
else begin
ClientSocket1.Host := Host.Text;
ClientSocket1.Active := True;
end;
end;
2.3 - Evento Connect do ClientSocket1:
begin
Status.Lines.Add('Cliente ::> Conectado a: ' + ClientSocket1.Host);
Connect.Caption := 'Desconectar';
Name.Enabled := False;
ClientSocket1.Socket.SendText('NICK::::' + Name.Text);
end;
2.4 - Evento Disconnect do ClientSocket1:
begin
Status.Lines.Add('Cliente ::> Desconectado ');
Connect.Caption := 'Conectar';
Name.Enabled := True;
end;
2.5 - Evento Error do ClientSocket1:
begin
Status.Lines.Add('Cliente ::> ERRO ao tentar conectar a: ' + ClientSocket1.Host);
end;
2.6 - Evento Read do Client Socket1:
begin
Messages.Lines.Add(Socket.ReceiveText);
end;
2.7 - Evento OnClick do botão Send:
begin
if Text.Text <> '' then
begin
ClientSocket1.Socket.SendText(Text.Text + '::::' + Name.Text);
Text.Text := '';
end;
end;
SERVIDOR:
1 - Adicione 2 Memos, 1 Edits, um Server Socket e um Client Socket, dê os nomes e posicione de acordo com a imagem abaixo:
1.5.1 - Mude a Port do Client Socket para um valor que não esteja sendo usado ( eu uso 7171 ).
1.5.2 - Mude a Port do Server Socket para o valor do ClientSocket ( eu uso 7171 ).
2 - Códigos:
2.1 - Evento KeyDown do edit Text:
begin
if Key = VK_Return then
begin
if Text.Text <> '' then
begin
ClientSocket1.Socket.SendText(Text.Text + '::::' + 'Admin');
Text.Text := '';
end;
end;
end;
2.2 - Evento Connect do ClientSocket1:
begin
Status.Lines.Add('Cliente ::> Conectado a: ' + ClientSocket1.Host);
ClientSocket1.Socket.SendText('NICK::::' + 'Admin');
end;
2.3 - Evento Disconnect do ClientSocket1:
begin
Status.Lines.Add('Cliente ::> Desconectado ');
end;
2.4 - Evento Error do ClientSocket1:
begin
Status.Lines.Add('Cliente ::> ERRO ao tentar conectar a: ' + ClientSocket1.Host);
end;
2.5 - Evento Read do ClientSocket1:ClientSocket1
begin
Messages.Lines.Add(Socket.ReceiveText);
end;
2.6 - Evento onClick do botão Start:
begin
if ServerSocket1.Active = True then
begin
ServerSocket1.Active := False;
Status.Lines.Add('Servidor ::> Servidor Desligado!');
Start.Caption := 'Iniciar Servidor';
ClientSocket1.Active := False;
end
else begin
ServerSocket1.Active := True;
Start.Caption := 'Parar Servidor';
ClientSocket1.Host := '127.0.0.1';
ClientSocket1.Active := True;
end;
end;
2.7 - Evento Listen do ServerSocket1:
begin
Status.Lines.Add('Servidor ::> Servidor Ligado!');
end;
2.8 - Evento ClientConnect do ServerSocket1:
begin
Status.Lines.Add('Servidor ::> Usuário Conectado => '+ Socket.RemoteAddress);
end;
2.9 - Evento ClientDisconnect do ServerSocket1:
begin
Status.Lines.Add('Servidor ::> Usuário Desconectado => '+ Socket.RemoteAddress);
end;
3 - Evento ClientRead do ServerSocket1:
var texto: array[0..1] of string;
temptexto: string;
Index: integer;
begin
temptexto := Socket.ReceiveText;
texto[0] := Copy(temptexto, 1,Pos('::::', temptexto) -1);
texto[1] := Copy(temptexto, Pos('::::', temptexto) + Length('::::'),Length(temptexto));
if texto[0] = 'NICK' then {Verifica se a mensagem eh de entrada}
begin
WITH ServerSocket1.Socket DO BEGIN {Se a msg for de entrada avisa a todos os clientes quem entrou }
FOR Index := 0 TO ActiveConnections-1 DO BEGIN
Connections[Index].SendText(texto[1] + ' entrou na sala: ');
END;
END;
end
else
begin
WITH ServerSocket1.Socket DO BEGIN {Se nao for de entrada, então eh msg normal, no caso passa para todos a msg}
FOR Index := 0 TO ActiveConnections-1 DO BEGIN
Connections[Index].SendText('(' + texto[1] + ') escreveu: ' + texto[0]);
END;
END;
Status.Lines.Add('Servidor ::> ' + texto[1] + ' (' + Socket.RemoteAddress + ') escreveu: '+ texto[0]);
end;
end;
3.1 - Evento onClick do botão Send:
begin
if Text.Text <> '' then
begin
ClientSocket1.Socket.SendText(Text.Text + '::::' + 'Admin');
Text.Text := '';
end;
end;
IMPORTANTE:
QuoteDesativar o firewall ?
Não, só adicionar a porta utilizada em exceções.
Tópicos semelhantes
» Criando Servidor
» [Delphi 7]Controlando o mouse com o teclado.
» [Delphi]Criando Um Alto-Click
» Listão de Códigos Para Delphi
» [Delphi] Player de Músicas com PlayList.
» [Delphi 7]Controlando o mouse com o teclado.
» [Delphi]Criando Um Alto-Click
» Listão de Códigos Para Delphi
» [Delphi] Player de Músicas com PlayList.
Página 1 de 1
Permissões neste sub-fórum
Não podes responder a tópicos
|
|
Qua Dez 25, 2013 12:37 am por Sirius
» Recrutamento King Cheats
Dom Abr 08, 2012 9:15 pm por gealves
» Parceiria????
Qua Fev 08, 2012 7:29 pm por Pocahontas
» Novo botão de download da MasterGames
Qua Fev 08, 2012 7:28 pm por Pocahontas
» Hello .
Qua Fev 08, 2012 7:28 pm por Pocahontas
» Sobre A Nova Era DDTank...
Ter Jan 24, 2012 11:06 pm por Snow
» MuNect|Iaugurando Dia 20/jul/11 as 8:00hrs|Super Eventos|Não perca.
Ter Jul 19, 2011 9:25 pm por felipebme
» Angel Trainer Hack
Qua Fev 23, 2011 7:52 pm por TEUSO97
» [Tradução] Pro Evolution Soccer 2011
Qua Nov 03, 2010 8:19 pm por PMD PKMN