Anda di halaman 1dari 12

CHAT SERVER DAN CHAT CLIENT

Komponen TServerSocket

Terdapat pada tab

TServerSocket : mengatur komunikasi menggunakan socket sebagai Server pada protokol


TCP/IP, terdapat bagian dari unit scktcomp

Koponen TServerSocket

Properti
Properti Nilai Keterangan
Active true/false Mengaktifkan server
Name menggunakan huruf pemberian nama
Port Int Nomor port
ServerType stNonBlocking, Socket tidak dibatasi untuk koneksi sejumlah
client
stThreadBlocking otomatis create satu socket
Service AnsiString berisi nama servie : ftp, http, finger,dan time

Methode
Methode Nilai Keterangan
Close( ) Void Menutup socket
Open() Void membuka socket sama dengan properti Active
True

Event
event parameter Keterangan
OnClientConnect (TObject *Sender, menerima konekse dari client
TCustomWinSocket *Socket) Kelas dasar milik Windows
Socket API
OnClientDisconnect (TObject *Sender, pemutusan koneksi
TCustomWinSocket *Socket) Kelas dasar milik Windows
Socket API
OnClientRead (TObject *Sender, membaca pesan dari client
TCustomWinSocket *Socket)
OnClientWrite (TObject *Sender, Menulis pesan dari client
TCustomWinSocket *Socket)
OnAccept (TObject *Sender, manerima koneksi
TCustomWinSocket *Socket)
OnListen (TObject *Sender, server Socket dibuka dan
TCustomWinSocket *Socket) mendengarkan
Komponent TCustomWinSocket
Properti Nilai Keterangan
Addr LocalAddress dan Alamat
LocalPort
Connected true/false Koniksi
RemoteAddress AnsiString Nama host atau no IP 123.197.1.2

LocalHost AnsiString Local host


LocalPort GetLocalPort Local port
RemoteAddress AnsiString Alaamat Ip
RemotePort GetRemotePort Nomor Port

Latihan Program
Chat Server
Kompone yang digunakan :
Form
Label1
Edit1
ServerSocket1
Memo1
Button1

Seperti rancangan berikut :

Kode Program

//--------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "UnitChatServer.h"
//----------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormChatServer *FormChatServer;
//----------------------------------------------------------------
__fastcall TFormChatServer::TFormChatServer(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------

Progam pada tombol StartServer

void __fastcall TFormChatServer::Button1Click(TObject *Sender)


{
if (ServerSocket1->Active)
{
ServerSocket1->Active=false;
Button1->Caption ="Server Start";
}
else
{
ServerSocket1->Active=True;
Button1->Caption ="Server Stop";
}
}
//----------------------------------------------------------------

Program event ClientConnect() mengambil informasi dari client (saat melakukan


koneksi)
//--------------------------------------------------------------
void __fastcall TFormChatServer::ServerSocket1ClientConnect(TObject
*Sender,TCustomWinSocket *Socket)
{
int klient = ServerSocket1->Socket->ActiveConnections;
Edit1->Text=IntToStr(klient);
}

Program event ClientReadClientConnect() mengambil informasi dari client (saat


melakukan koneksi)
//--------------------------------------------------------------
void __fastcall TFormChatServer::ServerSocket1ClientRead(TObject
*Sender,
TCustomWinSocket *Socket)
{
AnsiString pesan=Socket->ReceiveText();
Memo1->Lines->Add(pesan);

}
//-------------------------------------------------------------

Komponen ClientSocket
Properti
Properti Nilai Keterangan
Active true/false Mengaktifkan server
Name menggunakan huruf pemberian nama
Port int Nomor port
ClientType stNonBlocking, Socket tidak dibatasi untuk koneksi sejumlah
client
stThreadBlocking otomatis create satu socket
Service AnsiString berisi nama servie : ftp, http, finger,dan time
Host AnsiString No IP atau nama host

Methode
Methode Nilai Keterangan
Close( ) Void Menutup socket
Open() Void membuka socket sama dengan properti Active
True

event
event parameter Keterangan
OnConnect( …) (TObject *Sender, Proses client melakukan
TCustomWinSocket *Socket) koneksi
OnDisconnect(..) (TObject *Sender, Sesuatu yang dilakukan pada
TCustomWinSocket *Socket) saat pemutusan koneksi
OnError (TObject *Sender, Mengambil informasi
TCustomWinSocket *Socket) kesalahan.

Latihan Program
Chat Client
Kompone yang digunakan :
Form
Label1
Edit1
ClientSocket1
Button1
Button2

//----------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "UnitChatClient.h"
//---------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormChatClient *FormChatClient;
//----------------------------------------------------------------
__fastcall TFormChatClient::TFormChatClient(TComponent* Owner)
: TForm(Owner)
{
}
//----------------------------------------------------------------

Tombol Koneksi ditekan (untuk malakukan koneksi)


void __fastcall TFormChatClient::Button1Click(TObject *Sender)
{
if (ClientSocket1->Active)
{
ClientSocket1->Active=false;
Button1->Caption="Koneksi";
}
else
{
ClientSocket1->Active=true;
Button1->Caption="Diskoneksi";
}
}

Tombol Kirim ditekan (untuk mengirim pesan ke server)


//----------------------------------------------------------------
void __fastcall TFormChatClient::Button2Click(TObject *Sender)
{
AnsiString pesan=Edit1->Text;
ClientSocket1->Socket->SendText(pesan);
}
//---------------------------------------------------------

Mencoba Menggunakan Jaringan 2 Komputer

Server
void __fastcall TFormChatServer::ServerSocket1ClientRead(TObject
*Sender,TCustomWinSocket *Socket)
{
AnsiString ip= Socket->RemoteAddress;

AnsiString pesan=Socket->ReceiveText();
Memo1->Lines->Add(pesan+ip);

Komunikasi Dua arah

Rancanglah Form Seperti dibawah ini :

Komponen
• Form
• Panel1
• Menu
• SpeedButton1
• ClientSocket
• ServerSocket
• Memo1
• Memo2
• StatusBar1
1.
Program

tambah kan pada main.h


public: // User declarations
bool IsServer;
String Server;
__fastcall TChatForm(TComponent* Owner);
};

Program main.cpp
//-----------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "main.h"
//-----------------------------------------------------------------
#pragma link "ScktComp"
#pragma resource "*.dfm"
TChatForm *ChatForm;
//-------------------------------------------------------------
__fastcall TChatForm::TChatForm(TComponent* Owner)
: TForm(Owner)
{
}

//--------------------------------------------------------------
// pilihan listen ( mengaktifkan server )
//-------------------------------------------------------------
void __fastcall TChatForm::FileListenItemClick(TObject *Sender)
{
FileListenItem->Checked = !FileListenItem->Checked;
if (FileListenItem->Checked)
{
ClientSocket->Active = false;
ServerSocket->Active = true;
StatusBar1->Panels->Items[0]->Text = "Listening...";
}else
{
if (ServerSocket->Active)
{
ServerSocket->Active = false;
}
StatusBar1->Panels->Items[0]->Text = "";
}
}

//----------------------------------------------------------------
// client Melakukan koneksi denngan mengisikan IP adress atau nama host
//-------------------------------------------------------------------

void __fastcall TChatForm::FileConnectItemClick(TObject *Sender)


{
if (ClientSocket->Active)
{
ClientSocket->Active = false;
}
if (InputQuery("Computer to connect to", "Address Name:", Server))
{
if (Server.Length() > 0)
{
ClientSocket->Host = Server;
ClientSocket->Active = true;
FileListenItem->Checked = false;
}
}
}
//---------------------------------------------------------------
// menu keluar untuk memutup client dan server
//--------------------------------------------------------------
void __fastcall TChatForm::Exit1Click(TObject *Sender)
{
ServerSocket->Close();
ClientSocket->Close();
Close();
}
//------------------------------------------------------------------
// mengetikan pesan lewat memo menekan enter untuk mengirim
//---------------------------------------------------------------
void __fastcall TChatForm::Memo1KeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if (Key == VK_RETURN)
{
if (IsServer){ServerSocket->Socket->Connections[0]->SendText(
Memo1->Lines->Strings[Memo1->Lines->Count - 1]); }
else
{ ClientSocket->Socket->SendText(Memo1->Lines->Strings[
Memo1->Lines->Count -1]);}
}
}
//----------------------------------------------------------------

