0

So, the end product is creating a multi client to single server type of program, where the clients send text files in xml form, the server then takes the file and parses it to attain information. I'm at square one, trying to get only one client to send a file to the server. I made it so (at the moment) the client creates a text file (that I as a user will input in console) and sends that text file to the server. The server checks if the file is indeed a text file, and then responds to the client with 'File received!', however, there is something wrong within the client class that I am just not understanding, can anyone help me out?

Here is the server code:

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

public class Server {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        String file,temp;
        ServerSocket sock1=new ServerSocket(1234);
        Socket ss=sock1.accept();
        Scanner scan=new Scanner(ss.getInputStream());
        PrintStream ps= new PrintStream(ss.getOutputStream());
        file=scan.next();
        if(file.substring(file.length() - 4)==".txt") {
            temp="File retrieved";
            ps.println(temp);
        }
        //temp= number + number;


    }}

Here is the client code:

package ServerClientTower;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {

    public static void main(String[] args) throws UnknownHostException, IOException {
        // TODO Auto-generated method stub

        File f= new File("invoice.txt"); //creating .txt file name invoice
        String option,file,temp;
        Scanner scan= new Scanner(System.in);
        Socket sock= new Socket("127.0.0.1",1234);
        Scanner scan2=new Scanner(sock.getInputStream());
        PrintStream print= new PrintStream(sock.getOutputStream());
        System.out.println("Please enter your command using this options \n1.Send (nameofFile).txt\n\n2.Delete\n\n");
        option= scan.nextLine(); //reads input line from user
        String input[]=option.split("\\s");// splits the input, taking in the first user string, and then the second user string
        System.out.println(input[0] +' '+ input[1]); //testing to see if the split actually worked
        if(input[0].contains("Send")) {

             print.println(input[1]);            
        }

       // print.println(number);    
        temp= scan2.next();   //this comes up with an error, though I don't understand why
        System.out.println(temp);

    }

}

The error that comes up is :

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at ServerClientTower.Client.main(Client.java:29)

So I don't know if I need to remove the temp=scan2.next() or use a different method all together.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Awwab
  • 1
  • 1

0 Answers0