Sunday, April 20, 2014

Socket Programming with File Handling

/*Client Socket program that accepts file name from Client and send it to Server, Server Side Socket reads the Content of a file and display them onto the Client Socket*/

/*Server Program (severprac.java)*/


import java.io.*;
import java.net.*;

public class serverprac
{

public static void main(String[] args) {

ServerSocket ss;
Socket client;
String nm;
try
{
ss = new ServerSocket(1212);
System.out.println("Server is Waitng For Client");
client = ss.accept();
System.out.println("Client is Connected");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br1 = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintStream ps = new PrintStream(client.getOutputStream());

String filenm = br1.readLine();

File f = new File(".");
File[] flist = f.listFiles();

for(File n:flist)
{
nm = n.getName();
if(nm.equals(filenm))
{

System.out.println("File is Present");
FileReader fr = new FileReader(nm);
BufferedReader bfr = new BufferedReader(fr);
String lne;
while(true)
{


lne = bfr.readLine();

                if (lne == null)
                break;
//System.out.println(lne);

ps.println(lne);
}
}
}


ps.close();
br1.close();
br.close();
ss.close();

}//try
catch(Exception e)
{
System.out.println(e);
}
}//main
}

/*Client Program (clientprac.java)*/

import java.io.*;
import java.net.*;

public class clientprac
{

public static void main(String[] args)
{
Socket s;
try
{
s = new Socket("127.0.0.1",1212);
System.out.println("Clent is Conneccted to the Server");

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintStream ps = new PrintStream(s.getOutputStream());
System.out.println("Enter File name");
ps.println(br.readLine());
while(br1.readLine() !=null)
System.out.println(br1.readLine());
br1.close();
ps.close();
br.close();
br1.close();
s.close();

}
catch(Exception e)
{
System.out.println(""+e);
}//catch
}//main
}

Saturday, March 29, 2014

Inter - Thread communication using wait(), notify() and notify all() method in Java

/*
Write a program to illustrate  inter - thread communication using wait(), notify() and notify all() method
*/

import java.io.*;
class q
{
String msg;
boolean flag;
q()
{
flag=false;
}
synchronized void get()
{
if(flag==false)
{
try
{
wait();
}//try
catch(InterruptedException e)
{

}//catch

}
flag=false;
System.out.println("Received Message"+msg);
notify();
}
synchronized void put(String x)
{
if(flag==true)
{
try
{
wait();
}
catch(InterruptedException e)
{

}//catch

}
msg = x;
flag = true;

if(msg.equalsIgnoreCase("Good Bye"))
System.exit(0);
notify();
}
}


class sender extends Thread{
q q1;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
sender(q x)
{
q1 = x;
start();
}
public void run()
{
while(true)
{
System.out.println("Enter Message ");
try
{
s = br.readLine();
q1.put(s);
}
catch(IOException e)
{

}//catch
}//while
}//run
}

class receiver extends Thread
{
q q2;
receiver(q x)
{
q2 = x;
start();

}

public void run()
{
while(true)
{
q2.get();
}
}
}

class fourteen
{
public static void main(String args[])
{
q q4 = new q();
sender s = new sender(q4);
receiver r = new receiver(q4);
}
}

User Defined Exception Example in Java

/*
Write a class Student with attributes roll no, name, age and course. Initialize values through parameterized constructor. If age of student is not in between 15 and 21 then generate user-defined exception “Age Not Within The Range”.If name contains numbers or special symbols raise exception “Name not valid
*/

import java.io.*;
class AgeNotWithinRangeException extends Exception
{
AgeNotWithinRangeException()
{
super();
}
}

