Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

27 February 2010

Socket Programming Part II

Assalamulaikum wbt..

Alhamdulillah, first syukur kpd Allah S.W.T krn kite msih lgi dianugerahi nikmat Iman dan Islam wlaupun hkikatnye kite selalu asyik dan lalai dlm menjalankan tggjwab as a khalifah-Nya.

"INTRODUCTION"

Entry y lepas
aku da explain sikit sal ape die socket programming..Kalu korang xbace lgi, sila la bace since aku tkut da y termuntah lak nnti..1st, keep in mind that socket is like a tunnel so that my next explaination would be easy for you guys..Kat bwh nie ade screenshot process flow on how socket programming really works. Oleh kerana buku Unix Network Programming API aku kat en. Azri, mka gambo kat bwh nie adalah gambo y aku google je.




"BRIEFLY"


Socket programming melibatkan communication between server n client. Who's gonna be server n client ?? Jwpan nye adlah terletak pd kite as a programmer. Pd aku, to make it simple, Server is the one who provide the service n client is the one who request/consume the service..For instance, Facebook y hri2 korang usya tue kn. Facebook mainframe wil be the server, n your machine will be the client n request sumer msg2 kat wall..

"1ST, SERVER INITIALIZATION"


Bende y plig penting adalah server perlu ade ip address n port..Bygkn korang da rumah with one address but more doors. Korang kne la decide which "door" y korang nk gune. Port 1-1024 is reserved by IANA as a default web service cam port 80 utk HTTP, port 22 utk FTP and so on..Suggestion aku gune port 2000 and above..1st, initialize Socket class or "tunnel" n call library2 y berkaitan..Next, bind kn dgn ip address machine korang n port y korang nk gune. Xksah IPv4 or IPv6, the step is still same but the address representation n network masking is little bit different. Once korang dh bind, korang sebenarnye dh advertise through out all computer in local network y korang ade "tunnel" ke laptop korang..Be carefull since this is the actuall way on how hackers gain access on victim's computer.So aku advise try dlu dlm mode local host n your ip address should be "127.0.0.1" or "::1" for IPv6..Lepas "tunnel" dh establish, server will be in "daemon state" - a program that always running in the background n listen for any incoming request.

"CONNECTION ESTABLISHMENT"

Next, prepare your client program. Client xperlu bind ip address n port ke socket. What he need to know is with who he want to connect. Atas sbb itu, korang kene supply kat die ip address n port server y korang dh wat td..Yela, kalu x, cmana lgi die nk connect ke server..Then bile connect ke server, 1 "path" akn di establish between your client n server. Utk permulaan, xpyahla pk soal connection secure or x secure, in fact, mmg xsecure pn..Kalu nk wat style "ssl" connection with further packet encryption, sila wat ms kat phase ni.

"DATA TRANSMISION"


Once dh connect, client n server should bleh communicate n pass some string msg as a sample data. Skg nie, data trasmision depend on what type of network protocol y korang nk gune, TCP or UDP. TCP is little bit complex compared with UDP but more reliable..Kenapa?? ok aku explain.

In TCP, stp data akn di convert from host byte to network byte, tmbah dgn other TCP header like source n destination info, TTL (time to live), checksum, padding, "window" size, packet sequence n etc. Maximum transmision unit (MTU) pn agak besar but fixed. Which means kalu buffer size = 1024 byte, but korang juz nk send hruf "A" je, TCP protocol akn automatik add some padding utk mksure die reach MTU size. One more, mose of the time kite akn download muvie y bersize besar >500 MB rite. Its imposible to transmit whole file with single data packet. In reality (like what all download manager do), this 500 MB file akn dipisah2kan kpd packet2 y lebih kecik mengikut size MTU td n also "window" size..Ape die "window" size nie?? Next post insyaAllah aku akn explain sikit since ia melibatkn 3-way handshake n bla bla bla..So, packet2 kecik nie td aku lbih suke pgil as a "chunk"..Each chunk akn di bg "no giliran" or sequence number. Then all chunk will be sent regardless which one is the first or last. Bile packet2 or chunk nie arrive kat laptop korang (client), download manager akn combine or rebuild blik to form the big file again.

