Java Networking, As everyone know that networking means connecting one or more systems together to share the resources, the advantage of Java networking is that centralize the software management. The networking classes and interfaces will be available in the java.net package and provide the support for the two protocols, they are as follows.
TCP stand for Transmission Control Protocol which is used to provide communication between two applications.
UDP stands for User Datagram Protocol which is used to transmit packets of data between an application.
Networking Terminology
Description
While working with Java Networking, following are the terms need to know.
IP Address is just a unique address, it will be separated by dot, it ranges from 0 to 255.
Protocol is nothing but set of rules used for the communication, for example: TCP, FTP, SMTP.
Port Number will be associated with IP Address to provide the communication between two applications.
MAC Address stands for Media Access Control which is uses as network address for IEEE 802 network technologies.
Java Socket programming
Description
Java Networking, The purpose of socket programming is that providing communication between two applications running on various JRE. It may be a connection oriented and connection less. connection oriented socket programming uses Socket and ServerSocket classes. The connection-less socket programming uses DatagramSocket and DatagramPacket classes.
Example
Java Networking, Following is an example to understand the concept of socket programming. Here client is going to send the message and server will receives that.
MyServer.java
[java]package socket;
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args){
try{
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();//establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}catch(Exception e){System.out.println(e);}
}
} [/java]
An application uses a data output stream to write data that can later be read by a DataInputStream.
[java]DataInputStream dis=new DataInputStream(s.getInputStream()); [/java]
MyClient.java
[java]package socket;
import java.io.*;
import java.net.*;
public class MyClient {
public static void main(String[] args) {
try{
Socket s=new Socket("localhost",6666);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Splessons");
dout.flush();
dout.close();
s.close();
}catch(Exception e){System.out.println(e);}
}
} [/java]
Output
When compile the code result will be as follows.
[java]message= Hello Splessons[/java]
The following is an example to find the ipaddress of the system.
[java]
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IPTest {
public static void main(String args[]) throws UnknownHostException {
InetAddress addr = InetAddress.getLocalHost();
//Getting IPAddress of localhost - getHostAddress return IP Address
// in textual format
String ipAddress = addr.getHostAddress();
System.out.println("IP address of localhost from Java Program: " + ipAddress);
//Hostname
String hostname = addr.getHostName();//Used to get the host name
System.out.println("Name of hostname : " + hostname);
}
}
[/java]
Output:
Now compile the code result will be as follows.
[java]
IP address of localhost from Java Program: 192.168.1.23
Name of hostname : SPlessons
[/java]
Summary
Key Points
Socket is simply an endpoint of communication between machines.
The connect() method is used to connect socket to a server.