class NameNotValidException extends Exception
{
NameNotValidException()
{
super();
}
}
class Student
{
private
int roll,age;
String name,course;

public
Student(int r,String nm,int ag,String co)
{
roll=r;
name=nm;
age=ag;
course=co;

System.out.println("Roll Number is "+roll);
System.out.println("Name is "+name);;
System.out.println("Age is "+age);
System.out.println("Course is "+course);
}

public static void main(String args[]) throws IOException
{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int roll,age;
String name,course;
System.out.println("Enter No. :");
roll = Integer.parseInt(br.readLine());
System.out.println("Enter Name :");
name = br.readLine();
try
{
for(int i=0;i<name.length();i++)
{
char c = name.charAt(i);

if((c<65 || c>90) && (c<97 || c>122) && (c!=32))
{
throw new NameNotValidException();
}//if
}//for
}//try
catch(NameNotValidException n)
{
System.out.println("Invalid Name"+n);
}
System.out.println("Enter Age :");
age = Integer.parseInt(br.readLine());
try
{
if(age<15 || age>21)
{
throw new AgeNotWithinRangeException();
}
}//try

catch(AgeNotWithinRangeException e)
{
System.out.println("Invalid Age"+e);
}

System.out.println("Enter Course :");
course = br.readLine();

Student s = new Student(roll,name,age,course);
}
}

Friday, March 28, 2014

Error while Creating New Android Project in Eclipse.

Problem with Eclipse while creating New Android Application 


Solution - In Eclipse go to Window - > Android SDK Manager 
Select one or more SDK Platform and Install them.
Also Update ADT plugins.

Thursday, January 23, 2014

University Rankings For MS in Computer Science (CS) 2013

RankUniversities
1Carnegie Mellon University
2Massachusetts Institute of Technology
3Stanford University
4University of California, Berkeley
5Cornell University
6University of Illinois, Urbana-Champaign
7University of Washington
8Princeton University
9University of Texas, Austin
10Georgia Institute of Technology
11California Institute of Technology
12University of Wisconsin, Madison
13University of Michigan, Ann Arbor
14University of California, Los Angeles
15University of California, San Diego
16University of Maryland, College Park
17Columbia University
18Harvard University
19University of Pennsylvania
20Brown University
21Purdue University, West Lafayette
22Rice University
23University of Massachusetts, Amherst
24University of North Carolina, Chapel Hill
25University of Southern California
26Yale University
27Duke University
28Johns Hopkins University
29New York University
30Ohio State University
31Pennsylvania State University
32Rutgers, the State University of New Jersey
33University of California
34University of Virginia
35Northwestern University
36University of California
37University of Chicago
38University of Minnesota
39University of California
40University of Colorado
41University of Florida
42University of Utah
43Washington University in St. Louis
44Dartmouth College
45Stony Brook University
46Virginia Tech
47Boston University
48North Carolina State University
49Rensselaer Polytechnic Institute
50Texas A&M University
51University of Arizona
52University of Rochester
53Arizona State University
54Indiana University
55University of California
56University of California
57University of Pittsburgh
58Michigan State University
59University of Illinois
60Vanderbilt University
61Northeastern University
62University at Buffalo
63Case Western Reserve University
64George Mason University
65Iowa State UniversityOregon State University
66Syracuse University
67University of Iowa
68University of Notre Dame
69University of Oregon
70University of Tennessee
71Brandeis University
72Naval Postgraduate School
73Polytechnic Institute of New York University
74Tufts University
75University of Connecticut
76University of Delaware
77University of Maryland
78Clemson University
79College of William and Mary
80Colorado State University
81CUNY Graduate School and University Center
82Florida State University
83George Washington University
84Oregon Health and Science University
85University of Kansas
86University of Nebraska
87University of New Mexico
88University of Texas
89Washington State University
90Auburn University
91Brigham Young University
92New Jersey Institute of Technology
93University at Albany
94University of Central Florida
95University of Georgia
96University of Kentucky
97Worcester Polytechnic Institute
98Drexel University
99Georgia State University
100Kansas State University
101Lehigh University
102Louisiana State University
103Stevens Institute of Technology
104University of Cincinnati
105University of North Carolina
106University of South Carolina
107University of South Florida
108University of Texas
109Binghamton University
110Colorado School of Mines
111Illinois Institute of Technology
112Southern Methodist University
113Temple University
114University of Houston
115University of Missouri
116University of Oklahoma
117University of Wisconsin
118Wayne State University
119West Virginia University
120Claremont Graduate University
121Mississippi State University
122Missouri University of Science & Technology
123New Mexico State University
124Texas Tech University
125Wright State University