Huaaah.. dah lama nggak nulis di blog ini.. kebetulan ada topik yang cukup menarik.. yak.. namanya socket..
Apakah itu socket?? kalo mau lengkapnya sih liat di wikipedia.. tapi intinya si socket ini sesuatu yang menghubungkan antar komputer agar komputer itu bisa saling berkomunikasi, entah itu chatting, kirim – kiriman file, dan lain – lain..
Nah langsung mulai aja ke tekniknya.. si socket ini mempunya dua komponen yaitu server dan client.. biasanya si komputer yang satu jadi server yang lainnya jadi client.. si server ini akan terus menunggu request dari client sehingga biasanya server harus dinyalakan duluan..
Untuk kodingan buat servernya
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
namespace SocketServer
{
class Server
{
public Socket mClientSock = null;
public Server()
{
try
{
IPEndPoint tIpEnd = new IPEndPoint(IPAddress.Any, 5656);
Socket tSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
tSocket.Bind(tIpEnd);
tSocket.Listen(100);
while (true)
{
mClientSock = tSocket.Accept();
if (mClientSock.Connected)
{
Thread tc = new Thread(new ThreadStart(ListenClient));
tc.Start();
}
}
}
catch (Exception e)
{
Console.WriteLine("File sending fail. " + e.Message);
}
finally
{
Console.ReadLine();
}
}
private void ListenClient()
{
int tJumlahData = 1000
Socket tSock = mClientSock;
byte[] tStringSend = System.Text.Encoding.ASCII.GetBytes("OK");
tSock.Send(tStringSend, tStringSend.Length, 0);
while (tSock.Connected)
{
byte[] tRecCommand = new byte[tJumlahData];
int tRecCount = tSock.Receive(tRecCommand, tRecCommand.Length, 0);
string tMessage = System.Text.Encoding.ASCII.GetString(tRecCommand);
tMessage = tMessage.Substring(0, tRecCount);
string[] tCommandList = tMessage.Split(' ');
if (tCommandList[0] == "OK")
{
tStringSend = System.Text.Encoding.ASCII.GetBytes("OK");
tSock.Send(tStringSend, tStringSend.Length, 0);
}
}
}
}
}
Untuk kodingan di clientnya
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace SocketClient
{
class Program
{
static void Main(string[] args)
{
try
{
String tStringIP = "127.0.0.1"; //Diganti dengan IP server yang akan dituju"
IPAddress tIP = IPAddress.Parse(tStringIP);
IPEndPoint tIpEnd = new IPEndPoint(tIP, 5656);
Socket tClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
tClientSocket.Connect(tIpEnd);
while (tClientSocket.Connected)
{
byte[] tRecCommand = new byte[1000];
int tRecCount = tClientSocket.Receive(tRecCommand, tRecCommand.Length, 0);
string tMessage = System.Text.Encoding.ASCII.GetString(tRecCommand);
tMessage = tMessage.Substring(0, tRecCount);
if (tMessage == "OK")
{
byte[] tStringSend = System.Text.Encoding.ASCII.GetBytes("OK");
tClientSocket.Send(tStringSend, tStringSend.Length, 0);
}
}
}
catch (Exception e)
{
Console.WriteLine("File sending fail. " + e.Message);
}
}
}
}
Jika ke-dua program dijalankan maka mereka akan saling berbalas “OK” tanpa henti. jika ada command – command yang lain maka tinggal ditambahkan di dalam while(tClientSocket.Connected) pada client atau while(true) pada server..
Sekian dulu deh..
Selamat mencoba.. ^_^