<?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>mythrobin•com &#187; socket programming</title>
	<atom:link href="http://www.mythrobin.com/archives/tag/socket-programming/feed" rel="self" type="application/rss+xml" />
	<link>http://www.mythrobin.com</link>
	<description>where myth becomes real</description>
	<lastBuildDate>Sat, 21 Jan 2012 08:32:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Java Socket Programming &#8211; Simple Client/Server Chat Program</title>
		<link>http://www.mythrobin.com/archives/129</link>
		<comments>http://www.mythrobin.com/archives/129#comments</comments>
		<pubDate>Wed, 24 Feb 2010 18:14:23 +0000</pubDate>
		<dc:creator>Robin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[client/server]]></category>
		<category><![CDATA[java chat]]></category>
		<category><![CDATA[server chat]]></category>
		<category><![CDATA[socket programming]]></category>

		<guid isPermaLink="false">http://www.mythrobin.com/?p=129</guid>
		<description><![CDATA[Open up the two java programs given below (SimpleServer &#38; SimpleClient) using your JAVA IDE (I prefer JCreator) separately. Buid it and run the SimpleServer<a href="http://www.mythrobin.com/archives/129"> <br /><br /> (Read More)…</a>]]></description>
			<content:encoded><![CDATA[<p>Open up the two java programs given below (SimpleServer &amp; SimpleClient) using your JAVA IDE (I prefer JCreator) separately. Buid it and run the SimpleServer 1st then run SimpleClient. Now start chating.</p>
<p><img class="alignnone size-full wp-image-130" title="SimpleServer" src="http://www.mythrobin.com/wp-content/uploads/2010/02/1.JPG" alt="SimpleServer" width="504" height="656" /></p>
<p><img class="alignnone size-full wp-image-131" title="SimpleClient" src="http://www.mythrobin.com/wp-content/uploads/2010/02/2.JPG" alt="SimpleClient" width="507" height="656" /></p>
<p><span style="color: #3366ff;"><strong>SimpleServer Source Code: </strong></span></p>
<blockquote><p><strong><br />
</strong><br />
import java.net.*;<br />
import java.io.*;<br />
import java.util.*;<br />
import java.text.DateFormat;<br />
public class SimpleServer {<br />
public static void main(String args[]) {<br />
ServerSocket s = null;</p>
<p>//Input From Keyboard<br />
String str;<br />
DataInputStream indata= new  DataInputStream (System.in);<br />
System.out.println(&#8220;Type in Something &amp; Press Enter to Send it To The &gt;&gt;C L I E N T&lt;&lt;: &#8220;);<br />
// Register your service on port 5432<br />
try {<br />
s = new ServerSocket(5432);<br />
} catch (IOException e) {<br />
// ignore<br />
}<br />
// Run the listen/accept loop forever<br />
while (true) {<br />
try {<br />
// Wait here and listen for a connection<br />
Socket s1 = s.accept();<br />
// Get output stream associated with the socket<br />
OutputStream s1out = s1.getOutputStream();<br />
DataOutputStream dos = new DataOutputStream(s1out);</p>
<p>System.out.println();<br />
System.out.println(&#8220;Write Something: &#8220;);<br />
str=indata.readLine();<br />
dos.writeUTF(str);</p>
<p>// Get an input stream from the socket<br />
InputStream is = s1.getInputStream();</p>
<p>// Decorate it with a &#8220;data&#8221; input stream<br />
DataInputStream dis = new DataInputStream(is);<br />
// Read the input and print it to the screen<br />
System.out.println(&#8220;Incoming From Client&gt;&gt;&gt;:&#8221; +dis.readUTF());<br />
//Display System Date<br />
DateFormat defaultDate = DateFormat.getDateInstance();<br />
System.out.println(defaultDate.format(new Date()));<br />
//Display System Time<br />
DateFormat shortTime = DateFormat.getTimeInstance(DateFormat.SHORT);<br />
System.out.println(shortTime.format(new Date()));</p>
<p>// Close the connection, but not the server socket<br />
dos.close();<br />
s1.close();<br />
dis.close();<br />
} catch (IOException e) {<br />
// ignore<br />
}<br />
}<br />
}<br />
}<br />
<strong><br />
</strong></p></blockquote>
<p><span style="color: #3366ff;"><strong>SimpleClient Source Code</strong></span></p>
<blockquote><p>import java.net.*;<br />
import java.io.*;<br />
import java.util.*;<br />
import java.text.DateFormat;<br />
public class SimpleClient {<br />
public static void main(String args[]) {</p>
<p>//Input From Keyboard<br />
String str;<br />
DataInputStream indata= new  DataInputStream (System.in);</p>
<p>System.out.println(&#8220;Type in Something &amp; Press Enter to Send it To The &gt;&gt;S E R V E R&lt;&lt;: &#8220;);</p>
<p>while(true){</p>
<p>try {</p>
<p>// Open your connection to a server, at port 5432</p>
<p>// localhost used here</p>
<p>Socket s1 = new Socket(&#8220;127.0.0.1&#8243;, 5432);</p>
<p>// Get an input stream from the socket</p>
<p>InputStream is = s1.getInputStream();</p>
<p>// Decorate it with a &#8220;data&#8221; input stream</p>
<p>DataInputStream dis = new DataInputStream(is);</p>
<p>// Read the input and print it to the screen</p>
<p>System.out.println(&#8220;Incoming From Server&gt;&gt;&gt;:&#8221; +dis.readUTF());<br />
//Display System Date<br />
DateFormat defaultDate = DateFormat.getDateInstance();<br />
System.out.println(defaultDate.format(new Date()));<br />
//Display System Time<br />
DateFormat shortTime = DateFormat.getTimeInstance(DateFormat.SHORT);<br />
System.out.println(shortTime.format(new Date()));</p>
<p>// Get output stream associated with the socket<br />
OutputStream s1out = s1.getOutputStream();<br />
DataOutputStream dos = new DataOutputStream(s1out);<br />
System.out.println();<br />
System.out.println(&#8220;Write Something: &#8220;);<br />
str = indata.readLine();<br />
dos.writeUTF(str);</p>
<p>// When done, just close the steam and connection<br />
dis.close();<br />
dos.close();<br />
s1.close();<br />
} catch (ConnectException connExc) {<br />
System.err.println(&#8220;Could not connect to the server.&#8221;);<br />
} catch (IOException e) {<br />
// ignore<br />
}<br />
}<br />
}<br />
}</p></blockquote>
<p>Enjoy. Don&#8217;t forget to give me feedback. If you face any difficulties please let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mythrobin.com/archives/129/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

