2010-01-13 114 views
-2

这是两个类,其中一个在我的客户端应用程序中,另一个在服务器应用程序中。为什么它返回空值?(网络)

我MainServer类:

public class MainServer { 

static Socket client = null; 
static ServerSocket server = null; 
static BufferedReader in; 
static PrintWriter out; 
static String line; 

public static void main() { 
    System.out.println("Server is starting..."); 
    System.out.println("Server is listening..."); 

    try { 
     server = new ServerSocket(5050); 
    } catch (IOException ex) { 
     System.out.println("Could not listen on port 5050"); 
     System.exit(-1); 

    } 
    try { 
     client = server.accept(); 
     System.out.println("Client Connected..."); 
    } catch (IOException e) { 
     System.out.println("Accept failed: 5050"); 
     System.exit(-1); 
    } 

    try { 
     in = new BufferedReader(new InputStreamReader(
       client.getInputStream())); 
     out = new PrintWriter(client.getOutputStream(), 
       true); 
    } catch (IOException e) { 
     System.out.println("Read failed"); 
     System.exit(-1); 
    } 
    while (true) { 
     try { 
      line = in.readLine(); 
//Send data back to client 
       out.println(line); 
      } catch (IOException e) { 
       System.out.println("Read failed"); 
       System.exit(-1); 
     } 
    } 

}} 

我MainClient类:

public class MainClient implements Runnable { 

private static InformationClass info = new InformationClass(); 
private static Socket c; 
private static String text; 

public static String getText() { 
    return text; 
} 

public static void setText(String text) { 
    MainClient.text = text; 
} 
private static PrintWriter os; 
private static BufferedReader is; 
static boolean closed = false; 

/** 
* @param args the command line arguments 
*/ 
public static void runAClient() { 
    try { 
     c = new Socket("localhost", 5050); 

     os = new PrintWriter(c.getOutputStream(), true); 
     is = new BufferedReader(new InputStreamReader(c.getInputStream())); 
     System.out.println(is); 
     String teXt = getText(); 
     os.println(teXt); 

     String line = is.readLine(); 
     System.out.println("Text received: " + line); 




    } catch (UnknownHostException ex) { 
     System.err.println("Don't know about host"); 

    } catch (Exception e) { 
     System.err.println("IOException: " + e); 

    } 

} 

public void run() { 
    String responseLine; 

    try { 
     while ((responseLine = is.readLine()) != null) { 
      System.out.println(responseLine); 
      if (responseLine.indexOf("*** Bye") != -1) { 
       break; 
      } 
     } 
     closed = true; 
    } catch (IOException e) { 
     System.err.println("IOException: " + e); 
    } 

}} 

当我写上客户端的文本区域,然后点击发送按钮: 服务器控制台这些行将被写入==>

init: 
deps-jar: 
compile-single: 
run-single: 
Server is starting... 
Server is listening... 
Client Connected... 
java.net.SocketException: Connection reset 
     at java.net.SocketInputStream.read(SocketInputStream.java:168) 
     at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264) 
     at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306) 
     at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158) 
     at java.io.InputStreamReader.read(InputStreamReader.java:167) 
     at java.io.BufferedReader.fill(BufferedReader.java:136) 
     at java.io.BufferedReader.readLine(BufferedReader.java:299) 
     at java.io.BufferedReader.readLine(BufferedReader.java:362) 
     at ServerNetwork.MainServer.main(MainServer.java:67) 
Java Result: -1 
BUILD SUCCESSFUL (total time: 46 seconds) 

并且在客户端控制台上它们e行将被写入==>

init: 
deps-jar: 
compile-single: 
run-single: 
[email protected] 
Text received: null 
BUILD SUCCESSFUL (total time: 41 seconds) 

我编辑了我的post.please帮助我!我在网络初学者[:-(]

编辑:请从开始这部分阅读: 这是两个阶级,一个用于GUI和其他客户端(网络),它的控制台,服务器上没有返回。所以它不会返回任何客户端。请帮助我谢谢。 我的GUI类:(一部分)(就像一个聊天框,它通过点击发送按钮,我会发送一些东西给服务器)

我的贵班(聊天框)的一部分:

private void SendActionPerformed(java.awt.event.ActionEvent evt) {           
setButtonIsSelected(true); 
submit(); 
clear(); 
} 
private void submit() { 




String text = jTextArea1.getText(); 


jTextArea2.append(client.getCurrentName() + " : " + text + "\n"); 
MainClient.setText(client.getCurrentName() + " : " + text + "\n"); 
} 

private static boolean buttonIsSelected = false ; 

public static boolean isButtonIsSelected() { 
    return buttonIsSelected; 
} 

public static void setButtonIsSelected(boolean buttonIsSelected) { 
    ChatFrame.buttonIsSelected = buttonIsSelected; 
} 

我MainClient类:(其中的一部分)

show a chat frame. 

if (ChatFrame.isButtonIsSelected() == true) { 

     String teXt = getText(); 
     System.out.println(teXt); 
     os.println(teXt); 
     String line; 
     line = is.readLine(); 
     System.out.println("Text received: " + line); 

    } 

起初我将要运行的客户端类所以GUI类将运行哪个名字是聊天框。

+0

如何打印出确切的例外是什么? – bryantsai 2010-01-13 08:17:19

+0

一个可以帮助您调试的工具是打印出异常信息。在你的catch块中做System.err.println(e); – 2010-01-13 08:17:53

+0

如果您在System.out.println(“Read failed。Cause:”+ e.getMessage())中替换'System.out.println(“Read failed”);''''现在我们不完全知道服务器代码意外终止的位置和原因;) – 2010-01-13 08:25:48

回答

0

服务器在任何数据发送前关闭连接,客户端得到null(意思是“文件结束”)。

+0

我认为所有'PrintWriter'都是用(autoFlush = true)构造的。 – bryantsai 2010-01-13 08:26:04

3

getText()返回null,所以你写“null”到服务器。至少,这正是我在修改刚刚编译和运行之后发现的。

改变getText()返回 “你好” 之后我:

服务器:

Server is starting... 
Server is listening... 
Client Connected... 

客户:

[email protected] 
Text received: hello 

(其中 “的BufferedReader @ 7D ......” 行是由于到System.out.println(is);

请注意,在客户端有迪斯科舞厅没有问题,你的服务器将继续尝试向它发送“null”,因为你不测试line在服务器代码中为空。

相关问题