[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

java programming in network!

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

public class lab8S extends Thread {

public static final void main(String[] args)
{
try{
ServerSocket eSocket = new ServerSocket(2002);
ServerSocket efile=new ServerSocket(2003);
new process1(eSocket.accept()).start();
new fileserver(efile.accept()).start();
try
{
Thread.currentThread().sleep(1000);
}catch(InterruptedException ie)
{
}
}
catch(Exception e)
{
}
}
}
class process1 extends Thread
{
private Socket s=null;
public process1(Socket soc)
{
s=soc;
}

public final void run()
{
try{
ScreenOutThread st=new ScreenOutThread(s.getInputStream());
st.start();
new KeyboardInThread(s.getOutputStream(),st.dname).start();
try
{
Thread.currentThread().sleep(1000);
}catch(InterruptedException e)
{}
}
catch(Exception e)
{
try{
s.close();
}
catch(Exception ex){}
return;
}//catch
}//run()
}//process1




class KeyboardInThread extends Thread {
OutputStream out;
PrintWriter pStream =null;
private static File[] files=null;
private static File[] dirs=null;
public static String dname1;
public KeyboardInThread(OutputStream out,String dd) {
this.out = out;
dname1=dd;
}

public void run() {
try {
try
{
Thread.currentThread().sleep(1000);
}catch(InterruptedException e)
{}

pStream=new PrintWriter(out);
System.out.println(dname1);
File ff=new File(dname1);
files = ff.listFiles(new FileFilter() {
public final boolean accept(File file) {
return file.isFile();}});
dirs = ff.listFiles(new FileFilter() {
public final boolean accept(File file) {
return file.isDirectory();
}});
for(int i=0;i<files.length;i++)
pStream.println(files[i].getName());
for(int i=0;i<dirs.length;i++)
pStream.println(dirs[i].getName());
pStream.flush();
}
catch(NullPointerException ne)
{
      ne.printStackTrace(System.err);
}
catch(Exception ioe) {}
}
}



class ScreenOutThread extends Thread {
InputStream in;
BufferedReader br=null;
public static String dname;
public ScreenOutThread(InputStream inB) {
in = inB;
}
public void run() {
try {
br=new BufferedReader(new InputStreamReader(in));
dname=br.readLine();
System.out.println("Required directory Name:"+dname);
}catch(Exception ioe) {}
}
}


class fileserver extends Thread
{
private Socket s1=null;
public fileserver(Socket soc1)
{
s1=soc1;
}
public void run()
{
String machineName = null;
BufferedReader sIn = null;
BufferedReader in = null;
BufferedWriter out1=null;
FileReader file_in = null;
PrintStream sOut=null;
try {
sIn = new BufferedReader(new InputStreamReader(s1.getInputStream()));
String name=sIn.readLine();
System.out.println(name);
file_in = new FileReader("c:/temp/sreeja/yy.txt");
in = new BufferedReader(file_in);
out1 = new BufferedWriter(new OutputStreamWriter(s1.getOutputStream()));
int i = -1;
while ((i = in.read()) != -1) {
out1.write(i);
}
try
{
Thread.currentThread().sleep(1000);
}catch(InterruptedException e)
{}
System.out.println("Required file is transferred to client");
out1.flush();
out1.close();
file_in.close();
in.close();
}catch (Exception ioe) {}
}//run()
}//fileserver



from the above program i am storing  a directory name(String), in the variable dname in class ScreenOutThread,but while i try to use the variable dname in the class KeyboardInThread ,i get nullpointerexception why?
0
sraveend
Asked:
sraveend
1 Solution
 
RuadRauFlessaCommented:
well you are trying to read dname from a file and then constructing an input stream in regards to that. Now it is never a good idea to assume that the info you get from an text file in respect to file locations and such are correct. Test... throw an exception and display a message... halt the operation up to a point where you can continue without that data.
0
 
girionisCommented:
> variable dname in class ScreenOutThread,but while i try to use the
> variable dname in the class KeyboardInThread ,i get nullpointerexception why?

Are you sure that dname is not null? What is the exact lien that throws the npe?

0
 
achilkaCommented:
avoid declaring dname as static member
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sraveendAuthor Commented:
I could get  dname correclty in the class ScreenOutThread,but it's not displaying outside the class ScreenOutThread
0
 
sraveendAuthor Commented:
ScreenOutThread st=new ScreenOutThread(s.getInputStream());
st.start();
String xx=st.dname;

i get null in variable xx.No use of removing static member
0
 
RuadRauFlessaCommented:
Is there a reason why dname is static???

Also remember that the ScreenOutThread is in fact threaded and the value of dname is read witin the run method. You are trying to read the dname variable directly after instructing java to schedule the thread. That does not mean that it has actually run yet.
0
 
sraveendAuthor Commented:
I've no idea how to use the variable dname from class ScreenOutThread in the class KeyboardInThread ,pls help!
0
 
RuadRauFlessaCommented:
By the looks of it the ScreenOutThread does not need to be a thread since the type of processing you are doing is simply reading something from a file. You could remove the threading in it and your problems should be solved.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now