3

所以..我有一个服务器套接字,在这里:

ServerSocket server = new ServerSocket(5000);  

while(true){  
    Socket cliente = server.accept();  
    Thread thread = new Thread(new Converter(cliente));  
    thread.start();  

我的应用程序将 Word 文档 ( .doc) 转换为 PDF。首先,我的服务器收到一个 .doc,在使用 .pdf 存档响应后进行转换。

如何获取正在等待响应的客户端?

4

2 回答 2

2

如果我理解正确,您将为每个接受的套接字创建一个线程(因此是客户端)。该Converter线程可以访问客户端套接字(每个客户端都有不同的套接字返回accept())。现在解决方案非常简单:

public void run() {
    cliente.getInputStream();  //read .doc first
    //do the conversion to .pdf
    cliente.getOutputStream();  //send .pdf back
}
于 2012-10-29T19:27:01.513 回答
1

得到:

private void getDocArchive(){  

    try {  

        InputStream in = socket.getInputStream();  
        BufferedInputStream buffer = new BufferedInputStream(in, 1024);  

        byte[] b = new byte[1024];  
        int len = 0;  
        int bytcount = 1024;  

        FileOutputStream out = new FileOutputStream("docs/atual.doc");  

        while ((len = buffer.read(b, 0, 1024)) != -1) {  
            bytcount = bytcount + 1024;  
            out.write(b, 0, len);  
        }  

        out.flush();  

        //out.close();  
        //buffer.close();  

    } catch (IOException e) {  
        System.out.println("Ocorreu um erro no recebimento do arquivo");  
    }  
} 

回复:

private void reponse() {  
    try {  
        OutputStream out = socket.getOutputStream();  
        InputStream in = new FileInputStream(pdf);  

        BufferedInputStream buffer = new BufferedInputStream(in, 1024);  

        byte[] b = new byte[1024];  
        int len = 0;  
        int bytcount = 1024;  
        int i = 0;  

        while ((len = buffer.read(b, 0, 1024)) != -1) {  
            bytcount = bytcount + 1024;  
            out.write(b, 0, len);  
        }  

        out.flush();  
        out.close();  
        buffer.close();  
    } catch (IOException e) {  
        e.printStackTrace();  
        System.out.println(e.getMessage());  
    }  
}  

我的客户:

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

    try {  

        Socket client = new Socket("127.0.0.1", 5000);  

        OutputStream out = client.getOutputStream();  
        InputStream in = new FileInputStream("C:\\autistmo.docx");  

        BufferedInputStream buffer = new BufferedInputStream(in, 1024);  

        byte[] b = new byte[1024];  
        int len = 0;  
        int bytcount = 1024;  

        while ((len = buffer.read(b, 0, 1024)) != -1) {  
            bytcount = bytcount + 1024;  
            out.write(b, 0, len);  
        }  

        // resposta  

        BufferedInputStream buffer2 = new BufferedInputStream(in, 1024);  
        FileOutputStream out2 = new FileOutputStream("docs/final.doc");  

        while ((len = buffer2.read(b, 0, 1024)) != -1) {  
            bytcount = bytcount + 1024;  
            out2.write(b, 0, len);  
        }  

        out2.close();  
        buffer2.close();  

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

运行方法:

@Override  
public void run() {  
    getDocArchive();  
    converter();  
    reponse();  
}  
于 2012-10-29T19:40:00.043 回答