繁体   English   中英

无法在服务器套接字文件传输中获取输入流

[英]not able to get inputstream in server socket file transfer

我已经开发了一个屏幕,可以从服务器下载文件。 当我单击下载按钮一次时,整个概念工作正常。 但是当我第二次单击下载按钮时,我发现代码在获取输入流时暂停。(我使用显示的sysouts遵循了它。)

下面显示的是两个不同文件中的两个单独的代码片段。 TCPClient具有服务器套接字编码,而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.");
    }

在客户端界面中:

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();
            }

        }

猜测您的服务器是单线程的,并且仍在读取其输入流,因为您尚未关闭客户端套接字。 但这是所有人的猜测,直到您发布相关的服务器代码为止。

下载文件后(成功/失败),您是否需要关闭套接字? 从代码片段来看,它看起来并不像它。

我不确定,但这可能是问题所在

这是服务器类:

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();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM