【发布时间】:2018-07-14 06:39:12
【问题描述】:
我目前正在统一开发一款游戏,这就是我遇到的问题。我正在尝试创建一个用 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();
}
}
从本站找到的c++服务器代码http://www.linuxhowtos.org/C_C++/socket.htm
/* 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 作为服务器程序的主机操作系统。感谢任何帮助回答此帖子的人!
【问题讨论】:
标签: c# c++ .net sockets unity3d