Java套接字 - 有时只能工作,不会打印从服务器给出的所有内容

时间:2015-10-21 19:48:18

标签: java sockets tcp

我在使用套接字的java客户端/服务器程序时遇到问题。当我从循环菜单中选择一个选项时,它只能工作(很少超过一次),如果服务器的输出有很多行,客户端要么没有全部接收,要么全部不打印

我对此很新,所以任何事情都会有所帮助!

客户端:

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class client {

static String hostname = null;
static Scanner console = new Scanner(System.in);
static int myPort = 2123;
static String userChoice = null;
static String command = null;

public static void main(String[] args) throws IOException {

    System.out.print("Please enter hostname: ");
    hostname = console.nextLine();

    if (args.length > 0)
        hostname = args[0];
     System.out.println ("Attemping to connect to host " +
    hostname + " on port " + myPort + ".");

     Socket socket = null;
     PrintWriter out = null;
     BufferedReader in = null;

     try {
         socket = new Socket(hostname, myPort);
         out = new PrintWriter(socket.getOutputStream(), true);
         in = new BufferedReader(new InputStreamReader(
                                     socket.getInputStream()));
     } catch (UnknownHostException e) {
         System.err.println("Don't know about host: " + hostname);
         System.exit(1);
     } catch (IOException e) {
         System.err.println("Can't connect to: " + hostname);
         System.exit(1);
     }

    while(true)
    {
        displayMenu();
        saveChoice();

        if(command != null){
        out.println(command);
        while((in.readLine()) != null)
        {
        System.out.println(in.readLine());
        }
        }
    }
}

public static void displayMenu()
{
    System.out.println(" ");
    System.out.println("1. Host Current Date and Time");
    System.out.println("2. Host uptime");
    System.out.println("3. Host memory use");
    System.out.println("4. Host Netstat");
    System.out.println("5. Host current users");
    System.out.println("6. Host running processes");
    System.out.println("7. Quit ");

    System.out.println(" ");
    System.out.print("Your choice: ");
    userChoice = console.nextLine();
}

public static void saveChoice()
{
    switch (userChoice)
    {
    case "1":
        command = "date";
        break;
    case "2":
        command = "systeminfo | find \"System Boot Time\"";
        break;
    case "3":
        command = "systeminfo | find \"Virtual Memory: In Use\"";
        break;
    case "4":
        command = "netstat";
        break;
    case "5":
        command = "netuser";
        break;
    case "6":
        command = "tasklist";
        break;
    case "7":
        System.out.println("Goodbye");
        System.exit(0);
        break;
    default:
        System.out.print("That is not a choice. Please enter an option between 1-7.");
    }
}
}

服务器:

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

public class server 
{ 
static int count = 0;

public static void main(String[] args) throws IOException 
{ 
ServerSocket serverSocket = null; 

try { 
     serverSocket = new ServerSocket(2123); 
    } 
catch (IOException e) 
    { 
     System.err.println("Could not listen on port: 2123."); 
     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) 
    { 

    Runtime rt = Runtime.getRuntime();
     Process proc = rt.exec("cmd /c " + inputLine);

     BufferedReader stdInput = new BufferedReader(new 
          InputStreamReader(proc.getInputStream()));

     // read the output from the command
     String s = null;
     while ((s = stdInput.readLine()) != null) 
     {
         out.println(s);
         count++;
     }
     System.out.print("Lines sent to client: " + count);
    }
} 
}

0 个答案:

没有答案