In UDP is totally different, each file still need to be splitted into chunks but there is no 3-way handshake, cheksum, n byk lagi. In TCP kite pgil packet, but in UDP kite pgil datagram. Since there no overload network header information, UDP datagram mmg ringan n kcik. Ditambah plak dgn xder sistem "handshake" or acknowledgment, datagram besenye mmg cepat n lagi pantas drpd send gune TCP protocol. But once again, there is no guarantee y datagram nie td akn smpai kpd client. Aku bg contoh, let say la file y besar td split kpd >1000 chunk datagrams, blum tentu datagram ke-5 akan smpai dlu dripad datagram ke-10..Thats why UDP xseswai utk send file bersize bsar, tp advantage die amat berkesan bile nk wat application y byk gune message passing (instant messanger, distributed system).

"CLOSE CONNECTION"


After data transmision hbis, client akn close connection dlu, then bru server..After that, server akn return blik kpd "listen" mode coz cm aku ckp td, server ack like a "daemon" - always running. Bile connection dh close, next client bleh establish connection bru dgn server..

CONCLUSION

Kalu nk further lgi, korang bleh wat Socket connection either in asynchronous or syncronouse mode, non-blocking or blocking state n byk lgi. Aku tw rmai y xminat nk tw bnde2 complicated cmni, but the thing is, its good to expand your knowledge n make yourself "knowledgable" person instead of doing assignment n let that particular knowledge juz "pass by" your subconscious mind..Gain the concept, n apply it whenever needed..

Gudluck..




Sumer dean list from School of Computer Sciences, USM. Cube cri mane aku..hehe






.

26 February 2010

Socket Programming

Assalamulaikum wbt..

Firstly, syukur alhamdulillah krn ngn rahmat dri-Nya aku still lgi mmpu menulis kat blog nie..Sedikit petikan kate2 Dr. Danial ms khutbah jumaat td - "Udhulu fi silmi kaaffah" - y mane mksudnye "msuklah kamu kpd Islam scra menyeluruh / total..Oleh krna minggu nie aku struggle wat socket programming, so nk share sikit ar sal bnde nie. Mudah-mudahan korang y bace bnde nie leh expand n get the new knowledge.

"INTRODUCTION"

Let say korang nk wat 1 application cam Yahoo Messanger or Windows Live or Empathy (Ubuntu) y memerlukan extensive message passing between peers. Cara plig kacang n cepat adalah dgn manipulate database n timer sperti mana my teammate Acad wat ms die 2nd year for Widget Community beliau..Ali taip msg, insert kat shared database. Then Babun ade timer, every 3 seconds check database for new incoming msg. If ade y bru, fetch and display it. Same thing when Babun taip msg kpd Ali. Cara nie mmg sng and less complicated since everyone know how to use database (MsSql, MySql, Oracle etc)..But the thing is, frequent database access amat tidak efficient n high possibility for slow data retrieval time when 100 (let say) people access the database at the same time..Contoh laen, KFC ade byk branch kat 1 Malaysia nie. Director nk latest sales data for every branches. Cara plig senang, once again, every branches update shared central database about their latest sales data. Bnde y sama jgak akn berlaku when lot of people access the same shared database at the same time - slow data retrieval or even insertion..

"SOCKET PROGRAMMING"

So, the answer is - Socket Programming. Ape die Socket?? To make simple, aku bg analogi mudah. Socket nie snang nk fhm kalu kite anggap die mcm 1 tunnel y connect between 2 machine..The main thing is mesti kene 1 side jd Server n other side jd Client.

Server should be in "daemon" state - always running or be in infinite loops sbb Server akn wait for any incoming request..For that reason, Server kene ade ip address and port number that it will listen on to. Bind ip address dgn port number, listen then accept incoming connection, thats all..Kire kite create 1 public tunnel la..Simple kn..Client plak xperlu establish port number. Just create socket class, connect with Server's ip address n port number, setel..port number nie penting sbb server ade byk port number. Kite ade reserved port from 1 - 1024, so available port 1025 till 6000++..byk kn..Just imagine kite ade rumah y byk pintu but 1 je alamat rumah, so skg korang nk suh client tue masuk ikut pintu mana.

"NETWORK PROTOCOL - TCP OR UDP ??"

