java.io : Class FileInputStream

http://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html

FileInputStream: Obtains input bytes from a file in the file system. Meant for reading streams of raw bytes.

FileInputStream(File file) –> creates a FileInputStream by opening a connection to an actual file, where file specified by FILE object file in the file system …

Reading from FileInputStream:

FileInputStream fis = new FileInputStream(“test.txt”);

byte[] b = new byte[ fis.available() ];

int ch;  // first way

while( (ch=fis.read() ) != -1){

System.out.println((char)ch);

}

fis.read(b);  // second way

for(int i=0;i<b.length;i++){

System.out.println( (char)b[i] );

}

fis.read(b,0,b.length); // third way

….

Put any one method of avobe inside try block

try{

}

} catch (IOException e) {

   e.printStackTrace();

} finally {

       try {

             if (fis != null)

                  fis.close();

       } catch (IOException ex) {

               ex.printStackTrace();

       }

} // end of finally

 

LYCOG : Multicast Programming with Java

Multicast Programming with Java

Multicast is a special feature of UDP protocol that enables programmer to send messages to a group of receivers on a special multicast IP ADDRESS and PORT.. .. ..

 

” If multiple  MulticastSockets bind to the same port and join the same multicast group, they will all
receive copies of multicast traffic sent to that group/port.”

multicast is the delivery of information to multiple destinations simultaneously using the most efficient strategy to deliver the messages over the network over each link of the network only once and only create copies when the links to the destinations split

oAuth

Introduction

A web user (resource owner) can grant a printing service (client) access to her private photos stored at a photo sharing service (server), without sharing her username and password with the printing service.

” In the traditional client-server authentication model, the client uses its credentials to access its resources hosted by the server. OAuth introduces a third role to this model: the resource owner. In the OAuth model, the client (which is not the resource owner, but is acting on its behalf) requests access to resources controlled by the resource owner, but hosted by the server.”

Writing a Datagram Client and Server

LINK:  http://docs.oracle.com/javase/tutorial/networking/datagrams/clientServer.html

QuoteServer:

package avc.datagram;

import java.io.*;

public class QuoteServer {
public static void main(String[] args) throws IOException {
new QuoteServerThread().start();
}
}

QuoteServerThread

package avc.datagram;

import com.sun.org.apache.xpath.internal.operations.Quo;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class QuoteServerThread extends Thread {

public static final int PORT = 32214;

protected DatagramSocket socket = null;
protected BufferedReader in = null;
protected boolean moreQuotes = true;

public QuoteServerThread() throws IOException {
this(“QuoteServerThread”);
}

public QuoteServerThread(String name) throws IOException {
super(name);
socket = new DatagramSocket(PORT);

try {
in = new BufferedReader(new FileReader(“D:\\one-liners.txt”));
} catch (FileNotFoundException e) {
System.err.println(“Could not open quote file. Serving time instead.”);
}
}

public void run() {

while (moreQuotes) {
try {
byte[] buf = new byte[256];

// receive request
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);

// figure out response
String response= null;
if (in == null)
response = new Date().toString();
else
response = getNextQuote();

buf = response.getBytes();

// send the response to the client at “address” and “port”
InetAddress address = packet.getAddress();
int port = packet.getPort();
packet = new DatagramPacket(buf, buf.length, address, port);
socket.send(packet);
try {
sleep(1000);
// try {
// wait(1000);
// } catch (InterruptedException ex) {
// }
// }
} catch (InterruptedException ex) {
ex.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
moreQuotes = false;
}
}
socket.close();
}

protected String getNextQuote() {
String returnValue = null;
try {
if ((returnValue = in.readLine()) == null) {
in.close();
moreQuotes = false;
QouteClient.continu = false;
returnValue = “No more quotes. Goodbye.”;
}
} catch (IOException e) {
returnValue = “IOException occurred in server.”;
}
return returnValue;
}
}

}

}

}

}

 

QuoteClient

package avc.sample.datagram;
import java.io.*;
import java.net.*;
import java.util.*;

public class QouteClient {
public static boolean continu = true;
public static void main(String[] args) throws IOException {

// if (args.length != 1) {
// System.out.println(“Usage: java QuoteClient <hostname>”);
// return;
// }

// get a datagram socket
DatagramSocket socket = new DatagramSocket();

// send request
byte[] buf = new byte[256];

while(continu){
InetAddress address = InetAddress.getByName(“localhost”);
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, QuoteServerThread.PORT);
socket.send(packet);

// get response
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);

// display response
String received = new String(packet.getData(), 0, packet.getLength());
System.out.println(“Quote of the Moment: ” + received);
}
System.out.println(“Fully read the one-lines.txt file .. now terminating and closing the socket”);
socket.close();
}
}

 

Merge Sort in C

#include<stdio.h>

#define SIZE 20

void read_elements(int a[],int n);
void mergesort(int a[],int p,int q);
void merge_(int a[],int p,int q,int r);
void print_elements(int a[],int n);

int main(){
int a[SIZE]; // array to be sorted
int n; //number of elements to be sorted
printf(“Enter #of elements to be sorted..\n”);
scanf(“%d”,&n);
read_elements(a,n);
printf(“\nelements.. before sort\n”);
print_elements(a,n);
mergesort(a,0,n-1);
printf(“\nSorted elements are ..\n”);
print_elements(a,n);
}

void read_elements(int a[],int n){
for(int i=0;i<n;i++)
scanf(“%d”,&a[i]);
}

void mergesort(int a[],int p,int r){
int q;
if(p<r){
q = (p+r)/2;
mergesort(a,p,q);
mergesort(a,q+1,r);
merge_(a,p,q,r); //_ for to avoid system merge definition
}
}

void merge_(int a[],int p,int q,int r){
int n1 = q-p+1;
int n2 = r-q;
int L[n1+1];
int R[n2+1];
int i,j;
for(i=0;i<n1;i++)
L[i] = a[p+i];
for(j=0;j<n2;j++)
R[j] = a[q+j+1];
L[i] = 999;
R[j] = 999;
i = j = 0;
for(int k=p;k<=r;k++){
if(L[i]<=R[j]){
a[k] = L[i++] ;
}
else{
a[k] = R[j++];
}
}
}

void print_elements(int a[],int n){
printf(“\n”);
for(int i=0;i<n;i++)
printf(” %d “,a[i]);
printf(“\n”);
}