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

发布于 2024-12-08 15:27:20 字数 2430 浏览 3 评论 0原文

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

下面显示的是两个单独的代码片段不同的文件。 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.");
    }

在客户端 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();
            }

        }

I have developed a screen in swings to download a file from the server. The whole concept works fine when i click the download button once. But when i click the download button second time, i find that the code pauses in getting the inputstream.(this i have followed it using the sysouts shown.)

Below shown are the two separate code snippets in two different files. TCPClient has the serversocket codings whereas the clientUI has the ui components which calls the TCPSever method to accept a socket and for requesting purpose.

In the tcp client side:

  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.");
    }

In the Client UI:

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

Kindly provide me a solution for this...

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

        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

身边 2024-12-15 15:27:20

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

At a guess, your server is single-threaded and is still reading its input stream, because you haven't closed the client socket. But it's anybody's guess until you post the relevant server code.

深者入戏 2024-12-15 15:27:20

下载文件后(成功/不成功),您是否负责关闭套接字?从代码片段来看并不像。

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

Do you take care of closing the socket after the file is downloaded (successfully/unsuccessfully)? It doesn't looks like it from the code snippet.

I'm not sure, but this might be the problem

伏妖词 2024-12-15 15:27:20

这是服务器类:

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

This Is Server Class:

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


    }

}

This Is Client Cass:

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文