<?xml version="1.0" encoding="ISO-8859-1"?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
	<channel>
		<title>Java Programming Forums - The Java Community - Java Networking Tutorials</title>
		<link>http://www.javaprogrammingforums.com/</link>
		<description>TCP/IP, Socket, Chat Servers etc</description>
		<language>en</language>
		<lastBuildDate>Wed, 19 Jun 2013 22:25:36 GMT</lastBuildDate>
		<generator>vBulletin</generator>
		<ttl>60</ttl>
		<image>
			<url>http://www.javaprogrammingforums.com/images/misc/rss.png</url>
			<title>Java Programming Forums - The Java Community - Java Networking Tutorials</title>
			<link>http://www.javaprogrammingforums.com/</link>
		</image>
		<item>
			<title>Java networking basics</title>
			<link>http://www.javaprogrammingforums.com/java-networking-tutorials/29937-java-networking-basics.html</link>
			<pubDate>Mon, 10 Jun 2013 00:50:11 GMT</pubDate>
			<description><![CDATA[Before I start i'd like to say that this is 100% MINE I am just transferring it from anarchy forums. 
 
 
JAVA NETWORKING TUTORIAL 
 
Imports Needed:...]]></description>
			<content:encoded><![CDATA[<div>Before I start i'd like to say that this is <font color="#FF4500">100% MINE</font> I am just transferring it from anarchy forums.<br />
<font size="5"><br />
<br />
JAVA NETWORKING TUTORIAL</font><br />
<br />
Imports Needed:<br />
<br />
    Java.io.*<br />
    Java.util.*<br />
    Java.net.*<br />
<br />
Objects Used:<br />
<br />
    InetAddress - creates an IP connection to a specified host<br />
    Inet4Address - Creates an IPv4 connection to a specified host<br />
    Inet6Address - Creates an IPv6 connection to a specified host<br />
    SocketAddress - provides an immutable object used by sockets for binding, connecting, or as returned values. (Abstract class (used with InetAddress))<br />
    Socket - Opens a TCP socket to a specific IP address<br />
    ServerSocket - Creates a server for Sockets to connect to (Only needed for client/server networing)<br />
    DatagramPacket - Creates a UDP packet which connects through the DatagramSocket<br />
    DatagramSocket - creates a UDP socket to send DatagramPackets<br />
<br />
<br />
Examples/explenations of all:<br />
<br />
<br />
<br />
InetAddress:<br />
<br />
<br />
<div class="bbcode_container">
                <div class="bbcode_description">Code :</div>
                <hr /><code class="bbcode_code"><div class="" style="font-family:monospace;"><pre style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">public static void inetmethod() throws UnknownHostException{
InetAddress address =InetAddress.getByName(ADDRESS_AS_STRING_GOES_HERE);
System.out.println(address.getHostAddress() +&quot; || &quot;+ address.getHostName());
    }</pre></div></code><hr />
</div> <br />
The above doe will print out the IP address of anything given and it's host name. For example, if ADDRESS_AS_STRING_GOES_HERE was substituted with &quot;www.google.com&quot; then &quot;173.194.46.20 || http://www.google.com&quot; would print out.<br />
<br />
Inet4Address:<br />
<br />
<br />
<div class="bbcode_container">
                <div class="bbcode_description">Code :</div>
                <hr /><code class="bbcode_code"><div class="" style="font-family:monospace;"><pre style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">import java.net.*;
import java.util.*;
&nbsp;
public class GetAddress {
&nbsp;
    public static void main(String&#91;&#93; args) throws Exception {
        String result = &quot;&quot;;
        InetAddress i = Inet4Address.getByName(&quot;localhost&quot;);
        //returns the raw IP address of object.
        byte b&#91;&#93; = i.getAddress();
         System.out.print(&quot;IP address is: &quot;);
        for (int j = 0; j &lt; b.length; j++) {
            result+= b&#91;j&#93;;
            result+= &quot;.&quot;;
        }
        System.out.print(result);
    }
}</pre></div></code><hr />
</div> <br />
The above code will print out the IPv4 address of any given host. For example, the above code, will print out &quot;IP address is: 127.0.0.1&quot;. However, if we substitute &quot;localhost&quot; with &quot;www.google.com&quot; the output will end up as &quot;IP address is: 173.194.46.20&quot;<br />
<br />
Inet6Address:<br />
<br />
<br />
<br />
<div class="bbcode_container">
                <div class="bbcode_description">Code :</div>
                <hr /><code class="bbcode_code"><div class="" style="font-family:monospace;"><pre style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    public static void Inetsix throws UnknownHostException {
        InetAddress ipv6 = Inet6Address.getByName(ADDRESS_AS_STRING_GOES_HERE);
        System.out.println(ipv6.getHostAddress());
&nbsp;
    }</pre></div></code><hr />
</div> <br />
The above code would print out the IPv6 address of any given host. For example, if you replace ADDRESS_AS_STRING_GOES_HERE with a host that has an IPv6 address, it would print out said IPv6 address. However, due to the fact that I don't know of any IPv6 address using sites/hosts, I can not show you example output.<br />
<br />
<br />
SocketAddress:<br />
<br />
<div class="bbcode_container">
                <div class="bbcode_description">Code :</div>
                <hr /><code class="bbcode_code"><div class="" style="font-family:monospace;"><pre style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    public static void socketaddressmethod(){
        SocketAddress address = new InetSocketAddress(HOST_AS_STRING, PORT_AS_INT);
    }</pre></div></code><hr />
</div> <br />
The above code will set up a new connection to a given host on a given port. For example, if HOST_AS_STRING was replaced with &quot;www.google.com&quot; and PORT_AS_INT was replaced with 80, a new connection would be set up(accessible)(connections are made with a socket) with google on port 80 (<a href="http://www.google.com:80" target="_blank">Google</a>).<br />
<br />
<br />
Socket:<br />
<br />
<br />
<div class="bbcode_container">
                <div class="bbcode_description">Code :</div>
                <hr /><code class="bbcode_code"><div class="" style="font-family:monospace;"><pre style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">public static void SocketExample(){
        SocketAddress address = new InetSocketAddress(HOST_AS_STRING, PORT_AS_INT);
        Socket sock = new Socket();
        try {
            sock.connect(address);
            System.out.println(&quot;Connection made&quot;);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }</pre></div></code><hr />
</div> <br />
The above code will make a connection to any given host/port. For example, if HOST_AS_STRING was replaced with &quot;www.google.com&quot; and PORT_AS_INT was replaced with 80 a new connection would be made with google on port 80 (<a href="http://www.google.com:80" target="_blank">Google</a>) and &quot;Connection made&quot; would be printed to the screen.<br />
<br />
ServerSocket:<br />
<br />
<br />
<div class="bbcode_container">
                <div class="bbcode_description">Code :</div>
                <hr /><code class="bbcode_code"><div class="" style="font-family:monospace;"><pre style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">public static void serversocketexample(){
        try {
            ServerSocket server = new ServerSocket();
            while(true){
            server.accept();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }</pre></div></code><hr />
</div> <br />
The above code will accept all incoming connections from clients to that ip address. Thus, if we use a Socket and change the IP to the IP of the server, the server will willingly accept the connection.<br />
<br />
<br />
DatagramPacket:<br />
<br />
<div class="bbcode_container">
                <div class="bbcode_description">Code :</div>
                <hr /><code class="bbcode_code"><div class="" style="font-family:monospace;"><pre style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">byte&#91;&#93; buffer = new byte&#91;65508&#93;;
InetAddress address = InetAddress.getByName(ADDRESS_AS_STRING);
&nbsp;
DatagramPacket packet = new DatagramPacket(
    buffer, buffer.length, address, PORT_AS_INT);</pre></div></code><hr />
</div> <br />
The above code will make a UDP packet with the size of 65508 and set it up so that when it is sent it will go straight to the given host on the given port. For example, If we changed ADDRESS_AS_STRING to &quot;www.google.com&quot; and PORT_AS_INT to 80, we would have made a large packet set up to send to google on port 80 (not sent until we send it with DatagramSocket).<br />
<br />
<br />
DatagramSocket:<br />
<br />
<br />
<div class="bbcode_container">
                <div class="bbcode_description">Code :</div>
                <hr /><code class="bbcode_code"><div class="" style="font-family:monospace;"><pre style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">DatagramSocket datagramSocket = new DatagramSocket();
&nbsp;
byte&#91;&#93; buffer = MESSAGE_AS_STRING.getBytes();
InetAddress address = InetAddress.getByName(ADDRESS_AS_STRING);
&nbsp;
DatagramPacket packet = new DatagramPacket(
        buffer, buffer.length, address, PORT_AS_INT);
datagramSocket.send(packet);</pre></div></code><hr />
</div> <br />
The above code will send a udp packet with the given message on it, to the given host on the given port. For example, if we changes MESSAGE_AS_STRING to &quot;Hello Java Networking!&quot; and ADDRESS_AS_STRING to &quot;www.google.com&quot; and PORT_AS_INT to 80, we would have sent a UDP packet with the message &quot;Hello Java Networking!&quot; to google on port 80 (<a href="http://www.google.com:80" target="_blank">Google</a>)<br />
<br />
<br />
<br />
<br />
I HOPE THIS TUTORIAL HELPED YOU! KEEP AN EYE OUT FOR MY OTHER NETWORKING TUTORIALS FOR DIFFERENT LANGUAGES!</div>

]]></content:encoded>
			<category domain="http://www.javaprogrammingforums.com/java-networking-tutorials/">Java Networking Tutorials</category>
			<dc:creator>Earthly Minds</dc:creator>
			<guid isPermaLink="true">http://www.javaprogrammingforums.com/java-networking-tutorials/29937-java-networking-basics.html</guid>
		</item>
	</channel>
</rss>
