<?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>systemBash &#187; Java</title>
	<atom:link href="http://systembash.com/tags/code-samples/java-code-samples/feed/" rel="self" type="application/rss+xml" />
	<link>http://systembash.com</link>
	<description>Technology and System Administration</description>
	<lastBuildDate>Sat, 27 Feb 2010 02:12:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<atom:link rel='hub' href='http://systembash.com/?pushpress=hub'/>
		<item>
		<title>A Simple Java TCP Server and TCP Client</title>
		<link>http://systembash.com/content/a-simple-java-tcp-server-and-tcp-client/</link>
		<comments>http://systembash.com/content/a-simple-java-tcp-server-and-tcp-client/#comments</comments>
		<pubDate>Thu, 18 Sep 2008 13:01:30 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[tcp]]></category>

		<guid isPermaLink="false">http://systembash.com/?p=244</guid>
		<description><![CDATA[Following up on my previous post, we also had to demonstrate a sample Java TCP Server and TCP Client. They are pretty small and give you a good idea about how a TDP Server opens up a port, and then the TCP Client sends or receives data from that port.
This is a good page on [...]]]></description>
			<content:encoded><![CDATA[<p>Following up on my <a href="http://systembash.com/content/a-simple-java-udp-server-and-udp-client/">previous post</a>, we also had to demonstrate a sample Java TCP Server and TCP Client. They are pretty small and give you a good idea about how a TDP Server opens up a port, and then the TCP Client sends or receives data from that port.</p>
<p>This is a good page on the <a href="http://skullbox.net/tcpudp.php">differences between TCP and UDP</a>.</p>
<p>To compile these, install <a href="http://java.sun.com">Java JDK</a> to your system. Then compile the program with “javac TCPClient.java” &#8211; this will create a TCPClient.class. Execute the file with “java TCPClient” &#8211; leave off the .class, or you will get the error: <em>“Exception in thread “main” java.lang.NoClassDefFoundError”</em>.</p>
<p>Here is the sample code:</p>
<p><strong>TCPServer.java</strong></p>
<pre class="prettyprint">import java.io.*;
import java.net.*;

class TCPServer
{
   public static void main(String argv[]) throws Exception
      {
         String clientSentence;
         String capitalizedSentence;
         ServerSocket welcomeSocket = new ServerSocket(6789);

         while(true)
         {
            Socket connectionSocket = welcomeSocket.accept();
            BufferedReader inFromClient =
               new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            clientSentence = inFromClient.readLine();
            System.out.println("Received: " + clientSentence);
            capitalizedSentence = clientSentence.toUpperCase() + '\n';
            outToClient.writeBytes(capitalizedSentence);
         }
      }
}</pre>
<p>and the client:</p>
<p><strong>TCPClient.java</strong></p>
<pre class="prettyprint">import java.io.*;
import java.net.*;

class TCPClient
{
 public static void main(String argv[]) throws Exception
 {
  String sentence;
  String modifiedSentence;
  BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
  Socket clientSocket = new Socket("localhost", 6789);
  DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
  BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  sentence = inFromUser.readLine();
  outToServer.writeBytes(sentence + '\n');
  modifiedSentence = inFromServer.readLine();
  System.out.println("FROM SERVER: " + modifiedSentence);
  clientSocket.close();
 }
}</pre>
<p>If you have any questions, please leave a comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://systembash.com/content/a-simple-java-tcp-server-and-tcp-client/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>A Simple Java UDP Server and UDP Client</title>
		<link>http://systembash.com/content/a-simple-java-udp-server-and-udp-client/</link>
		<comments>http://systembash.com/content/a-simple-java-udp-server-and-udp-client/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 10:58:29 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[communications]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[tcp/ip]]></category>

		<guid isPermaLink="false">http://systembash.com/?p=236</guid>
		<description><![CDATA[For a class I am taking, we are testing out a simple UDP Server and UDP Client to demonstrate what each one does and how sockets work. They are pretty small and give you a good idea about how a UDP Server opens up a port, and then the UDP Client sends or receives data [...]]]></description>
			<content:encoded><![CDATA[<p>For a class I am taking, we are testing out a simple UDP Server and UDP Client to demonstrate what each one does and how sockets work. They are pretty small and give you a good idea about how a UDP Server opens up a port, and then the UDP Client sends or receives data from that port.</p>
<p>To compile these, install Java JDK to your system. Then compile the program with &#8220;javac UDPClient.java&#8221; &#8211; this will create a UDPClient.class. Execute the file with &#8220;java UDPClass&#8221; &#8211; leave off the .class, or you will get the error: <em>&#8220;Exception in thread &#8220;main&#8221; java.lang.NoClassDefFoundError&#8221;</em>.</p>
<p>Here is the sample code:</p>
<p><strong>UDPServer.java:</strong></p>
<pre class="prettyprint">import java.io.*;
import java.net.*;

class UDPServer
{
   public static void main(String args[]) throws Exception
      {
         DatagramSocket serverSocket = new DatagramSocket(9876);
            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];
            while(true)
               {
                  DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                  serverSocket.receive(receivePacket);
                  String sentence = new String( receivePacket.getData());
                  System.out.println("RECEIVED: " + sentence);
                  InetAddress IPAddress = receivePacket.getAddress();
                  int port = receivePacket.getPort();
                  String capitalizedSentence = sentence.toUpperCase();
                  sendData = capitalizedSentence.getBytes();
                  DatagramPacket sendPacket =
                  new DatagramPacket(sendData, sendData.length, IPAddress, port);
                  serverSocket.send(sendPacket);
               }
      }
}</pre>
<p><strong>UDPClient.java:</strong></p>
<pre class="prettyprint">import java.io.*;
import java.net.*;

class UDPClient
{
   public static void main(String args[]) throws Exception
   {
      BufferedReader inFromUser =
         new BufferedReader(new InputStreamReader(System.in));
      DatagramSocket clientSocket = new DatagramSocket();
      InetAddress IPAddress = InetAddress.getByName("localhost");
      byte[] sendData = new byte[1024];
      byte[] receiveData = new byte[1024];
      String sentence = inFromUser.readLine();
      sendData = sentence.getBytes();
      DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
      clientSocket.send(sendPacket);
      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
      clientSocket.receive(receivePacket);
      String modifiedSentence = new String(receivePacket.getData());
      System.out.println("FROM SERVER:" + modifiedSentence);
      clientSocket.close();
   }
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://systembash.com/content/a-simple-java-udp-server-and-udp-client/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
