Wednesday, May 23, 2012

why in file server program socket write as "Socket s =null"?

here , i am creating file server program .in this i have noticed that socket s= null is written.i want to know actual reason why null is given.I thought that it is either related to the ObjectInputStream or Scanner.is it true it related to the ObjectInputStream or Scanner .Here the code for



Server.java 

public class Server{
public static void main(String[] args){
Socket s=null;
ServerSocket ss=null;
ObjectInputStream ois=null;
ObjectOutputStream oos=null;
Scanner sc=new Scanner(System.in);


try
{
ss = new ServerSocket(1234);
System.out.println("server is created");

}
catch(Exception e)
{
System.out.println(e.getMessage());
}



try {
s=ss.accept();
System.out.println("connected");
oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject("Welcome");
ois= new ObjectInputStream(s.getInputStream());
}catch(Exception e)
{
e.printStackTrace();
}
try{
String fil=(String)ois.readObject();
FileInputStream fis = new FileInputStream(fil);
int d;
String data="";
while(true)
{
d=fis.read();
if(d==-1)
break;
data = data+(char)d;
}
oos.writeObject(data);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}


can anyone explain actual reason? Thanks in advance .





No comments:

Post a Comment