Winforms Windows窗体c和x2B中的套接字代码+;

Winforms Windows窗体c和x2B中的套接字代码+;,winforms,visual-studio-2010,sockets,c++-cli,Winforms,Visual Studio 2010,Sockets,C++ Cli,我必须将我的项目(使用Windows窗体实现简单的UI)与第三方程序集成,该程序在服务器模式下运行,因此我需要一个套接字。以下是相关代码: 入口点: #include "UserInterface1.h" #include "PanguConnection.h" using namespace std; using namespace client; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstanc

我必须将我的项目(使用Windows窗体实现简单的UI)与第三方程序集成,该程序在服务器模式下运行,因此我需要一个套接字。以下是相关代码:

入口点:

 #include "UserInterface1.h"
 #include "PanguConnection.h"


 using namespace std;
 using namespace client;


 int WINAPI WinMain(HINSTANCE hInstance, 
 HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
 {  
    Application::EnableVisualStyles();
    Application::Run(gcnew UserInterface1()); 

    return 0;
 }
套接字代码:

#ifndef PANGUCONNECTION_H_INCLUDED
 #define PANGUCONNECTION_H_INCLUDED

 #include "socket_stuff.h" 
 #include "pan_protocol_lib.h"

 //#define SERVER_NAME  "192.0.0.10"
 const int SERVER_PORT = 10363;

 class PanguConnection    
 {
 private:
    long addr;  
    SOCKET sock;
    unsigned long saddr_len;
    struct sockaddr_in saddr;
    char SERVER_NAME [10];

 public:
    PanguConnection();
    void Connection();
    unsigned long hostid_to_address(char *s);   
    void Terminate ();
    int simple_tests(SOCKET sock);
    int general_tests(SOCKET);
    int elevation_test(SOCKET);
    int lookup_test(SOCKET);
    int scan_test(SOCKET);
    int get_and_save_image(SOCKET sock, char *fname);   
 };

 #endif // PANGUCONNECTION_H_INCLUDED

 #include <cstdlib>
 #include <cmath>

 #include <stdio.h>
 #include <stdlib.h>

 #include "PanguConnection.h"

 #include "socket_stuff.h" 
 #include "pan_protocol_lib.h"

 using namespace std;

 PanguConnection::PanguConnection() 
 {
        SERVER_NAME [0] = 'l';
        SERVER_NAME [1] = 'o';
        SERVER_NAME [2] = 'c';
        SERVER_NAME [3] = 'a';
        SERVER_NAME [4] = 'l';
        SERVER_NAME [5] = 'h';
        SERVER_NAME [6] = 'o';
        SERVER_NAME [7] = 's';
        SERVER_NAME [8] = 't';      

 #ifdef _WIN32
    WSAData wsaData;
    if (WSAStartup(MAKEWORD(1, 1), &wsaData)){}

 #endif

        /* First get the numeric IP address of the server */
        addr = hostid_to_address((char *)SERVER_NAME);

        /* Create a communications TCP/IP socket */
        sock = socket(AF_INET, SOCK_STREAM, 0);     

        /* Connect the socket to the remote server */
        saddr.sin_family = AF_INET;
        saddr.sin_addr.s_addr = addr;
        saddr.sin_port = htons(SERVER_PORT);
        saddr_len = sizeof(struct sockaddr_in); 

        int res = connect(sock, (struct sockaddr *)&saddr, saddr_len);  

        /* Start the PANGU network communications protocol */
        pan_protocol_start(sock);

        int res1 = simple_tests(sock);
        int res2 = general_tests(sock);
        int res3 = elevation_test(sock);
    int res4 =  lookup_test(sock);
    int res5 = scan_test(sock);
    }

    unsigned long PanguConnection::hostid_to_address(char *s)
    {
        struct hostent *host;

        /* Assume we have a dotted IP address ... */
        long result = inet_addr(s);
        if (result != (long)INADDR_NONE) return result;

        /* That failed so assume DNS will resolve it. */
        host = gethostbyname(s);
        return host ? *((long *)host->h_addr_list[0]) : INADDR_NONE;
 }


 int PanguConnection::simple_tests(SOCKET sock)
 {
    int i, status;
    float x, y, z, yaw, pitch, roll;
    char fname[1024];

    /* Initialise the camera position */
    x = 192.257f, y = 192.257f, z = 126.785f;
    yaw = 135.0f, pitch = -25.0f, roll = 0.0f;

    /* Define the field of view we want to use */
    pan_protocol_set_field_of_view(sock, 30.0);

    /* Instruct the viewer to use this position */
    pan_protocol_set_viewpoint_by_angle(sock, x, y, z, yaw, pitch, roll);

    /* Fly towards the model with constant attitude */
    for (i = 0; i < 5; i++)
    {
        /* Set the new position */
        x -= 1.9f, y -= 1.9f, z -= 1.2f;
        pan_protocol_set_viewpoint_by_angle(sock, x, y, z, yaw, pitch, roll);

        /* Get this image */

        status = get_and_save_image(sock, fname);
        if (status) return status;
    }
用户界面部分:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                PanguConnect PC;
                 try{                
                    PC.Connection("localhost",10363);
                    }

                catch(Exception ^e)
                {
                    MessageBox::Show(e->Message);
                }


                try
                {
                    PC.CloseConnection ();
                }

                catch(Exception ^e)
                {
                    MessageBox::Show(e->Message);
                }
             }
我得到的错误是:没有连接,因为目标计算机主动拒绝了它:127.0.0.1:10363。对象引用未设置为对象的实例。
:(

此功能实际上是直接在.NET framework中实现的,不需要直接使用winsock,事实上这样做更危险

using namespace System::Net::Sockets;

public ref class PanguConnection
{
    private:
    System::Net:Sockets::TcpClient mTcpClient;

    public:
    PanguConnection(System::String^ server, Int32 port)
    {
        this->mTcpClient = gcnew System::Net::Sockets::TcpClient(server, port);
    }
}

注意:我注意到您使用TCP进行此操作,因此应能正常工作。

Net framework包含对的访问,为什么不直接使用它?是的,我考虑过。不过,我从第三方程序获得了套接字的代码(但它不是为在Windows窗体中使用而开发的),所以我想我会尝试实现它。我想我会照你说的做。谢谢你的输入。应用程序是作为winforms项目创建的吗?如果是这样的话,它可能被设置为纯CLR。原始的第三方应用程序是用本机C/C++创建的。这是我认为的主要问题。我对原始代码不太熟悉,因为PanguConnect是一个
ref
对象。在它存在之前,您需要执行
gcnew PanguConnect();
操作。还要验证您是否可以通过telnet或类似方式连接到服务器。如果服务器拒绝连接,那么它肯定无法工作。出于某种原因,它否决了您的答案,而不是放弃
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                PanguConnect PC;
                 try{                
                    PC.Connection("localhost",10363);
                    }

                catch(Exception ^e)
                {
                    MessageBox::Show(e->Message);
                }


                try
                {
                    PC.CloseConnection ();
                }

                catch(Exception ^e)
                {
                    MessageBox::Show(e->Message);
                }
             }
using namespace System::Net::Sockets;

public ref class PanguConnection
{
    private:
    System::Net:Sockets::TcpClient mTcpClient;

    public:
    PanguConnection(System::String^ server, Int32 port)
    {
        this->mTcpClient = gcnew System::Net::Sockets::TcpClient(server, port);
    }
}