0

我已经开发了一个摆动屏幕来从服务器下载文件。当我单击一次下载按钮时,整个概念工作正常。但是当我第二次单击下载按钮时,我发现代码在获取输入流时暂停(我使用所示的系统输出跟踪它。)

下面显示的是两个不同文件中的两个单独的代码片段。TCPClient 具有 serversocket 编码,而 clientUI 具有调用 TCPSever 方法以接受套接字并用于请求目的的 ui 组件。

在 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 回答