通过套接字Java c#发送大量文件

时间:2017-05-10 13:12:14

标签: java c# sockets

我试图通过套接字C#和Java应用程序发送一组文件,我无法收到所有文件。我认为服务器应用程序会关闭连接,因为服务器在发送文件后总是关闭连接,而不确保客户端应用程序收到文件。

这是C#客户端代码:

public static void connectWithRemoteServer(string strIp, string strMessage, out 

string strResult)
        {
            strResult = "";
            TcpClient tcpClient = new TcpClient();
            tcpClient.Connect(strIp, 1593);
            NetworkStream networkStream = tcpClient.GetStream();
             StreamReader sr = new StreamReader(networkStream);
            if (strMessage.Substring(0,1).Equals("s"))
            {
                byte[] byteBuffer = Encoding.ASCII.GetBytes(strMessage);
                networkStream.Write(byteBuffer, 0, byteBuffer.Length);

                strResult = sr.ReadLine();
            }

            else if (strMessage.Substring(0, 1).Equals("f"))
            {



                    byte[] byteBuffer = Encoding.ASCII.GetBytes(strMessage);
                    networkStream.Write(byteBuffer, 0, byteBuffer.Length);
                    int length = int.Parse(sr.ReadLine());

                    byte[] buffer = new byte[length];
                    networkStream.Read(buffer, 0, (int)length);
                    string var = strMessage.Substring(1);
                    //write to file
                    BinaryWriter bWrite = new BinaryWriter(File.Open(var, FileMode.Create));
                    bWrite.Write(buffer);
                    bWrite.Flush();
                    bWrite.Close();
                    strResult = "ok";
                }
            tcpClient.Close();

            }
        }
     foreach (string strFilePath in lst)
                {
                    Utilities.connectWithRemoteServer("192.168.0.167", "f" + strFilePath, out str);

                }

Java Server app

    while(true)
            {



        String listeFiles = "";
        ServerSocket serverSocket = new ServerSocket(1593);
        System.out.println("Server started and waiting for request..");
        Socket socket = serverSocket.accept();
        System.out.println("Connection accepted from " + socket);
        // this is to send to the client application
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        // this is to receive from the client application
        BufferedInputStream in = new BufferedInputStream(socket.getInputStream());

        byte[] bytearray = new byte[in.available()];// recived byte from the
                                                    // client application
        in.read(bytearray, 0, bytearray.length);// read recieved byte from

        String strRecieved = new String(bytearray);// get the received string
        System.out.println(strRecieved);
        //strRecieved = strRecieved.trim();

            if(strRecieved.substring(0,1).equals("s"))
            {
                List<String> lstFiles   = GetFiles.getListFilesPath("./testData");
                for (String str : lstFiles) {
                    listeFiles = str + "+" + listeFiles;
                }
                String listFilesToSend = listeFiles.substring(0, listeFiles.length() - 1);
                out.println(listFilesToSend);
                System.out.println("List Files Send ");
                serverSocket.close();
                socket.close();
            }
             else if (strRecieved.substring(0,1).equals("f"))
                    {


                File file = new File(strRecieved.substring(1));
//              // send file length
                out.println(file.length());
                // read file to buffer
                byte[] buffer = new byte[(int) file.length()];
                DataInputStream dis = new DataInputStream(new FileInputStream(file));
                dis.read(buffer, 0, buffer.length);
//              // send file
                BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
                bos.write(buffer);
                bos.flush();
                serverSocket.close();
                socket.close();


        }

    }
}

如何确保在发送下一个文件之前已成功接收文件问题

unable to write data to the transport connection an existing connection was forcibly closed

任何帮助将不胜感激。 谢谢。

0 个答案:

没有答案