No line found Scanner

时间:2016-03-04 18:06:01

标签: java sockets

I'm tring to write a program that communicates with a server in Java. I tried to make a basic communication program but I am stuck on repeated messages. One message works but when I try and get multiple without closing the server and client I fail. On my mission to get it working I got an error which I can not fix.

I have the following classes:

Client

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class MainClass {
    private static int port = 40021;
    private static String ip = "localhost";

    public static void main(String[] args) throws UnknownHostException,
            IOException {
        String command, temp;
        Scanner scanner = new Scanner(System.in);
        Socket s = new Socket(ip, port);
        Scanner scanneri = new Scanner(s.getInputStream());
        System.out.println("Enter any command");
        command = scanner.nextLine();
        PrintStream p = new PrintStream(s.getOutputStream());
        p.println(command);
        temp = scanneri.nextLine();
        System.out.println(temp);
    }

}

Server

import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;


public class MainClass {
    public static void main(String args[]) throws IOException {
        String command, temp;
        ServerSocket s1 = new ServerSocket(40021);
        Socket ss = s1.accept();
        Scanner sc = new Scanner(ss.getInputStream());
        while(true){
        command = sc.nextLine();
        temp = command + " Dat dus";
        PrintStream p = new PrintStream(ss.getOutputStream());
        p.println(temp);
        }

        }


    }

I am getting an error on the While loop.

Exception in thread "main" java.util.NoSuchElementException: No line found

I can understand why it gives an error but I am totally clueless how to get it working.

I hope someone could help me out. Thanks in advance.

1 个答案:

答案 0 :(得分:1)

In your server codes here the line runs forever even after the client has finished sending messages :-

 while(true){
    command = sc.nextLine();
    temp = command + " Dat dus";
    PrintStream p = new PrintStream(ss.getOutputStream());
    p.println(temp);
    } 

change that to :-

while(sc.hasNextLine()){
    command = sc.nextLine();
    temp = command + " Dat dus";
    PrintStream p = new PrintStream(ss.getOutputStream());
    p.println(temp);
    }

and also add your entire server code in a while loop that runs forever as you do not want your server to shutdown after the client disconnects that is :-

public static void main(String args[]) throws IOException 
{
   String command, temp;
   ServerSocket s1 = new ServerSocket(40021);
    while(true)
    {
      Socket ss = s1.accept();
      Scanner sc = new Scanner(ss.getInputStream());
      while(sc.hasNextLine())
      {
        command = sc.nextLine();
        temp = command + " Dat dus";
        PrintStream p = new PrintStream(ss.getOutputStream());
        p.println(temp);
      }
   }
 }