【问题标题】:Send File From Java to C# using Socket使用 Socket 将文件从 Java 发送到 C#
【发布时间】:2012-07-23 16:13:26
【问题描述】:

谁能给我一个关于如何从 java 服务器发送文件到 c# 客户端以及从 c# 到 java 接收完整确认消息的小教程。实际上我是 C# 新手,不知道如何进行套接字编程。我被困在里面很久了。试了很多代码。一些代码收到不完整的文件,一些文件陷入无限循环。请在这方面帮助我。 谢谢

编辑 这是我尝试过的:

C# 服务器:

{

            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 客户端:

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

    }

【问题讨论】:

  • socket 几乎没有任何意义:你想使用哪种协议?它是否已定义,是自定义的吗?
  • What have you tried? 即请给我们看一些代码,见sscce.org
  • 我今天必须提交作业。如果可以的话请帮帮我..:(
  • 请立即检查已编辑的问题。更新了一些代码
  • 一个不相关的建议,为什么不使用循环而不是在服务器代码中使用goto

标签: c# java android sockets file-transfer


【解决方案1】:

服务器或客户端使用的语言之间没有依赖关系。

只是数据的结构很重要!

您应该搜索一些关于使用 C# 进行套接字编程的教程。 例如:http://www.codeproject.com/Articles/10649/An-Introduction-to-Socket-Programming-in-NET-using

但语言无关紧要,了解数据在网络上发送时的格式。

编辑:您应该在数据中添加一两个字节来指示它的长度。不是因为你没有数据可以读取一次,所有的数据都已经收到了。

【讨论】:

  • 谢谢。但我被困在代码的某个地方。无法接收完整的文件。
  • 也许您没有使用好的测试来知道何时停止阅读。缓冲区中的可用字节数不够,将长度添加到您发送的数据中。
  • 可能是......你有什么像样的解决方案???循环条件应该写什么???
  • 服务器端:使用前两个字节作为数据包的长度(没有这两个字节),然后写入文件。 // 客户端:读取前两个字节,然后继续读取(或等待字节),只要你没有得到好的字节数
  • 一个好的解决方案是启动一个计时器,这样如果连接丢失(例如在传输过程中),您就不会永远等待。如果没有可用的字符并且计时器已过,则停止读取并返回错误或任何您想要指示超时的内容。
猜你喜欢
  • 1970-01-01
  • 2012-04-25
  • 1970-01-01
  • 2020-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-26
  • 2015-03-06
相关资源
最近更新 更多