0

サーバーからファイルをダウンロードするための画面をスイングで開発しました。ダウンロードボタンを1回クリックすると、コンセプト全体が正常に機能します。しかし、ダウンロードボタンを2回クリックすると、入力ストリームの取得中にコードが一時停止することがわかりました.(これは、表示されているsysoutを使用して追跡しました.

以下に示すのは、2 つの異なるファイルに含まれる 2 つの別個のコード スニペットです。TCPClient には serverocket コーディングがありますが、clientUI には ui コンポーネントがあり、TCPSever メソッドを呼び出してソケットを受け入れ、目的を要求します。

TCP クライアント側で:

  public TCPClient() throws Exception{
    System.out.println("Inside TCPClient constructor---");
    clientSocket = new Socket("localhost", 3500);
    System.out.println("After creating socket instance---");
    oos = new ObjectOutputStream(clientSocket.getOutputStream());
    System.out.println("after getting the ouput stream---");
    ois = new ObjectInputStream(clientSocket.getInputStream());
    System.out.println("after getting the input stream.");
    }

クライアント UI で:

private void downloadButton_actionPerformed(ActionEvent e) throws Exception 
{ 
    Object selectedItem = contentsList.getSelectedValue();
        System.out.println("selectedItem---"+selectedItem);
        new TCPClient().downloadContents(nodeName,selectedItem.toString());
    }
} 

これに対する解決策を教えてください...

Below is the server code:

    public void listening() throws Exception{
        ServerSocket ss = new ServerSocket(3500);
            System.out.println( "DataServer Is Listening..." );

            while( true )
            {
                Socket soc = ss.accept();

            ObjectInputStream ois = new ObjectInputStream(soc.getInputStream());                                                            
                ObjectOutputStream oos = new ObjectOutputStream( soc.getOutputStream() );
String input = ( String ) ois.readObject( );

if(input.startsWith("downloadContents")){
            String nodeName = ois.readObject().toString();
            String contentName = ois.readObject().toString();
            List contentsForNode = DBServer.getContentsForNode(nodeName);
            for(Object obj : contentsForNode){
                if(obj.toString().contains(contentName)){
                    new FileServer().send(obj.toString());
                    break;
                }
            }
        }

    }
    }

    public static void main( String[] args ) 
        {
            TCPServer obDataServer  = new TCPServer();

            try
            {
                obDataServer.listening();
            }
            catch ( Exception ioe )
            {
                ioe.printStackTrace();
            }

        }
4

3 に答える 3

3

クライアントソケットを閉じていないため、サーバーはシングルスレッドであり、入力ストリームをまだ読み取っていると推測されます。しかし、関連するサーバー コードを投稿するまでは、誰にもわかりません。

于 2011-10-09T08:54:29.670 に答える
1

ファイルのダウンロード後 (成功/失敗) にソケットを閉じますか? コードスニペットからはそう見えません。

よくわかりませんが、これが問題かもしれません

于 2011-10-09T08:43:31.800 に答える
0

これはサーバークラスです:

package client_to_server;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;`
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Server {
    public static void main(String args[])throws IOException
    {
        ServerSocket serverSocket=new ServerSocket(2222);
        System.out.println("New Server is Waiting");
        Socket socket=serverSocket.accept();
        System.out.println("My Connection Established");
        DateFormat df=new SimpleDateFormat("HH:mm:ss");
        Calendar c=Calendar.getInstance();
        String starttime=df.format(c.getTime());
        System.out.println("Start time is : "+starttime);
        InputStream inputStream=socket.getInputStream();
        byte[] readbyte=new byte[(1024*20)*1024];       
        FileOutputStream fileOutputStream=new FileOutputStream("/home/Manoj/copybulkfile5.zip");
        int writebyte;
        int count=0;
        while((writebyte=inputStream.read(readbyte))!=-1)
        {
            if(writebyte>0)
                count+=writebyte;
            fileOutputStream.write(readbyte, 0, writebyte);
        }
        DateFormat df1=new SimpleDateFormat("HH:mm:ss");
        Calendar c1=Calendar.getInstance();
        String endtime=df1.format(c1.getTime());
        System.out.println("END TIME is "+endtime);
        System.out.println("THE WRITEBYTE VALUE IS "+writebyte+"THE READ BYTE VALUE IS"+count);
        inputStream.close();


    }

}

これはクライアントのキャスです:

package client_to_server;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class Client {
    public static void main(String args[])throws IOException
    {
        //Socket socket=new Socket("localhost",2222);
        Socket socket=new Socket("localhost",2222);
        File file=new File("/home/Checking/Myfile.zip");
        byte[] mybyte=new byte[(1024*20)*1024];
        FileInputStream fileInputStream=new FileInputStream(file);
        int count;
        OutputStream outputStream=socket.getOutputStream();
        while((count=fileInputStream.read(mybyte))!=-1)
        {
            outputStream.write(mybyte);
        }
        System.out.println("THIS FILE HAS BEEN SENT SUCCESSFULLY!!!");

        //System.out.println("END TIME "+hr+"Hours"+min+"Minutes "+sec+"Seconds");
        socket.close();
    }
}
于 2014-04-09T06:37:11.983 に答える