Sekarang kita bisa membuat mesin chating sendiri untuk jaringan lokal. Bagi yang sudah mengenal bahasa pemograman delphi pasti sudah familier dengan program ini. cukup dengan hanya kabel UTP kita bisa chatingan antar komputer. berikut source code nya.
antara 2 komputer yang dipakai, salah satunya sebagai server dan yang lain sebagai client.
berikut yang digunakan sebagai server.
procedure TForm1.Button1Click(Sender: TObject); // connect client
begin
serversocket1.Open;
memo1.Clear;
end;
procedure TForm1.Button2Click(Sender: TObject); //tombol kirim
begin
memo1.Lines.Add(‘server : ‘+memo2.Text);
if (serversocket1.Socket.ActiveConnections>0) then
serversocket1.Socket.Connections[0].SendText(‘server : ‘+memo2.Text);
memo2.Clear;
end;
{procedure TForm1.ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
begin
memo1.Lines.Add(‘accept dari’ + socket.RemoteHost);
end;}
{procedure TForm1.ServerSocket1Listen(Sender: TObject;
Socket: TCustomWinSocket);
begin
memo1.Lines.Add(‘accept + dari’ + socket.LocalHost);
end;}
procedure TForm1.ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
memo1.Lines.Add(‘error dari’ + inttostr(errorcode));
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
memo1.Lines.Add(socket.ReceiveText);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
memo1.Clear;
memo2.Clear;
serversocket1.Port:=25;
end;
procedure TForm1.Memo2KeyPress(Sender: TObject; var Key: Char);
begin
if key = chr(13) then
begin
memo1.Lines.Add(‘server : ‘+memo2.Text);
if (serversocket1.Socket.ActiveConnections>0) then
serversocket1.Socket.Connections[0].SendText(‘server : ‘+memo2.Text);
memo2.Clear;
end;
end;
end.
sedangkan untuk client source code nya sebagai berikut
procedure TForm1.Button1Click(Sender: TObject);
begin
clientsocket1.Open;
memo1.Clear;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
memo1.Lines.Add(‘client :’ +memo2.Text);
if (clientsocket1.Socket.Connected) then
clientsocket1.Socket.SendText(‘client : ‘+memo2.Text);
memo2.Clear;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
memo1.Clear;
memo2.Clear;
clientsocket1.Host :=’10.111.210.69′;
clientsocket1.Port :=25;
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
memo1.Lines.Add(socket.RemoteHost);
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
memo1.Lines.Add(‘error’ + inttostr(errorcode));
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
begin
memo1.Lines.Add(socket.ReceiveText);
end;
procedure TForm1.Memo2KeyPress(Sender: TObject; var Key: Char);
begin
if key = chr(13) then
begin
memo1.Lines.Add(memo2.Text);
if (clientsocket1.Socket.Connected) then
clientsocket1.Socket.SendText(‘client : ‘+memo2.Text);
memo2.Clear;
end;
end;
end.
selamat mencoba……
0 comments