<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java Interview Questions &#187; Client/Servler Communication</title>
	<atom:link href="http://www.bestjavainterviewquestions.com/category/java-util-questions/clientservler-communication/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bestjavainterviewquestions.com</link>
	<description>Java Interview Questions &#124; Core Java Interview Questions &#124; Advanced Java Interview Questions &#124; EJB Interview Questions &#124; J2EE Interview Questions &#124; Hibernate Interview Questions</description>
	<lastBuildDate>Fri, 30 Jul 2010 06:00:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Sockets programming in Java for Client/Server</title>
		<link>http://www.bestjavainterviewquestions.com/sockets-programming-in-java-for-clientserver/</link>
		<comments>http://www.bestjavainterviewquestions.com/sockets-programming-in-java-for-clientserver/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 16:53:37 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Client/Servler Communication]]></category>
		<category><![CDATA[client/server programming]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=519</guid>
		<description><![CDATA[Note: Establishing Communication between a Server and a Client through a socket is called Socket Programming To create the Client Socket Socket Class is available in java.net package. To create ServerSocket, ServerSocket class is avialable in java.net package A Client is a machine that sends a request for some service A Server is machine that [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Note:</strong></p>
<p>Establishing Communication between a Server and a Client through a socket is called Socket Programming</p>
<ul>
<li>To create the Client Socket <em>Socket Class is available in java.net package.</em></li>
<li>To create ServerSocket, <em>ServerSocket </em>class is avialable in <em>java.net package</em></li>
<li>A Client is a machine that sends a request for some service</li>
<li>A Server is machine that provides services to the client</li>
</ul>
<p><em><strong><br />
</strong></em></p>
<p><em><strong>Note:  Write a java program to create a client and server Communication between them</strong></em></p>
<p><strong>// A Server that sends a String to the client as a response to the client connection<br />
</strong></p>
<p><em>import java.io.*;<br />
import java.net.*;</em></p>
<p><em>class Server1<br />
{<br />
public static void main(String args[])throws IOException<br />
{</em></p>
<p><em>//create the serversocket</em></p>
<p><em>ServerSocket ss=new ServerSocket(777);</em></p>
<p><em>//make the socket to accept the client connection</em></p>
<p><em>Socket s=ss.accept();<br />
System.out.println(&#8220;connection established&#8221;);</em></p>
<p><em>//flow of data in the socket</em></p>
<p><em>OutputStream obj=s.getOutputStream();</em></p>
<p><em>//to send data to the client</em></p>
<p><em>PrintStream ps=new PrintStream(obj);</em></p>
<p><em>//to receive data from the client</em></p>
<p><em>BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));</em></p>
<p><em>//to read data from keyboard</em></p>
<p><em>BufferedReader kb=new BufferedReader(new InputStreamReader(System.in));<br />
while(true)<br />
{<br />
String str,str1;<br />
while((str=br.readLine())!=null)<br />
{<br />
System.out.println(str);<br />
str1=kb.readLine();<br />
ps.println(str1);<br />
}</em></p>
<p><em>//disconnet server</em></p>
<p><em>ss.close();<br />
s.close();<br />
br.close();<br />
kb.close();<br />
ps.close();<br />
System.exit(0);<br />
}<br />
}<br />
}</em></p>
<p><strong>// A Client that sends a Request to the Server for the response<br />
</strong></p>
<p><em>import java.io.*;<br />
import java.net.*;</em></p>
<p><em>class Client1<br />
{<br />
public static void main(String args[])throws IOException<br />
{</em></p>
<p><em>//create client socket</em></p>
<p><em>Socket s=new Socket(&#8220;localhost&#8221;,777);</em></p>
<p><em>//data send by the client the dos is most preferable</em></p>
<p><em>DataOutputStream dos=new DataOutputStream(s.getOutputStream());<br />
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));<br />
BufferedReader kb=new BufferedReader(new InputStreamReader(System.in));<br />
String str,str1;<br />
while(!(str=kb.readLine()).equals(&#8220;exit&#8221;))<br />
{<br />
dos.writeBytes(str+&#8221;\n&#8221;);<br />
str1=br.readLine();<br />
System.out.println(str1);<br />
}</em></p>
<p><em>//disconnect the client</em></p>
<p><em>dos.close();<br />
br.close();<br />
kb.close();<br />
}<br />
}</em></p>
<p><strong>Procedure of Compiling  and executing the program</strong></p>
<ol>
<li>First Compile the Client Program and wait for the server compilation<strong><em> javac Client1.java </em></strong></li>
<li>Secound Compile the Server Program and wait for the client compilation <em><strong>javac Server1.java</strong></em></li>
<li>and Run the server wait for the request from the client  <strong><em>java Server1.</em></strong></li>
<li>But don&#8217;t run the Server program before the Compilation of the Client compilation</li>
<li>Run the Client and send the Request to the Server which is waiting for the connection to be establish by <strong><em>java Client1.</em></strong></li>
<li>After the client sends the request the server will show the message as the Connection is established and ready for the communication.</li>
</ol>
<p><strong><br />
</strong></p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li><a href="http://www.bestjavainterviewquestions.com/java-multi-thread-questions/" title="Java Multi-Thread Questions"><img src="Array" alt="Java Multi-Thread Questions" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/example-programs-of-lifecycle-of-servlets/" title="Example programs of lifecycle of servlets"><img src="Array" alt="Example programs of lifecycle of servlets" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/what-are-synchronized-methods-and-synchronized-statements/" title="What are synchronized methods and synchronized statements?"><img src="Array" alt="What are synchronized methods and synchronized statements?" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/why-does-ejb-needs-two-interfaces/" title="Why does EJB needs two interfaces"><img src="Array" alt="Why does EJB needs two interfaces" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/applet-interview-question/" title="Applet Interview Question"><img src="Array" alt="Applet Interview Question" /></a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/sockets-programming-in-java-for-clientserver/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