//----------------------------------------------------------------
void __fastcall TChatForm::FormCreate(TObject *Sender)
{
FileListenItemClick(NULL);
}
//----------------------------------------------------------------
// menulist status koneksi pada StatusBar (di bagian bawah Form)
//-----------------------------------------------------------------
void __fastcall TChatForm::ClientSocketConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->Panels->Items[0]->Text =
"Connect to: " + Socket->RemoteHost;
}
//-----------------------------------------------------------------
//memutuskan koneksi
//----------------------------------------------------------------
void __fastcall TChatForm::Disconnect1Click(TObject *Sender)
{
ClientSocket->Active = false;
ServerSocket->Active = true;
StatusBar1->Panels->Items[0]->Text = "Listening...";
}
//----------------------------------------------------------------

//---------------------------------------------------------
// membaca pesan dari client
//--------------------------------------------------------------
void __fastcall TChatForm::ClientSocketRead(TObject *Sender,
TCustomWinSocket *Socket)
{
Memo2->Lines->Add(Socket->ReceiveText());
}
//----------------------------------------------------------------
// server mambaca pesan
//-------------------------------------------------------------
void __fastcall TChatForm::ServerSocketClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
Memo2->Lines->Add(Socket->ReceiveText());
}
//----------------------------------------------------------------
// Status hubungan socket diterima
//---------------------------------------------------------------
void __fastcall TChatForm::ServerSocketAccept(TObject *Sender,
TCustomWinSocket *Socket)
{
IsServer = true;
StatusBar1->Panels->Items[0]->Text =
"Connect to: " + Socket->RemoteAddress;
}
//--------------------------------------------------------------
// menghapus isi memo
//----------------------------------------------------------------
void __fastcall TChatForm::ServerSocketClientConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
Memo2->Lines->Clear();
}
//--------------------------------------------------------------
// client diskoneksi
//-----------------------------------------------------------------
void __fastcall TChatForm::ClientSocketDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
FileListenItemClick(NULL);
}
//--------------------------------------------------------------
// cek kesalahan koneksi
//----------------------------------------------------------------
void __fastcall TChatForm::ClientSocketError(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
Memo2->Lines->Add("Error connecting to:" + Server);
ErrorCode = 0;
}
//---------------------------------------------------------------
// Server diskoneksi
//----------------------------------------------------------------
void __fastcall TChatForm::ServerSocketClientDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->Panels->Items[0]->Text = "Listening...";
}
//-----------------------------------------------------------------

UJI Coba

Komputer