开发者

Client Server Program stuck after loop?

Server

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;


public class GetFileServer implements Runnable {

    public static final int SERVERPORT = 4444;
    public String FileName=null; 
    public void run() {
        try {
            ServerSocket svr=new ServerSocket(SERVERPORT);
            while(true){
                System.out.println("S: Waiting...");

                Socket sktClient=svr.accept();
                System.out.println("S: Receiving...");
                try{

                    PrintService services[] =
                        PrintServiceLookup.lookupPrintServices(null, null);

                    PrintWriter out2 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sktClient.getOutputStream())),true);

                    for(int z=0;z<services.length;z++){

                        out2.println(services[z]);
                    }

                    //Get All Required Strings
                    InputStream inStream = sktClient.getInputStream();
                    BufferedReader inm = new BufferedReader(new InputStreamReader(inStream));
                    String fileName = inm.readLine();


                    //Read, and write the file to the socket
                    BufferedInputStream in = new BufferedInputStream(sktClient.getInputStream());           
                    int i=0;                    
                    File f=new File("D:/data/"+fileName);
                    if(!f.exists()){
                        f.createNewFile();
                    }
                    FileOutputStream fos = new FileOutputStream("D:/data/"+fileName);
                    BufferedOutputStream out = new BufferedOutputStream(fos);

                    while ((i = in.read()) != -1) {
                        System.out.println(i);
                        out.write(i);
                        System.out.println("Receiving data...");
                    }

                    out.flush();
                    in.close();
                    out.close();
                    sktClient.close();           
                    System.out.println("Transfer complete.");

                File inp = new File("D:/data/"+fileName);
            //  PrintFile.fileToPrint(inp);
                }
                catch(Exception e){

                    e.printStackTrace();
                }
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }   

    public static void main(String [] args){

        Thread servThread =new Thread(new GetFileServer());
        servThread.start();
    }
}

Client Side

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;


public class Client implements Runnable {
    static final int PORT = 4444; //Change this to the relevant port
    static final String HOST = "192.168.0.53"; //Change this to the relevant HOST,//(where Server.java is running)

    public void run() {

        try {
            System.out.print("Sending data...\n");
            Socket skt = new Socket(HOST, PORT);

            File fil=new File("D:/a.txt");
            String FileName=fil.getName();

            //Send required data Strings to Server
            PrintWriter out2 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(skt.getOutputStream())),true);
            out2.println(FileName);         

            ArrayList Printers =new ArrayList();
            InputStream inStream = skt.getInputStream();
            BufferedReader inm = new BufferedRead开发者_JS百科er(new InputStreamReader(inStream));

            while ((inm.read()) != -1) {
                Printers.add(inm.readLine());

            }
            //Create a file input stream and a buffered input stream.
            FileInputStream fis = new FileInputStream(fil);
            BufferedInputStream in = new BufferedInputStream(fis);
            BufferedOutputStream out = new BufferedOutputStream(skt.getOutputStream());

            //Read, and write the file to the socket            
            int i;
            while ((i = in.read()) != -1) {
                out.write(i);
                System.out.println(i);
            }

            //Close the socket and the file
            out.flush();
            out.close();
            in.close();
            skt.close();

        }
        catch( Exception e ) {          
            System.out.print("Error! It didn't work! " + e + "\n");
        }
    }

    public static void main(String [] args){

        Thread cThread =new Thread(new Client());
        cThread.start();
    }
}

The code stops after getting the Printers List in the While loop At the Client side??Why?when i stop the server progarm the code runs full.


Your client reads from the server until the stream is closed in the first while loop. Since your server never closes the stream the client continues to try and read even though the server isn't saying anything anymore. So the call to read just waits forever.

You either need to close the stream on the server's side, or send down some string from the server to the client that indicates that it's now sent the complete list of printers - in other words, some control string like "END" or something.

Better still make the client and server communication asynchronous so you don't have these kinds of dependencies between them which will lead to these sorts of deadlocking-like problems.


Probably because you are not closing out2 writer (or flushing) on server-side, just after cycle with out2.println(services[z]);.

The writer doesn't send data immediately when you pass it to him, instead it caches data and waits appropriate moment to send it. Then server waits for input from the client and you get a kind of a dead-lock.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