0

I am trying to send set of files through socket C# and Java application, I am not able to recieve all files. The server application closes connections, I think, because the server always closes connection after sending the file without making sure that the client app received the file.

Here is the C# client code:

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


        }

    }
}

How to make sure the file has been received successfully before send the next file Issue

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

Any help would be appreciated. Thank you .

Spock
  • 315
  • 2
  • 13
Maher HTB
  • 737
  • 3
  • 9
  • 23
  • Here is your problem : `byte[] bytearray = new byte[in.available()];` If your data buffer sent from client is larger than the maximum capacity then you just read data of the length equal to the maximum capacity of that `in` buffer. – mrogal.ski May 10 '17 at 13:25
  • @m.rogalski i did not get well your problem , the exeption raised in the client application (C#) how to selve the issue – Maher HTB May 10 '17 at 13:28
  • The general problem you're having is that you're not using specified protocol and you only relay on the input/output buffer. [Check this](http://stackoverflow.com/questions/41588439/client-server-socket-c-sharp/41588694#41588694) or [this](http://stackoverflow.com/questions/41822581/how-to-read-the-content-of-file-from-port/41848445#41848445) for more informations. – mrogal.ski May 10 '17 at 13:34
  • @m.rogalski i am using TCP protocolwould you please help me help – Maher HTB May 10 '17 at 13:37
  • You have all of the necessary details in answers I've linked you in my previous comment. – mrogal.ski May 10 '17 at 13:38

0 Answers0