Showing posts with label Networking. Show all posts
Showing posts with label Networking. Show all posts
Tuesday, April 9, 2013
Today I will tell you how you can send a HTTP request from your simple Java program and then read the data(HTML) sent from the server. This is actually the process which is carried out by browsers when you type the url in its address bar. The browser receives the data and then parses and displays it. Here we will omit the parsing and only print the data received from server on the console. The request to the url here is a HTTP GET request. The following classes are used
java.net.URL : This class represents the Uniform Resource Locator and points to a resource. Here we will use openConnection() method to connect to the url and get a URLConnection reference.
java.ney.URLConnection : The abstract class
--------------------------------------------------------------------------------------------------------------------------
import java.net.URL;
import java.net.URLConnection;
import java.io.*;
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL url = new URL(args[0]); //url read from command line
URLConnection c = url.openConnection(); //connecting to url
BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream())); //stream to resource
String str;
while ((str = in.readLine()) != null) //reading data
System.out.println(str); //printing data read from url to console
in.close(); //closing stream
}
}
java.net.URL : This class represents the Uniform Resource Locator and points to a resource. Here we will use openConnection() method to connect to the url and get a URLConnection reference.
java.ney.URLConnection : The abstract class
URLConnection
is the superclass
of all classes that represent a communications link between the
application and a URL. We will call getInputStream() to get a stream to that url which is used for reading data from url or server.--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------import java.net.URL;
import java.net.URLConnection;
import java.io.*;
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL url = new URL(args[0]); //url read from command line
URLConnection c = url.openConnection(); //connecting to url
BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream())); //stream to resource
String str;
while ((str = in.readLine()) != null) //reading data
System.out.println(str); //printing data read from url to console
in.close(); //closing stream
}
}
Labels:Networking | 2
comments
Friday, March 8, 2013
Today I am going to post programs related with networking. Actually today I am going to implement a connectionless packet delivery service using datagram packets. Each machine will be routed from one machine to another solely on the information contained in the packet. But to deliver this datagrams we will have to open a datagram socket programatically. This socket can be used for both sending and receiving datagrams. But to deliver packets we must know the IP address and port number. The following classes will be used for this -->
java.net.InetAddress : This class is used to get the IP address based on the host name using its static method getByName() .
java.net.DatagramSocket : This class represents a socket for sending and receiving datagram packets. Where possible, a newly constructed DatagramSocket has the SO_BROADCAST socket option enabled so as to allow the transmission of broadcast datagrams. In order to receive broadcast packets a DatagramSocket should be bound to the wildcard address.
java.net.DatagramPacket : Each message is routed from one machine to another based solely on information contained within that packet. Multiple packets sent from one machine to another might be routed differently, and might arrive in any order. Packet delivery is not guaranteed.
--------------------------------------------------------------------------------------------------------------------------
/* THE RECEIVER CODE */
import java.net.DatagramSocket;
import java.net.DatagramPacket;
import java.util.Scanner;
public class Receiver{
public static void main(String[] args)throws Exception{
String str="";
Scanner sc=new Scanner(System.in);
System.out.print("Enter port : ");
int PORT=sc.nextInt(); //receiver port
DatagramSocket rsock=new DatagramSocket(PORT); //creating socket
do{
byte buf[]=new byte[512];
DatagramPacket packet=new DatagramPacket(buf,512);
rsock.receive(packet); //receiving packets
str=new String(buf); //converting to string
System.out.println("Received : "+str);
}while(!str.contains("bye")); //goes on till bye is sent
rsock.close(); //closing socket
}
}
/* THE SENDER CODE */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramSocket;
import java.net.DatagramPacket;
import java.net.InetAddress;
public class Sender{
public static void main(String[] args)throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
DatagramSocket ssock=new DatagramSocket(); //binding socket to any
available port
System.out.print("Enter host name : ");
String str=br.readLine(); //getting host-name
System.out.print("Enter receiver's port : ");
int PORT=Integer.parseInt(br.readLine()); //getting receiver port
InetAddress addr=InetAddress.getByName(str); //getting IP of receiver
do{
System.out.print("Sending : ");
str=br.readLine(); //getting data to be sent
byte buf[]=str.getBytes();
//creating packet with receiver info inside packet
DatagramPacket packet=new DatagramPacket(buf,buf.length,addr,PORT);
ssock.send(packet); //sending packet
}while(!str.equals("bye"));
ssock.close(); //closing socket
}
}
--------------------------------------------------------------------------------------------------------------------------
Screenshots of output
--------------------------------------------------------------------------------------------------------------------------
DOWNLOAD the source package from Mediafire
DOWNLOAD the source package from 4shared
java.net.InetAddress : This class is used to get the IP address based on the host name using its static method getByName() .
java.net.DatagramSocket : This class represents a socket for sending and receiving datagram packets. Where possible, a newly constructed DatagramSocket has the SO_BROADCAST socket option enabled so as to allow the transmission of broadcast datagrams. In order to receive broadcast packets a DatagramSocket should be bound to the wildcard address.
java.net.DatagramPacket : Each message is routed from one machine to another based solely on information contained within that packet. Multiple packets sent from one machine to another might be routed differently, and might arrive in any order. Packet delivery is not guaranteed.
--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------/* THE RECEIVER CODE */
import java.net.DatagramSocket;
import java.net.DatagramPacket;
import java.util.Scanner;
public class Receiver{
public static void main(String[] args)throws Exception{
String str="";
Scanner sc=new Scanner(System.in);
System.out.print("Enter port : ");
int PORT=sc.nextInt(); //receiver port
DatagramSocket rsock=new DatagramSocket(PORT); //creating socket
do{
byte buf[]=new byte[512];
DatagramPacket packet=new DatagramPacket(buf,512);
rsock.receive(packet); //receiving packets
str=new String(buf); //converting to string
System.out.println("Received : "+str);
}while(!str.contains("bye")); //goes on till bye is sent
rsock.close(); //closing socket
}
}
/* THE SENDER CODE */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramSocket;
import java.net.DatagramPacket;
import java.net.InetAddress;
public class Sender{
public static void main(String[] args)throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
DatagramSocket ssock=new DatagramSocket(); //binding socket to any
available port
System.out.print("Enter host name : ");
String str=br.readLine(); //getting host-name
System.out.print("Enter receiver's port : ");
int PORT=Integer.parseInt(br.readLine()); //getting receiver port
InetAddress addr=InetAddress.getByName(str); //getting IP of receiver
do{
System.out.print("Sending : ");
str=br.readLine(); //getting data to be sent
byte buf[]=str.getBytes();
//creating packet with receiver info inside packet
DatagramPacket packet=new DatagramPacket(buf,buf.length,addr,PORT);
ssock.send(packet); //sending packet
}while(!str.equals("bye"));
ssock.close(); //closing socket
}
}
--------------------------------------------------------------------------------------------------------------------------
Output
--------------------------------------------------------------------------------------------------------------------------Screenshots of output
![]() |
Receiver |
![]() |
Sender |
--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------DOWNLOAD the source package from Mediafire
DOWNLOAD the source package from 4shared
Labels:Networking | 1 comments
Subscribe to:
Posts
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...