English 中文(简体)
为什么在文件服务器程序套接字中写成“ socket s = null”?
原标题:why in file server program socket write as "Socket s =null"?
  • 时间:2012-05-23 12:42:26
  •  标签:
  • java
  • tcp

在此, 我正在创建文件服务器程序 。 在此我注意到 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());
    }
}
}

有谁能解释实际原因吗?

最佳回答

您问题的答案是,如果变量被分别宣布和指定(而不是让编译者/执行环境为您初始化),它被认为是将变量初始化为 null 的良好做法。 这可以追溯到早期的C, 早期C无法保证本地变量的初始化价值, 除非明确设定 。

Java 编译器 < 坚固> 将产生错误,如果它检测到一个变量未经初始化或设置被访问过。 例如 :

    String s;
    try {
        s = "abc";
    } catch (Exception e) {

    }
    System.out.println(s);

将会在最后一行生成编译器错误, 说明本地变量可能尚未初始化( 因为如果试键在 < / em > < code> s 之前丢出一个例外 < em >, 它将永远无法设定 ) 。

在您的情况中,这似乎不是一个问题,因为即使您在试管区外宣布您的 < code> socket ,您也永远不会在试管区外访问 < /em> 。

问题回答

我不知道我是否正确理解你, 但如果你说的是你的代码的第一条线, 那么这只是一个简单的初始化。

良好的编程实践指出, 您不应该让程序语言执行初始化变量。 即使标准指出初始值将是一个特定值, 标准也会改变, 实施也可能在某个时候有错误。 因此, 您应该总是以预期初始值初始化变量 。

在您的代码中,无效值意味着连接不存在。因此,在初始化变量时,鉴于您尚未连接,您将在代表插座的所有对象中存储无效。

我希望这能帮上忙

据我所知,它实际上不是因为一个原因。但是,一般来说,它是一种良好的编码做法,以确保在使用之前,所有变量都是先初始化的。

所以,既然你不想指派一个新的套接字, 直到一个接受 () 被调用为止, 你就会将它指定为无效 。

在这种背景下,这没有意义,但是,如果你把未初始化变量传递到某些地方,这将产生警告。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签