在此, 我正在创建文件服务器程序 。 在此我注意到 socket s= null is writeed. I wants 想知道为什么给 null 的真正原因 。 我认为它或者与 objectInputStream 或 Scanner 有关, 要么与 objectInputStream 或 Scanner 有关, 要么与 objectInputStream 或 Scanner 有关 。 这里的代码是这里的代码 。
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());
}
}
}
有谁能解释实际原因吗?