2014-12-13 206 views
0

我是新来的java。我正在使用NetBeans 8.0.2 我开始了一个新项目(Java - > Java Class Libary) 我已经放了两个类并运行我的代码。然而,没有错误,在成功构建项目之后没有输出。聊天服务器客户端输出

我真的很感谢你的帮助。

我的第一堂课是ChatServer &我的第二个是ClientServer!

import java.net.*; 
import java.io.*; 

public class ChatServer 
{ private Socket   socket = null; 
    private ServerSocket server = null; 
    private DataInputStream streamIn = null; 

    public ChatServer(int port) 
    { try 
     { System.out.println("Binding to port " + port + ", please wait ..."); 
     server = new ServerSocket(port); 
     System.out.println("Server started: " + server); 
     System.out.println("Waiting for a client ..."); 
     socket = server.accept(); 
     System.out.println("Client accepted: " + socket); 
     open(); 
     boolean done = false; 
     while (!done) 
     { try 
      { String line = streamIn.readUTF(); 
       System.out.println(line); 
       done = line.equals(".bye"); 
      } 
      catch(IOException ioe) 
      { done = true; 
      } 
     } 
     close(); 
     } 
     catch(IOException ioe) 
     { System.out.println(ioe); 
     } 
    } 
    public void open() throws IOException 
    { streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream())); 
    } 
    public void close() throws IOException 
    { if (socket != null) socket.close(); 
     if (streamIn != null) streamIn.close(); 
    } 
    public static void main(String args[]) 
    { ChatServer server = null; 
     if (args.length != 1) 
     System.out.println("Usage: java ChatServer port"); 
     else 
     server = new ChatServer(Integer.parseInt(args[0])); 
    } 
} 

import java.net.*; 
import java.io.*; 

public class ChatClient 
{ private Socket socket    = null; 
    private DataInputStream console = null; 
    private DataOutputStream streamOut = null; 

    public ChatClient(String serverName, int serverPort) 
    { System.out.println("Establishing connection. Please wait ..."); 
     try 
     { socket = new Socket(serverName, serverPort); 
     System.out.println("Connected: " + socket); 
     start(); 
     } 
     catch(UnknownHostException uhe) 
     { System.out.println("Host unknown: " + uhe.getMessage()); 
     } 
     catch(IOException ioe) 
     { System.out.println("Unexpected exception: " + ioe.getMessage()); 
     } 
     String line = ""; 
     while (!line.equals(".bye")) 
     { try 
     { line = console.readLine(); 
      streamOut.writeUTF(line); 
      streamOut.flush(); 
     } 
     catch(IOException ioe) 
     { System.out.println("Sending error: " + ioe.getMessage()); 
     } 
     } 
    } 
    public void start() throws IOException 
    { console = new DataInputStream(System.in); 
     streamOut = new DataOutputStream(socket.getOutputStream()); 
    } 
    public void stop() 
    { try 
     { if (console != null) console.close(); 
     if (streamOut != null) streamOut.close(); 
     if (socket != null) socket.close(); 
     } 
     catch(IOException ioe) 
     { System.out.println("Error closing ..."); 
     } 
    } 
    public static void main(String args[]) 
    { ChatClient client = null; 
     if (args.length != 2) 
     System.out.println("Usage: java ChatClient host port"); 
     else 
     client = new ChatClient(args[0], Integer.parseInt(args[1])); 
    } 
} 
+0

你用命令行参数运行它吗? – 2014-12-13 11:31:13

+0

也许你不应该创建一个“Java Class Libary”,而应该创建一个“Java Application”。顾名思义,它是一个可供其他应用程序使用的库,而不是应用程序本身。 – BDL 2014-12-13 11:32:33

+0

正如我所说我是新来的java所以什么是命令行! – user4347547 2014-12-13 11:47:10

回答

0

您需要同时运行ChatClient和的ChatServer(因此这两类主要的方法)。 此外,您需要传递命令行参数。让我们来看看在main()ChatClient

public static void main(String args[]) 
    { ChatClient client = null; 
     if (args.length != 2) 
     System.out.println("Usage: java ChatClient host port"); 
     else 
     client = new ChatClient(args[0], Integer.parseInt(args[1])); 
    } 

你解析参数检索服务器名称和端口。这与合适的参数运行这些类的一个例子:

java ChatServer 55444 
java ChatClient 127.0.0.1 55444 

Here是一个简短的教程覆盖在NetBeans IDE设定命令内衬参数的问题。

另外,正如@BDL指出的那样,您最好创建一个New Project而不是java库(或者分别为服务器和客户端分别创建两个项目)。