简体   繁体   中英

Send File From Java to C# using Socket

Can anyone give me a small tutorial on how to send file from java server to c# client and on receive complete acknowledgment message from c# to java. Actually I'm new to C# and dont know how to do socket programming. I'm stuck in it since long. Tried many codes. Some codes receive incomplete files some stuck in infinite loop. Please help me in this regard. Thanks

EDIT Here is what I have tried:

C# Server:

{

            IPAddress ipAd = IPAddress.Parse("192.168.1.131");
            // use local m/c IP address, and 

            // use the same in the client


            /* Initializes the Listener */
            TcpListener myList = new TcpListener(ipAd, 5600);

            /* Start Listeneting at the specified port */
            myList.Start();

            Console.WriteLine("The server is running at port 5600...");
            Console.WriteLine("The local End point is  :" +
                              myList.LocalEndpoint);
            Console.WriteLine("Waiting for a connection.....");
        m:
            clientSock = myList.AcceptSocket();

            //clientSock.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReceiveTimeout,10000);


            Console.WriteLine("Connection accepted from " + clientSock.RemoteEndPoint);

            //byte[] b = new byte[100];
            //int k = clientSock.Receive(b);
            string fileName = "hello.wav";


            NetworkStream networkStream = new NetworkStream(clientSock);

            StreamReader sr = new StreamReader(networkStream);

            //read file length
            int length = int.Parse(sr.ReadLine());

            if (networkStream.CanRead)
            {
                BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Create));
                int receivedBytesLen = -1;
                byte[] clientData = new byte[4096 * 5000];

                receivedBytesLen = networkStream.Read(clientData, 0, clientData.Length);
                bWrite.Write(clientData, 0, receivedBytesLen);

                 do
                {
                    receivedBytesLen = networkStream.Read(clientData, 0,clientData .Length);
                    bWrite.Write(clientData, 0, receivedBytesLen);
                } while (receivedBytesLen > 0);

                bWrite.Close();
                networkStream.Close();


            }


            Console.WriteLine("Client:{0} connected & File {1} started received.", clientSock.RemoteEndPoint, fileName);
            Console.WriteLine("File: {0} received & saved at path: {1}", fileName, receivedPath);


            Recognizer_2 recognizeVoice = new Recognizer_2(clientSock);
            recognizeVoice.recognize_wav(); // Acknowledgement 
            Console.WriteLine("\nResult Sent to the Client");
            goto m;
        }

Java Client:

        Socket socket = new Socket("192.168.1.131", 5600);


        BufferedReader response_Stream = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));
        File f = new File(mFileName);
        byte[] buffer = new byte[(int) f.length()];
        FileInputStream fis = new FileInputStream(f);

        BufferedInputStream bis = new BufferedInputStream(fis);


        bis.read(buffer, 0, buffer.length);
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(buffer);

        outputStream.flush();

        String final_Result_String = "";

        if (response_Stream != null) {
            String respose_text = "";
            while ((respose_text = response_Stream.readLine()) != null) {

                final_Result_String += respose_text;

            }

        }

        Toast.makeText(getApplicationContext(), final_Result_String, 1)
                .show();
        outputStream.close();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }

There is no dependance between the languages used by the server or the client.

Just the structure of the data is important !

You should search for some tutorials on socket programming with C#. For example: http://www.codeproject.com/Articles/10649/An-Introduction-to-Socket-Programming-in-NET-using

But the language doesn't matter, understand how the data is formatted when sent on the network.

Edit: you should add a byte or two in the data indicating the length of it. It's not because you dont have data to read once that all the data has been received.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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