2012-11-08 74 views
1

后,控制了从未到达客户端的代码。但是,我需要在后台运行服务器代码,然后运行我的客户端代码。运行服务器套接字代码在后台,然后再运行客户端代码

这里是我的代码:

//Server code 

try { 
     ServerSocket serverSocket = null; 

    try { 
     serverSocket = new ServerSocket(10007); 
     } 
    catch (IOException e) 
     { 
     System.err.println("Could not listen on port: 10007."); 
     System.exit(1); 
     } 

    Socket clientSocket = null; 
    System.out.println ("Waiting for connection....."); 

    try { 
     clientSocket = serverSocket.accept(); 
     } 
    catch (IOException e) 
     { 
     System.err.println("Accept failed."); 
     System.exit(1); 
     } 

    System.out.println ("Connection successful"); 
    System.out.println ("Waiting for input....."); 

    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), 
             true); 
    BufferedReader in = new BufferedReader( 
      new InputStreamReader(clientSocket.getInputStream())); 

    String inputLine; 

    while ((inputLine = in.readLine()) != null) 
     { 
     System.out.println ("Server: " + inputLine); 
     out.println(inputLine); 

     if (inputLine.equals("Bye.")) 
      break; 
     } 

    out.close(); 
    in.close(); 
    clientSocket.close(); 
    serverSocket.close(); 

    //Client code 
     String serverHostname = new String ("127.0.0.1"); 
     System.out.println ("Attemping to connect to host " + 
       serverHostname + " on port 10007."); 
     Socket echoSocket = null; 
     try { 
      echoSocket = new Socket(serverHostname, 10007); 
      out = new PrintWriter(echoSocket.getOutputStream(), true); 
      in = new BufferedReader(new InputStreamReader(
             echoSocket.getInputStream())); 
     } catch (UnknownHostException e) { 
      System.err.println("Don't know about host: " + serverHostname);    
     } catch (IOException e) { 
      System.err.println("Couldn't get I/O for " 
           + "the connection to: " + serverHostname);    
     } 

     BufferedReader stdIn = new BufferedReader(
            new InputStreamReader(System.in)); 
     String userInput; 

     System.out.print ("input: ");   
      while ((userInput = stdIn.readLine()) != null) { 
       out.println(userInput); 
       System.out.println("echo: " + in.readLine()); 
       System.out.print ("input: "); 
      } 
     out.close(); 
     try { 
      in.close();   
      stdIn.close();   
      echoSocket.close(); 
     } catch (IOException ex) { 
      Exceptions.printStackTrace(ex); 
     } 

我不明白如何在后台运行我的服务器代码,然后运行我的客户端代码。

+1

参见本工作[示例](http://stackoverflow.com/a/3245805/230513)。 – trashgod

+1

退房[并发](http://docs.oracle.com/javase/tutorial/essential/concurrency/)教程 – MadProgrammer

回答

0

你需要做2类:

  • 一台客户机
  • 一台服务器

每个人都会有自己的main()方法。

这样你就可以开始2 JVM的一个服务器,一个客户端

或者在一个类:

创建2个静态内部类实现Runnable,你从你的主类中同时启动:(我取这里假设你的主类被称为入门)

public static main(String args [ ]) { 
       new Thread(new Starter.Server()).start(); 
       new Thread(new Starter.Client()).start(); 
      } 

我让你做的清理代码...

+0

但我需要在这两个一类上点击运行。可能吗? –

+0

是的,但你必须产生一个线程,你有2个选项...旧的方式,那么你看看Runnable接口,或则新的方式,你看并发框架。 – Frank

+0

@Frank Runnable如何运行“旧”和并发框架“新”?可运行的,据我所知,是一个组成部分,如果并发框架 – MadProgrammer

0

使用public static class Client嵌套在主Server类中。他们每个人都可以拥有自己的main方法,所以这是一种在一个文件中实现一切目标的方法,但它还有两个独立的入口点。

另一种选择是有一个单一的入口点,但让它启动两个线程,一个客户端和一个服务器。

0

从技术上讲,你可以通过实例调用这样

// Start the Server 
    new Thread(new Runnable() { 
     public void run() { 
      // .. all the server code 
     } 
    }).start(); 

    // Start the client 

    // .. all the client code 

在幕后服务器代码线程做到这一切在一个源文件,Java的可能是搞什么新的Runnable创建匿名内部类( ){}技术。