Showing posts with label Socket. Show all posts
Showing posts with label Socket. Show all posts

21 March 2010

Tengah berfikir..

Assalamulaikum wbt..




Click to enlarge


Gambar atas nie adalah workflow utk socket y aku prepare utk grid computing..pening mmg pening..jadi mmg jd..cume aku xsure workable or x bile combine dgn part Acad..hmm..

Sperti y ditunjukkan pd gambarajah di atas, aku write based on basic socket for client server. 3 hari gak berfikir smbil tgk CSI Las Vegas nih..Nseb bek la aku amik CST234 dulu, network programming.Kalu x, menjajah jgak nk tnye uncle googlerudin..

Aku rse cm complicated nk explain..cume pe y bleh di explain adalah sumer y kt atas tuh run on cross platform (windows + linux) and y pling penting, stiap server bleh jd client n vice versa..Alternating each other whenever needed.



i'm still thinking...








.

18 March 2010

FYP Progress

Assalamulaikum wbt..

Alhamdulillah, skg nie msih lgi da kekuatan utk berfikir, consume knowledge atas keizinan Allah swt..Dipendekkan cite, aku xsure projek aku nie nk ke mana..Wat ms nie, saat nie, pkataan y seswai utk projek nie adalah "impossible" to me..Still stp hri kne keep thinking nk figure out cmana nk "possible"kn bnde nie..Bese ar, bnde nie sgt bru bg aku..name pn bdak network terjun lam field AI n grid computing..

"INSPORA - IN DETAILS"



Click to enlarge

Kat atas nie adalah main system framework utk projek aku..Main purpose bnde nie adalah utk membolehkan orag rmai dptkan info sal bunga dgn secepat y mungkin..igt tue, "secepat y mungkin"..Which means, this project should implement all necessary method to make it run faster and efficient, xksah ar processing algorithm ker, memory leak management ker..Aku explain ar sikit supaya korang senang nk phm k..

"1 - USER INPUT"

Utk system nie, ade dua main input iaitu mobile device or website. User bleh upload gambar bunga either melalui website (conventional way) or send directly from mobile thru MMS ke system..Utk website, team aku gune Silverlight 3 technology utk interface visualization (XAML), all animation n language C# utk sumer event handling..Silverlight nie slah satu component dlm .Net Framework (latest version 4.0) y specialize utk high user interface experience. Ape y best nye, Silverlight nie ade full integration with Asp.net..mksudnye, let say ar certain function xleh wat dlm Silverlight, kite bleh invoke Asp.net punyer function as a delegate function..

Next, utk integration between Silverlight page ngn Web Server, aku gune WCF utk manage data connection..WCF nie integated web service y operate based on SOA (service oriented architecture)..snang gune nie sbb programmer cm kite xperlu nk pk kn sal WSDL, UDDI, Object Serialization n etc since everything WCF dh handle..data serialization done by enumerable data collection.Tue part web, utk receive MMS, kne ader GSM Modem spya bleh receive image file from network provider..GSM modem nie lebih kurang cm broadband y kite bese gune, cume beza nye die ade antena kecik cam access point..Once user send MMS ker system, akan ade "daemon" apps y akn retrive image from GSM modem nie and transfer tue web server..So, for now web server dh ade flower image y die dpt either dri GSM Modem (MMS) or Silverlight (manual upload)..

"2 - WEB SERVER + GRID MANAGER"


Next, web server n grid manager actualy reside on two separate machines..One on windows n linux on the other..Utk communication n data transmission between 2 machine nie aku gune socket programming dgn UDP protocol, since aku assume both are on dedicated line n no service lookup is needed..just one-to-one mapping..Those y interested nk tw pe die socket programmng nie leh rujuk kat entry y lepas2 yer..Once web server dh dpt image tue, cam bese, path file akn di store lam MySql database, n image file dlm file server..Bleh je nk stor image dlm database gune format BLOB, tp nti nk load image akn jd lmbat..Then path file tue akn di pass as a forward request to Grid Manager..Kat sini, ngn bantuan Globus middleware and main language Java, any available image processing engine bleh di pick on round robin basis..Grid computing mmg pnting kat sini..Xleh nk citer byk sbb aku pn still ngah develop part nie..Examiner aku suggest gune ping method, kire machine y reply plig cepat, machine tu y akn di pick..

"3 - IMAGE PROCESSING"

Dlm projek Inspora nie, ade 2 part y plig pnting iaitu cross platform communication n image processing..After receive path file image td, ngn bantuan Matlab, system akn retrive blik image from file server, and go thru 1st phase which is image enhancement. Reason being is image from user bleh dtg ngn mcm2 format, ade 2, 3 or 8 megapixel, ade y jpeg, bitmap or png, ade y blur n low contrast..mcm2 lagi..After that, 2nd phase which is image recognition will take over..Bermula ngn binarization, elimination, then edge detection utk define degree of circularity or shape, n dominant color determination..process nie melibatkan bbrapa feature extraction algorithm y agk pnjag lebar kalu nk explain kt sini..so, xyah la explain erk..Data2 y dh di extract tu akn dicompare ngn system punye neural network based on back propagation algorithm..berkenaan ngn bnde nie, aku srahkan kat acad ar (team mate) sbb die majoring in AI..hehe..

"4 - FORWARD RESULT"

After dh dpt result n dh tw bunga tue bunga ape, retrieve reference id from database, pass back to web server. Based on id tue, web server akn retrieve necessary info n display result kat user (in case of Silverlight) or send to user using SMS via SMS gateway (in case of MMS)..SMS gateway nie plak actually 1 "daemon" apps y connect ngn external dummy gateway

"CURRENT PROGRESS"

Click to enlarge

Progress skg ? leh rujuk pic kat atas tu..mmg byk lagi y aku xwat..xtaw bile nk siap nie..bnde nie ssh nk wat sbb name pn projek bdak master, level cm kite nie ssh gak ar nk prah otak fikirkan kn bnde y kite xpnah blaja..Dlm project ni, aku responsible utk interprocess and cross platform communication on mobile n distributed grid computing, and team mate aku, Acad plak responsible utk image processing n neural network algorithm..exploration takes time kn..sabar je laa.

"SOME SCREENSHOT"

Kalu korang prasan ar, projek aku nie bleh kate 87% main programming kat background system..xder ar interface sgt pun..Projek Inspora nie da 1 interface jer iaitu from Silverlight website..So, aku tnjuk ar sikit2 shot ya ader..hak cipta terpelihara ok..watch out..

Main front page..Konsep die kt sini, besenye org taip keyword kat google utk cri image, but skg ktorang reverse, upload image utk cri keyword n explaination.


Sign up page..Main theme web site nie kaler kuning + grey..logo kaler hjau tu aku pick je mane2..


Front page after sign in..data xder lagi, sbb mlas nk goreng info..nk display jer pun.


Page utk display all image uploaded by user..sumer gmbar same sbb gune sample pic y sme.


Flower description..Once again, data kosong sbb mlas nk goreng info..


Page utk list all or favorite article..kosong gak sbb info n karangan len mlas nk isi..y ptg programming sumer siap..






.

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 =)





.