awesome video check it out ...
Greg Pattillo
Tuesday, December 15, 2009
Monday, December 14, 2009
A simple server-client program
A simple server-client program
As you guys might know a server program is one that accepts requests from various clients. A client program connects to server and requests or asks it to perform task for it...
An intermediary between a server and a client is through a socket. Think of socket as an electrical socket in your house... eletrical appliances are plugged in to the socket. For example, A phone line is plugged into a similar socket in order to communicate to person who is on another phone some where else and his phone (in a similar way) is connected to a socket as well.
Both the persons on the phone need not worry about the network connections and the way the data flows through the network... the simple server client program works in a similar way. JAVA makes it look similar to transferring data through files as in File I/O programs... Only an additional socket needs to be created...
Let me demonstrate a basic client program...
First we begin with import the 2 mandatory packages that my program requires -
1. java.io.*;
2. java.net.*;
io package is imported so that I can read and write stuff through network just as I do it in case of file reading and file writing. The net package is imported so that I can create a socket. Socket class is present in net package...
Next i'm gonna create a class MyClient with the main method...(this is easy)
The program may throw some IO Exception and therefore we write "throws IOException" along with main method definition.
The program goes something like this...
import java.io.*;
import java.net.*;
public MyClient{
public static void main(String args[]) throws IOException
{
....
....
....
}
}
Now i'm gonna write some stuff in it....
I'm gonna create a socket now using the Socket class...
Socket s = new Socket("localhost",9999);
Socket constructor accepts Internet address or hostname and port as parameters... I assume that both server and client are on the same box (computer) and therefore I used "localhost" which represents the loopback address 127.0.0.1 and I chose 9999 as port.
Ok... we are done with the hardest part... lolz
Now as we all know... be it files or network everything goes through streams...
So now we create a stream... Since it is through network I'd prefer creating a buffered stream...
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
Here s is the socket as you know it... the stream is created for the socket and therefore we get the output stream for the socket.
Now I create the data to be passed... lets say a "hi" to the server...
String str = "hi server... wassup?? $";
I put a "$" character at the end... so the server reads data till "$" appears... (server should know when to stop reading stuff :P)
Now, I write stuff to buffered output stream created for the socket using write()
for(int i=0 ; i bos.write(str.charAt(i));
I write one character at a time... therefore I put it in a for loop which iterates for the number of characters in the string and so I use length() method to find no. of characters for string.
Since I'm writing one character at time and as string a set of characters I use charAt() method for get character at that particular position...
Thats it we're done...
Just to make sure you know when your data writing ended... i'll put a display statement...
System.out.println("Data writing complete...");
I make sure that I close my socket and bos otherwise the second time I run the program I'll get an exception... I'll have to change the port to something else...
Therefore,
bos.close;
s.close;
Thats it... the complete program goes like this...
import java.io.*;
import java.net.*;
public MyClient{
public static void main(String args[]) throws IOException
{
Socket s = new Socket("localhost",9999);
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
String str = "hi server... wassup?? $";
for(int i=0 ; i bos.write(str.charAt(i));
System.out.println("Data writing complete...");
bos.close;
s.close;
}
}
Now the server program...
Same as in client we import io , net packages and create a MyServer class with main method...
import java.io.*;
import java.net.*;
public MyServer{
public static void main(String args[]) throws IOException
{
....
....
....
}
}
To give an effect of to separate programs interacting I created the server program with main method. But as we know... we cannot run two main methods in the same workspace at the same time...
Therefore , I create a new workspace and launch a new instance of eclipse... We'll I used eclipse which an IDE (Integrated Development Environment) for creating and running java programs.
You can use any other IDE or if you are running programs with java compiler and have been using notepad for writing java programs... launch another dos prompt... that will give the effect of two separate
programs interacting...
Now in main method , I create a Server Socket using ServerSocket class...
ServerSocket ss = new ServerSocket(9999);
We need to give only port... since we are the server.
Now we accept connection from the user... a socket captures connections accepted by the server socket....
Socket s = ss.accept();
Then we create to recieve data through Buffered input stream...
BufferedInputStream bis = new BufferedInputStream(s.getInputStream); // same as in client program
We get the data in integer form one byte at a time... lets declare an integer variable...
int c;
Then we read one byte by read() method till we have reached "$" symbol...and display it.
while((c=bis.read())!='$')
System.out.print((char)c);
We have to type cast variable c to char or else we'll have number displaying on screen.
We write a display to show that we've completed reading...
System.out.println("Data reading complete...");
Then we close the sockets ...
s.close();
ss.close();
Thats our server program....
The whole program goes like this...
import java.io.*;
import java.net.*;
public MyServer{
public static void main(String args[]) throws IOException
{
ServerSocket ss = new ServerSocket(9999);
Socket s = ss.accept();
BufferedInputStream bis = new BufferedInputStream(s.getInputStream);
int c;
while((c=bis.read())!='$')
System.out.print((char)c);
System.out.println("Data reading complete...");
s.close();
ss.close();
}
}
Run the server program first...then the client.
If you run the client first you'll get an exception...
There you go... a server-client program :)
Cheers :)
As you guys might know a server program is one that accepts requests from various clients. A client program connects to server and requests or asks it to perform task for it...
An intermediary between a server and a client is through a socket. Think of socket as an electrical socket in your house... eletrical appliances are plugged in to the socket. For example, A phone line is plugged into a similar socket in order to communicate to person who is on another phone some where else and his phone (in a similar way) is connected to a socket as well.
Both the persons on the phone need not worry about the network connections and the way the data flows through the network... the simple server client program works in a similar way. JAVA makes it look similar to transferring data through files as in File I/O programs... Only an additional socket needs to be created...
Let me demonstrate a basic client program...
First we begin with import the 2 mandatory packages that my program requires -
1. java.io.*;
2. java.net.*;
io package is imported so that I can read and write stuff through network just as I do it in case of file reading and file writing. The net package is imported so that I can create a socket. Socket class is present in net package...
Next i'm gonna create a class MyClient with the main method...(this is easy)
The program may throw some IO Exception and therefore we write "throws IOException" along with main method definition.
The program goes something like this...
import java.io.*;
import java.net.*;
public MyClient{
public static void main(String args[]) throws IOException
{
....
....
....
}
}
Now i'm gonna write some stuff in it....
I'm gonna create a socket now using the Socket class...
Socket s = new Socket("localhost",9999);
Socket constructor accepts Internet address or hostname and port as parameters... I assume that both server and client are on the same box (computer) and therefore I used "localhost" which represents the loopback address 127.0.0.1 and I chose 9999 as port.
Ok... we are done with the hardest part... lolz
Now as we all know... be it files or network everything goes through streams...
So now we create a stream... Since it is through network I'd prefer creating a buffered stream...
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
Here s is the socket as you know it... the stream is created for the socket and therefore we get the output stream for the socket.
Now I create the data to be passed... lets say a "hi" to the server...
String str = "hi server... wassup?? $";
I put a "$" character at the end... so the server reads data till "$" appears... (server should know when to stop reading stuff :P)
Now, I write stuff to buffered output stream created for the socket using write()
for(int i=0 ; i
I write one character at a time... therefore I put it in a for loop which iterates for the number of characters in the string and so I use length() method to find no. of characters for string.
Since I'm writing one character at time and as string a set of characters I use charAt() method for get character at that particular position...
Thats it we're done...
Just to make sure you know when your data writing ended... i'll put a display statement...
System.out.println("Data writing complete...");
I make sure that I close my socket and bos otherwise the second time I run the program I'll get an exception... I'll have to change the port to something else...
Therefore,
bos.close;
s.close;
Thats it... the complete program goes like this...
import java.io.*;
import java.net.*;
public MyClient{
public static void main(String args[]) throws IOException
{
Socket s = new Socket("localhost",9999);
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
String str = "hi server... wassup?? $";
for(int i=0 ; i
System.out.println("Data writing complete...");
bos.close;
s.close;
}
}
Now the server program...
Same as in client we import io , net packages and create a MyServer class with main method...
import java.io.*;
import java.net.*;
public MyServer{
public static void main(String args[]) throws IOException
{
....
....
....
}
}
To give an effect of to separate programs interacting I created the server program with main method. But as we know... we cannot run two main methods in the same workspace at the same time...
Therefore , I create a new workspace and launch a new instance of eclipse... We'll I used eclipse which an IDE (Integrated Development Environment) for creating and running java programs.
You can use any other IDE or if you are running programs with java compiler and have been using notepad for writing java programs... launch another dos prompt... that will give the effect of two separate
programs interacting...
Now in main method , I create a Server Socket using ServerSocket class...
ServerSocket ss = new ServerSocket(9999);
We need to give only port... since we are the server.
Now we accept connection from the user... a socket captures connections accepted by the server socket....
Socket s = ss.accept();
Then we create to recieve data through Buffered input stream...
BufferedInputStream bis = new BufferedInputStream(s.getInputStream); // same as in client program
We get the data in integer form one byte at a time... lets declare an integer variable...
int c;
Then we read one byte by read() method till we have reached "$" symbol...and display it.
while((c=bis.read())!='$')
System.out.print((char)c);
We have to type cast variable c to char or else we'll have number displaying on screen.
We write a display to show that we've completed reading...
System.out.println("Data reading complete...");
Then we close the sockets ...
s.close();
ss.close();
Thats our server program....
The whole program goes like this...
import java.io.*;
import java.net.*;
public MyServer{
public static void main(String args[]) throws IOException
{
ServerSocket ss = new ServerSocket(9999);
Socket s = ss.accept();
BufferedInputStream bis = new BufferedInputStream(s.getInputStream);
int c;
while((c=bis.read())!='$')
System.out.print((char)c);
System.out.println("Data reading complete...");
s.close();
ss.close();
}
}
Run the server program first...then the client.
If you run the client first you'll get an exception...
There you go... a server-client program :)
Cheers :)
Tuesday, December 8, 2009
The thought came to me...
THE THOUGHT CAME TO ME....
The thought came to me
when I saw the countdown
at one corner of the
chalkboard.
The thought came to me
when my friends
started filling in
slambooks.
I could feel
the chilled air flow in
and give me
goosebumps.
It felt like something crushed my heart.
I could clearly hear the sound
of my heartbeat,
as it bet
harder and harder.
The very notion of us separating
is hurting me.
But what can we do...
its just part of our destiny.
I watched the seasons go by,
I watched the time flow by,
I watched my people change,
Yet they never left me unchanged.
And now, I have to watch them go...
All the faces seen now, will be unseen in a few days.
I'll miss all my friends...
friends I've talked to
and friends I haven't talked to.
I thank my college...
it brought us all closer.
But I hate my college...
coz now its tearing us apart.
I'm just another guy
who sits in one corner of the classroom...
always present and seen
but usually unknown.
I've observed my people
around me...
found joy in their joy,
found sadness in their sadness.
I'm almost into tears
when I write this unrythmically sound poem,
which in short...
says...
I've loved you all...
And I'll always
love you all.
WITH LOVE,
---- CHIKA --->
The thought came to me
when I saw the countdown
at one corner of the
chalkboard.
The thought came to me
when my friends
started filling in
slambooks.
I could feel
the chilled air flow in
and give me
goosebumps.
It felt like something crushed my heart.
I could clearly hear the sound
of my heartbeat,
as it bet
harder and harder.
The very notion of us separating
is hurting me.
But what can we do...
its just part of our destiny.
I watched the seasons go by,
I watched the time flow by,
I watched my people change,
Yet they never left me unchanged.
And now, I have to watch them go...
All the faces seen now, will be unseen in a few days.
I'll miss all my friends...
friends I've talked to
and friends I haven't talked to.
I thank my college...
it brought us all closer.
But I hate my college...
coz now its tearing us apart.
I'm just another guy
who sits in one corner of the classroom...
always present and seen
but usually unknown.
I've observed my people
around me...
found joy in their joy,
found sadness in their sadness.
I'm almost into tears
when I write this unrythmically sound poem,
which in short...
says...
I've loved you all...
And I'll always
love you all.
WITH LOVE,
---- CHIKA --->
My First Post - Well Done Chika !!!
hi everyone.... i am shrikant.... and this is my first blog post :) ..... taliyaan , taliyaan... claps , claps!!! yeah... passed out of college .... K.L. College of Enggineering working in Infosys Technologies Ltd. :)
My friends call me Chika... during college "shrikant" somehow changed to "chika". But a friend of mine.... prashant a.k.a "prashy" .... whose birthday is TODAY....."Happy B'day PRASHY!!!" .... told that "Chika" actually has a meaning.... it means a "small monkey". Many of you may take "chika" in the spanish version as a "cute, sausy and hot chick".... let me enlighten you all... i am a GUY !!!
Life has purpose.... and so should every action. And here was I till yesterday.... doing nothing in my free time.... so instead i thought i'd do something for the community. Yes!!! blogging , i believe , can be a community service.... many of you may disagree.... "Prashy... hi again" ... who actually do not support blogging much :) . As for all the professional bloggers out there , reviewing my blog and admonishing me for writing crap in the name of blogging.... please excuse me but i shall improvize :)
Through my blog i shall try to take you through the latest developments in technology , fashion , college buzz , carreer opportunities , spirituality (if i may) , events , health etc... so feel free to reply to my post , update me with your ideas and information :)
Party time.... itz december guys n gals.... and with Christmas n New Year fast approaching we have Bacardi Blast music festival in your town.... popular DJs performing live acts in Goa on dec 19th and 20th.... Chicane , Sister Bliss , shaa'ir + func , order of essence , b.l.o.t , lost stories and Ministry of Sound..... right on dude!!!
For those who think Goa is a distance.... we have Megamix in Mysore .... n performing live are DJ Ivan and Medieval Punditz on 12th Dec.... and Ministry of Sound in Delhi and Hyderabad..... venue not yet decided.... so wait until my next post for the update :)
Have a blast ..... :)
My friends call me Chika... during college "shrikant" somehow changed to "chika". But a friend of mine.... prashant a.k.a "prashy" .... whose birthday is TODAY....."Happy B'day PRASHY!!!" .... told that "Chika" actually has a meaning.... it means a "small monkey". Many of you may take "chika" in the spanish version as a "cute, sausy and hot chick".... let me enlighten you all... i am a GUY !!!
Life has purpose.... and so should every action. And here was I till yesterday.... doing nothing in my free time.... so instead i thought i'd do something for the community. Yes!!! blogging , i believe , can be a community service.... many of you may disagree.... "Prashy... hi again" ... who actually do not support blogging much :) . As for all the professional bloggers out there , reviewing my blog and admonishing me for writing crap in the name of blogging.... please excuse me but i shall improvize :)
Through my blog i shall try to take you through the latest developments in technology , fashion , college buzz , carreer opportunities , spirituality (if i may) , events , health etc... so feel free to reply to my post , update me with your ideas and information :)
Party time.... itz december guys n gals.... and with Christmas n New Year fast approaching we have Bacardi Blast music festival in your town.... popular DJs performing live acts in Goa on dec 19th and 20th.... Chicane , Sister Bliss , shaa'ir + func , order of essence , b.l.o.t , lost stories and Ministry of Sound..... right on dude!!!
For those who think Goa is a distance.... we have Megamix in Mysore .... n performing live are DJ Ivan and Medieval Punditz on 12th Dec.... and Ministry of Sound in Delhi and Hyderabad..... venue not yet decided.... so wait until my next post for the update :)
Have a blast ..... :)
Subscribe to:
Comments (Atom)