Unity WebRequest与c ++套接字交谈

时间:2018-02-04 05:36:41

标签: c# c++ .net sockets unity3d

我目前正在开展团结游戏,这就是我遇到的问题。我正在尝试创建一个用c / c ++编写的服务器端客户端来与我的游戏进行通信。我在c中找到了一个socket程序并且统一编写了一个网络连接。

客户端会将初始信息发送到服务器,但它永远无法正确捕获服务器的响应。

Unity客户端代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Net;

public class networking_interface : MonoBehaviour {

    // Use this for initialization
    void Start () {

        // Create a web request to the hosted c++ socket on port 6006
        HttpWebRequest request = new HttpWebRequest(new System.Uri("http://192.168.1.109:6006"));

        // Create a GET method from the server
        request.Method = "GET";

        // Default the user credentials
        request.Credentials = CredentialCache.DefaultCredentials;

        // Get the response back from the server
        // This is the line causing the error
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    }
}

从此网站http://www.linuxhowtos.org/C_C++/socket.htm

找到的c ++服务器代码
/* A simple server in the internet domain using TCP
   The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

void error(const char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
  int sockfd, newsockfd, portno;
  socklen_t clilen;
  char buffer[256];
  struct sockaddr_in serv_addr, cli_addr;
  int n;
  if (argc < 2) {
    fprintf(stderr,"ERROR, no port provided\n");
    exit(1);
  }
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
    error("ERROR opening socket");
  bzero((char *) &serv_addr, sizeof(serv_addr));
  portno = atoi(argv[1]);
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons(portno);
  if (bind(sockfd, (struct sockaddr *) &serv_addr,
    sizeof(serv_addr)) < 0)
    error("ERROR on binding");

  listen(sockfd,5);
  clilen = sizeof(cli_addr);

  newsockfd = accept(sockfd,
    (struct sockaddr *) &cli_addr,
    &clilen);
  if (newsockfd < 0)
    error("ERROR on accept");
  bzero(buffer,256);
  n = read(newsockfd,buffer,255);
  if (n < 0) error("ERROR reading from socket");
    printf("Message from server\n%s\n",buffer);
  n = write(newsockfd,"I got your message",18);
  if (n < 0) error("ERROR writing to socket");
    close(newsockfd);

  close(sockfd);
  return 0;
}

Bash编译脚本。

#!/bin/bash
gcc main.c -o main
./main 6006

运行统一脚本时从c ++客户端输出

Message from server
GET / HTTP/1.1
Content-Length: 0
Connection: keep-alive
Host: 192.168.1.109:6006

这是我得到的错误。当统一WebRequest尝试从服务器获取GetResponse()时。

Exception: at System.Net.WebConnection.HandleError(WebExceptionStatus st, 
System.Exception e, System.String where)
   at System.Net.WebConnection.ReadDone(IAsyncResult result)
   at System.Net.Sockets.Socket+SocketAsyncResult.Complete()
   at System.Net.Sockets.Socket+Worker.Receive()
System.Net.WebConnection.HandleError (WebExceptionStatus st, 
System.Exception e, System.String where)
Rethrow as WebException: Error getting response stream (ReadDone2): 
ReceiveFailure
System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult)
System.Net.HttpWebRequest.GetResponse ()
networking_interface.Start () (at Assets/networking_interface.cs:21)

如果有任何帮助,我也运行Linux Mint 18.1 Serena作为服务器程序的主机操作系统。感谢任何帮助回答这篇文章的人!

1 个答案:

答案 0 :(得分:2)

您正尝试将两种不同的网络协议连接在一起。

HttpWebRequest用于发出REST请求。这类似于HTTP请求。

C ++端的socket(AF_INET, SOCK_STREAM, 0);函数用于创建纯TCP套接字。

这两个完全不同,HttpWebRequest / HTTP建立在socket之上。

怎么做

如果您决定在C#端使用HttpWebRequest,则必须在C ++端实现REST。您可以在C ++ here中找到完整的REST实现。

如果决定使用纯TCP服务器(当前的C ++代码),则还必须在C#端使用TCP。这是在C#端使用Socket或TcpClient类完成的。 this帖子上有很多例子。