我写了一个非常简单的客户端程序
import java.net.*;
import java.io.*;
public class GreetingClient
{
private Socket cSocket;
public GreetingClient() throws IOException
{
String serverName = "127.0.0.1";
int port = 5063;
cSocket = new Socket(serverName,port);
}
public static void main(String [] args)
{
GreetingClient client;
try
{
client = new GreetingClient();
}
catch(IOException e)
{
e.printStackTrace();
}
while (true)
{
try
{
InputStream inFromServer = client.cSocket.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF());
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
当我编译这个程序时,我得到了错误
GreetingClient.java:33: error: variable client might not have been initialized
InputStream inFromServer = client.cSocket.getInputStream();
^
1 error
这个错误是什么意思?如果新的 Socket() 调用失败(例如,如果打开了太多套接字),那么这是否意味着我不能在代码中使用它?我该如何处理这样的情况。?