2015-07-12 37 views
-2

我正在开发一个客户端服务器程序和我的程序给我Socket对象上的一些错误不能由编译器检测,我已经重新检查它并没有任何拼错错误或丢失分号,但它仍然告诉我error.Beside,我也与一些在线socket编程教程比较我的计划,我发现我的方式是正确的。我很新到Java Socket编程Java Socket技术方案和符号不能找到DataInputStream类类

try 
    { 
    JOptionPane.showMessageDialog(null,"Server : Configuring the port"+port); 
    ServerSocket server = new ServerSocket(port); 
    JOptionPane.showMessageDialog(null,"Server : Waiting for client's connection");; 
    while(true) 
    { 
     Socket socket = new Socket(); 
     socket = server.accept(); 
    } 
    } 
catch(IOException e) 
    { 
    JOptionPane.showMessageDialog(null,"Server : Nothing Found"); 
    } 
    try 
    { 

     DataInputStream input = new DataInputStream(socket.getInputStream()); // get the input from client 
     DataOutputStream output = new DataOutputStream(socket.getOutputStream()); // Send the output to client 
     String word = "Connection Establish"; 
     while(true) 
     { 
      word = output.readLine(); // read the message 
      output.println(word); // display the message to client 
     } 
    } 
    catch(IOException e) 
    { 
     JOptionPane.showMessageDialog(null,"Server : Can't Connect To Server, Please Try Again"); 
    } 

} 

这是我的一些程序的部分,错误显示在DataInputStream类类,因为它不能找到符号插座
http://codepad.org/ISGe9eTx将提供完整的程序

+1

所以,你会得到一个错误。它是什么?发布完整且准确的错误消息。告诉我们它指的是哪一行。不读取错误信息不会帮助您(和我们)解决问题。 –

+0

错误发生在所有DataInputStream类类给我Socket对象未找到 –

+0

错误发生线35/36和40/41线。 SRY不提供明确的指示 –

回答

1

您需要声明sockettry块之外:

Socket socket = null; 
try{ 
    ... 
    while(true) { 
    socket = server.accept(); 
    } 
} 
... 
+0

问题仍然看起来是一样的...... –

+0

@ShinjiKagawa什么问题会是这样?你没有足够详细地描述任何人知道它是什么,但这个答案肯定会纠正你的代码中的错误。 – EJP

+0

谢谢大家,我解决了所有的错误信息ady –