简体   繁体   English

服务器/客户端java.net.SocketException

[英]Server/Client java.net.SocketException

I'm trying to build a client/server application that sends files but I'm having an issue with larger files. 我正在尝试构建一个发送文件的客户端/服务器应用程序,但是我遇到了较大文件的问题。 I'm using a BufferedInputStream to read information from a file and OutputStream to write to the socket. 我正在使用BufferedInputStream从文件读取信息,并使用OutputStream写入套接字。 I have a loop that reads 1 KB from the file and then sends it, which works fine for the first 25 loops then crashes with a socket write error. 我有一个循环,该循环从文件读取1 KB,然后将其发送,这对于前25个循环工作正常,然后因套接字写入错误而崩溃。 Any ideas? 有任何想法吗? Here's the code. 这是代码。

Client 客户

import java.io.*;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TCPClient
{
public static void main(String[] args)
{
    /*Variables*/
    int serverPort = 8899;
    String ip = "localhost";
    File myFile = new File("GeneratedFile.txt"); //fileToBeSent.txt

    System.out.println(myFile.length());

    try
    {
        /*Connect to Server*/
        Socket sock = new Socket(ip, serverPort);
        System.out.println("Connection Made");

        /*Create Streams*/
        FileInputStream fis = new FileInputStream(myFile);
        BufferedInputStream bis = new BufferedInputStream(fis);
        OutputStream clientOutput = sock.getOutputStream();

        /*This is the old code for transfer*/
        /*Create Byte Array
        byte[] myByteArray = new byte[(int) myFile.length()];  //was 1024

        /*Send File
        bis.read(myByteArray, 0, 1024);
        clientOutput.write(myByteArray, 0, 1024);
        clientOutput.flush();
        */

        for(long i = 0; i <= myFile.length(); i += 1024)
        {
            byte[] myByteArray = new byte[1024];
            bis.read(myByteArray, 0, 1024);
            clientOutput.write(myByteArray, 0, 1024);
            clientOutput.flush();
            System.out.println("i is: " + i);
        }
        System.out.println("File Written");
        sock.close();
    } catch (IOException ex)
    {
        Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println("You can't do that!");
    }
    System.out.println("Finished");
}
}

Server 服务器

import java.io.*;
import java.net.*;

public class RequestHandler
{   
public void handleRequest()
{
    try
    {
        ServerSocket welcomeSocket = new ServerSocket(8899);

        while(true)
        {
            Socket socket = welcomeSocket.accept();
            System.out.println("Socket Open");

            /* Create byte array */
            byte[] mybytearray = new byte[1024 * 512];

            /* Create streams */
            InputStream is = socket.getInputStream();
            FileOutputStream fos = new FileOutputStream("newFile.txt",true);
            BufferedOutputStream bos = new BufferedOutputStream(fos);

            /*Write to file*/ 
            int bytesRead = is.read(mybytearray, 0, mybytearray.length);
            bos.write(mybytearray, 0, bytesRead);

            /*Close Stream and Socket*/
            bos.close();
            socket.close();
        }
    } catch (Exception e)
    {
        e.printStackTrace();
    }
}
public static void main(String args[])
{
    RequestHandler rq = new RequestHandler();
    rq.handleRequest();
    System.out.println("Here");
}
}

Your copying technique is incorrect. 您的复制技术不正确。 This is how to copy streams in Java: 这是在Java中复制流的方法:

byte[] buffer = new byte[8192]; // or whatever you like, but declare it outside the loop
int count;
while ((count = in.read(buffer)) > 0)
{
  out.write(buffer, 0, count);
}
out.flush();
// then in a finally block ...
out.close();
in.close();

You need this at both ends. 您两端都需要此。 You cannot assume that any given read will fill the buffer, so you must loop until EOS. 您不能假定任何给定的读取都会填满缓冲区,因此必须循环直到EOS。 Note that you don't flush inside the loop. 请注意,您不会在循环内冲洗。

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

相关问题 客户端和服务器中发现java.net.SocketException - java.net.SocketException noticed in client and server java.net.SocketException:连接重置服务器客户端 - java.net.SocketException: Connection reset server-client Java客户端-服务器应用程序引发“ java.net.SocketException:连接重置”异常 - Java Client-Server application throws “java.net.SocketException: Connection reset” exception 从dotnet客户端连接到Java服务器时收到“ java.net.SocketException:连接重置” - Receiving “java.net.SocketException: Connection reset” when connecting from dotnet client to a java server java.net.SocketException:尝试将对象从客户端传递到服务器时重置连接 - java.net.SocketException: Connection reset when trying to pass an object from a client to a server java.net.SocketException:连接重置-在客户端和服务器之间以webapp的形式部署在tomcat中 - java.net.SocketException: Connection reset - Between a client and server deployed as webapps in tomcat 运行simpleSSL客户端时出现“ java.net.SocketException:连接重置” - “java.net.SocketException: Connection reset” when running a simpleSSL client java.net.SocketException with fabric8 kubernetes 客户端和观察者 - java.net.SocketException with fabric8 kubernetes client and watcher Netty中的java.net.SocketException - java.net.SocketException in Netty 处理java.net.SocketException - Handling java.net.SocketException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM