UDP - Java 服务器将字符串发送到 C++/CLI .net 客户端

UDP - Java Server sends String to C++/CLI .net Client

我正在尝试将字符串流从 Java 服务器发送到 C++/CLI 客户端,但在此之前我想从最简单的情况开始,即从Java 服务器到 C++/CLI 客户端并显示它。 我在文献或教程中找到的示例对我不起作用,因为我知道同一个 Java 服务器可以轻松地与另一个 Java 客户端(在同一台机器或不同机器上)通信。 事不宜迟,这是我的代码:

the Java Server Side: SendStringToCpp.java

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


public class SendStringToCpp {


    public static void main(String[] args) {

        String message = "message"; // The String that contains the information
        byte[] sentBytes = message.getBytes();

        System.out.println("Message: " + message);

        ServerSocket s = null;
        try {
            s = new ServerSocket(30011);
        } catch (IOException e) { 
            e.printStackTrace();
        }


        Socket s1 = null;
        try {
            s1 = s.accept();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  


        OutputStream s1out = null;
        try {
            s1out = s1.getOutputStream();
        } catch (IOException e) {

            e.printStackTrace();
        }


        DataOutputStream dos = new DataOutputStream (s1out);

        try {
            //dos.writeUTF(message); // Sending the String  
              dos.write(sentBytes);  // Sending the bytes  


        } catch (IOException e) {

            e.printStackTrace();
        }

        try {
            dos.close();
        } catch (IOException e) {

            e.printStackTrace();
        }
        try {
            s1out.close();
        } catch (IOException e) {

            e.printStackTrace();
        }
        try {
            s1.close();
        } catch (IOException e) {

            e.printStackTrace();
        }
        try {
                s.close();
            } catch (IOException e) {

                e.printStackTrace();
            } 
    } 
}

The C++ Client Side: ReceiveStringFromJava.cpp

#include "stdafx.h"
#include <exception>

using namespace System;
using namespace System::Net;
using namespace System::Net::Sockets;
using namespace System::Text;
using namespace System::IO;

int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Creating the Socket..."); 
    try {
        //Socket^ listener = gcnew Socket(AddressFamily::InterNetwork, SocketType::Dgram, ProtocolType::Udp);
        //Creates a UdpClient for reading incoming data.
        UdpClient^ receivingUdpClient = gcnew UdpClient();
        IPEndPoint^ RemoteIpEndPoint = gcnew IPEndPoint(IPAddress::Any, 30011);
        //listener->Bind(RemoteIpEndPoint);
        array <Byte>^ receiveBytes = receivingUdpClient->Receive(RemoteIpEndPoint); // get the Bytes array from the end poitn
        String^ receivedString = Encoding::ASCII->GetString(receiveBytes); // retrieve the string from the received Bytes
        Console::WriteLine("This is the message received {0}", receivedString);
        //  Console::WriteLine("this message was send from {0} on their ort number {1}", RemoteIpEndPoint->Address, RemoteIpEndPoint->Port);     

    }
    catch (Exception^ e) {
        Console::WriteLine("Error!  ");
        Console::WriteLine( e->ToString());
    } 
    Console::ReadLine(); 
    return 0;
}

And Here's the Exception printed on the Console:

http://i.stack.imgur.com/uTDqm.jpg

P.S。我试图将 IPEndPoint 绑定到套接字(上面有评论),但无济于事,并给出了同样的错误。

Socket^ listener = gcnew Socket(AddressFamily::InterNetwork, SocketType::Dgram, ProtocolType::Udp);
.
.
 listener->Bind(RemoteIpEndPoint);

您需要使用允许绑定到侦听端口的UdpClient constructor

引用 MSDN(强调我的):

Initializes a new instance of the UdpClient class and binds it to the local port number provided.

UdpClient^ receivingUdpClient = gcnew UdpClient(30011);
//                  specify the port both here  ^^^^^  and here vvvvv
IPEndPoint^ RemoteIpEndPoint = gcnew IPEndPoint(IPAddress::Any, 30011);
array <Byte>^ receiveBytes = receivingUdpClient->Receive(RemoteIpEndPoint);