1

I am currently working on a game in unity and this is the problem i am having. I am trying to create a server side client written in c/c++ to communicate with my game. I found a socket program in c and wrote a network connection in unity.

The client will send the initial information to the server but it can never catch the response from the server properly.

Unity Client Code

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++ server code found from this website 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 compile script.

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

Output from c++ client when running the unity script

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

This is the error I am getting. When the unity WebRequest attempts to GetResponse() from the server.

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)

If it is any help i am also running Linux Mint 18.1 Serena as my host operating system for the server program. Thanks to anyone who helps in answering this post!

Programmer
  • 121,791
  • 22
  • 236
  • 328
wezley
  • 130
  • 7

1 Answers1

2

You are trying to connect two different network protocols together.

HttpWebRequest is used to make REST request. This is similar to HTTP requests.

The socket(AF_INET, SOCK_STREAM, 0); function on your C++ side is used to create pure TCP socket.

These two are totally different from one another and HttpWebRequest/HTTP is built on top of socket.

What to do:

If you decide to use HttpWebRequest on the C# side, you must implement REST on the C++ side. You can find a complete implementation of REST in C++ here.

If you decide to use pure TCP server (your current C++ code), you must also use TCP on the C# side. This is done on the C# side with the Socket or TcpClient class. There are many examples on this post.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Thank you so much! I knew there was some small networking issue I was dealing with. But the fact I was still able to contact the server from the client through me off. I have been stuck on this for quite some time now so I really appreciate your help! – wezley Feb 04 '18 at 06:41