Next major things are type of data packet n internet protocol. Dlm Socket Programming, ade 2 protocol y kite bleh gune, TCP or UDP..Deep explaination bleh tanye Uncle Googlerudin. Briefly, TCP nie connection oriented n UDP conectionless. Analogy mudah (cam bese), TCP nie cam korang call mak kat kmpung. Once korang dail no. tepon, korang kene tgu mak korang agkat tepon dlu bru bleh ckap rite. Thats means in TCP, connection must establish first before data transmision can be done. However in UDP, analogi nye cam korang send SMS kat pakwe. Juz taip then send kat no tepon y berkaitan. Korang xtaw pn SMS tue dh dibaca ker blum, ape y korang tw ialah SMS tue dh delivered (lum tentu pakwe korang bace lg)..In other words, UDP doesn't need established or reliable connection..

So, bile ms nk gune TCP or UDP?? My opinion, use TCP if packets sequence is important or packet size is big (more than 1024 byte), otherwise use UDP instead..Aku bg contoh, application cam Yahoo Messanger gune UDP, since ape je y nk di send, juz string perkataan or file2 kecik. But Internet Download Manager (IDM) kene gune TCP protocol, kalu x, cmana die nk tlog donloadkn muvie korang kat Syok.org y 700 MB tue ye x..

"CODE EXAMPLE"

Finnish theory class, now practical part. Kat bwah nie aku bg sample code dalam Java utk Socket programming using UDP as network protocol

UDP Server:
import java.io.*; 
import java.net.*;

class UDPServer {
public static void main(String args[]) throws Exception
{
try
{
DatagramSocket serverSocket = new DatagramSocket(9876);

byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];

while(true)
{

receiveData = new byte[1024];

DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);

System.out.println ("Waiting for datagram packet");

serverSocket.receive(receivePacket);

String sentence = new String(receivePacket.getData());

InetAddress IPAddress = receivePacket.getAddress();

int port = receivePacket.getPort();

System.out.println ("From: " + IPAddress + ":" + port);
System.out.println ("Message: " + sentence);

String capitalizedSentence = sentence.toUpperCase();

sendData = capitalizedSentence.getBytes();

DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress,
port);

serverSocket.send(sendPacket);

}

}
catch (SocketException ex) {
System.out.println("UDP Port is occupied.");
System.exit(1);
}

}
}

UDP Client:
import java.io.*; 
import java.net.*;

class UDPClient {
public static void main(String args[]) throws Exception
{
try {
String serverHostname = new String ("127.0.0.1");

if (args.length > 0)
serverHostname = args[0];

BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));

DatagramSocket clientSocket = new DatagramSocket();

InetAddress IPAddress = InetAddress.getByName(serverHostname);
System.out.println ("Attemping to connect to " + IPAddress +
") via UDP port");

byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];

System.out.print("Enter Message: ");
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();

System.out.println ("Sending data to " + sendData.length +
" bytes to server.");
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, 9876);

clientSocket.send(sendPacket);

DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);

System.out.println ("Waiting for return packet");
clientSocket.setSoTimeout(10000);

try {
clientSocket.receive(receivePacket);
String modifiedSentence =
new String(receivePacket.getData());

InetAddress returnIPAddress = receivePacket.getAddress();

int port = receivePacket.getPort();

System.out.println ("From server at: " + returnIPAddress +
":" + port);
System.out.println("Message: " + modifiedSentence);

}
catch (SocketTimeoutException ste)
{
System.out.println ("Timeout Occurred: Packet assumed lost");
}

clientSocket.close();
}
catch (UnknownHostException ex) {
System.err.println(ex);
}
catch (IOException ex) {
System.err.println(ex);
}
}
}

"CONCLUSION"

Ape y code nie buat adalah sgt simple, client connect with server, send perkataan dlm small letter, server receive n convert to Uppercase n then send blik kat client, thats all..Aku rse ckup kot code kat atas nie, juz nk show how client can communicate with server via Socket Programming..Mula2 mmg pening, tp try ar dlu..mesti best..Bleh gune ape2 java compiler, Netbeans or Eclipse (aku gune Eclipse)..No extra library, juz import java.io n java.net jer..hehe



Welcome to Socket Programming =)





.