C ++ / CLI System :: NullReferenceException-它源自哪里?

时间:2019-05-15 14:40:20

标签: c# c++ c++-cli

我正在创建一个简单的客户端以连接到服务器,向其发送一些数据,并将其回显给客户端。

但是,当我进入端口并尝试连接时,将抛出NullReferenceException。我到处都看过,找不到它的来源。

谢谢!

这是客户端的代码:

#include <string>
#include <string.h>
#include "client.h"

using namespace System::Text;
using namespace client;

int main(array<System::String ^> ^args) {
    IPAddress^                  IP_obj;
    IPEndPoint^                 ipEndPoint;
    Socket^                     client_sock;
    NetworkStream^              netStream;
    BinaryWriter^               bw;
    BinaryReader^               br;
    std::string                 ip_address;
    unsigned int                port;
    System::String^             data;
    System::String^             result;
    array<unsigned char>^       byteArray; 
    int                         bytes;

    // mainly server port & address input stuff... boring
    Console::WriteLine("Input host to connect to (IPv4 format): ");
    client_functions::setIP(IP_obj);
    Console::WriteLine("Input port (must be between {0} amd {1}): ", IPEndPoint::MinPort, IPEndPoint::MaxPort); std::cin >> port;
    if (port < IPEndPoint::MinPort || port > IPEndPoint::MaxPort) {
        Console::WriteLine("Invalid port... exiting.");
        exit(CLIENT_INVALID_PORT);
    }

    // client connection handlers
    client_functions::setEndPoint(ipEndPoint, port, IP_obj);
    client_functions::createSocket(client_sock);
    client_functions::connectSocket(client_sock, ipEndPoint);
    client_functions::setupNetStream(netStream, br, bw, client_sock);

    while (true) {
        // ask user for data to input
        Console::Write("Data: ");
        data = Console::ReadLine();
        if (data == "/E") {
            break;
        }

        // begin sending data
        bw->Write(byteArray, 0, byteArray->Length);
        Console::WriteLine("Sent data: {0}", data);

        // begin reading data
        bytes = br->Read(byteArray, 0, byteArray->Length);
        result = String::Empty; result = Encoding::ASCII->GetString(byteArray, 0, bytes);

        // print received data
        Console::WriteLine("Received data: {0}", result);
    }

    // begin client shutdown process
    client_sock->Shutdown(SocketShutdown::Both);
    Console::WriteLine("Notifying server we are shutting down...");
    client_sock->Close();
    netStream->Close();
    Console::WriteLine("Shutting client down...");
    exit(CLIENT_SUCCESS);
}

...and here is the namespace structure I use for functions and errors:
/*
    [i]API[/i] for client related functions and information
*/

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

namespace client {
    // quite self-explanatory...
    enum client_errors {
        CLIENT_SUCCESS = 0x00, // successful
        CLIENT_INVALID_PORT = 0x01, // thrown if an invalid port is entered
        CLIENT_IOEXCEPTION = 0x02, // thrown if an IO exception occurs (waste of an error)
        CLIENT_SOCKET_ERROR = 0x03, // thrown if unable to connect to host
        CLIENT_INVALID_ADDRESS = 0x04 // thrown if an invalid IP address is entered
    };

    ref class client_functions {
    public: // set the IP to connect to via their input address and IPAddress object
    static void setIP(IPAddress^ &IPAddress_obj) {
        try {
            IPAddress_obj = IPAddress::Parse(Console::ReadLine());
        } catch (FormatException^ e) {
            exit(CLIENT_INVALID_ADDRESS);
        }
    }
        // for user to set IP endpoint to eventually connect to
    static void setEndPoint(IPEndPoint^ &IPEP_obj, unsigned port, IPAddress^ ip_obj) {
        IPEP_obj = gcnew IPEndPoint(ip_obj, port);
    }
        // for user to create a TCP Stream socket, could add more prototypes but meh
    static void createSocket(Socket^ &sock) {
        sock = gcnew Socket(AddressFamily::InterNetwork, SocketType::Stream, ProtocolType::Tcp);
    }
        // for user to connect to host defined in EndPoint object
    static void connectSocket(Socket^ sock, IPEndPoint^ IPEP_obj) {
        try {
            sock->Connect(IPEP_obj);
        } catch (SocketException^ e) {
            Console::WriteLine("Error connecting to host...");
            exit(CLIENT_SOCKET_ERROR);
        }
    }
        // for user to setup the netstream to read and write to the stream
    static void setupNetStream(NetworkStream^ &netstream, BinaryReader^ &bReader, BinaryWriter^ &bWriter, Socket^ sock) {
        netstream = gcnew NetworkStream(sock);
        bReader = gcnew BinaryReader(netstream);
        bWriter = gcnew BinaryWriter(netstream);
    }
        // 
    static void messageToBytes(System::String^ dataToSend, array<unsigned char>^ &data) {
        data = Text::ASCIIEncoding::ASCII->GetBytes(dataToSend);
    }
    };
}```

0 个答案:

没有答案