Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Error while using the networking module in VS express 2015  (Read 3876 times)

0 Members and 1 Guest are viewing this topic.

mitchell02

  • Newbie
  • *
  • Posts: 2
    • View Profile
Error while using the networking module in VS express 2015
« on: February 18, 2016, 11:04:22 am »
COMPLETE NEWBIE!!!

I've been trying to verify my SFML has been installed correctly by running the following code to check SFML is running correctly, and to get an idea of how to use sockets, but I keep getting this error:

unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)

I am using Visual Studio Express 2015 on Windows 10. What am I missing?

#include "stdafx.h"
#include <SFML/Network.hpp>
#include <iostream>


////////////////////////////////////////////////////////////
/// Launch a server, wait for an incoming connection,
/// send a message and wait for the answer.
///
////////////////////////////////////////////////////////////
void runTcpServer(unsigned short port)
{
        // Create a server socket to accept new connections
        sf::TcpListener listener;

        // Listen to the given port for incoming connections
        if (listener.listen(port) != sf::Socket::Done)
                return;
        std::cout << "Server is listening to port " << port << ", waiting for connections... " << std::endl;

        // Wait for a connection
        sf::TcpSocket socket;
        if (listener.accept(socket) != sf::Socket::Done)
                return;
        std::cout << "Client connected: " << socket.getRemoteAddress() << std::endl;

        // Send a message to the connected client
        const char out[] = "Hi, I'm the server";
        if (socket.send(out, sizeof(out)) != sf::Socket::Done)
                return;
        std::cout << "Message sent to the client: \"" << out << "\"" << std::endl;

        // Receive a message back from the client
        char in[128];
        std::size_t received;
        if (socket.receive(in, sizeof(in), received) != sf::Socket::Done)
                return;
        std::cout << "Answer received from the client: \"" << in << "\"" << std::endl;
}


////////////////////////////////////////////////////////////
/// Create a client, connect it to a server, display the
/// welcome message and send an answer.
///
////////////////////////////////////////////////////////////
void runTcpClient(unsigned short port)
{
        // Ask for the server address
        sf::IpAddress server;
        do
        {
                std::cout << "Type the address or name of the server to connect to: ";
                std::cin >> server;
        } while (server == sf::IpAddress::None);

        // Create a socket for communicating with the server
        sf::TcpSocket socket;

        // Connect to the server
        if (socket.connect(server, port) != sf::Socket::Done)
                return;
        std::cout << "Connected to server " << server << std::endl;

        // Receive a message from the server
        char in[128];
        std::size_t received;
        if (socket.receive(in, sizeof(in), received) != sf::Socket::Done)
                return;
        std::cout << "Message received from the server: \"" << in << "\"" << std::endl;

        // Send an answer to the server
        const char out[] = "Hi, I'm a client";
        if (socket.send(out, sizeof(out)) != sf::Socket::Done)
                return;
        std::cout << "Message sent to the server: \"" << out << "\"" << std::endl;
}

Note: This code was copied from https://github.com/SFML/SFML/blob/master/examples/sockets/TCP.cpp. I just added
#include "stdafx.h"
to make it work in Visual Studio.


ramaskrik

  • Newbie
  • *
  • Posts: 45
    • View Profile
Re: Error while using the networking module in VS express 2015
« Reply #1 on: February 18, 2016, 02:07:29 pm »
You miss the entry point (int main()) of your application. You need to define it, as that is what gets called when you run your executable. In that function you call all the other functions you want to get executed.

Please learn C++ (for instance you could start here), make some console programs, etc. before trying to use an external library. Otherwise you'll just get confused.

mitchell02

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Error while using the networking module in VS express 2015
« Reply #2 on: February 20, 2016, 02:21:23 am »
Sorry. I just copy pasted from Github. Because it was an official SFML one I didn't think to check the code, because I downloaded it to try and get an understanding of how networking works.

PS. I understand I look like a noob who doesn't know how to code from the mistake, but please don't judge me. I have completed the Sololearn C++ tutorial. (Whether or not that qualifies me as being able to code I don't know, but I'm not a complete beginner.)

ramaskrik

  • Newbie
  • *
  • Posts: 45
    • View Profile
Re: Error while using the networking module in VS express 2015
« Reply #3 on: February 20, 2016, 09:18:47 am »
Not every example on official SFML page is instantly runnable. More specifically, those which lack int main() certainly aren't.

Tutorials are a good source of knowledge for beginners, but in order to get past the initial "noob stage", you need to write a code that fails, fix it, see it execute gracefully. Repeat so many times you stop tracking the number of it. Then tadahh - you can write a simple program from top of your head.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Error while using the networking module in VS express 2015
« Reply #4 on: February 20, 2016, 10:36:55 am »
The example is complete... as long as you compile the 3 .cpp files that make it, and not just pick a single file and hope that it will work magically by itself ;)
Laurent Gomila - SFML developer

ramaskrik

  • Newbie
  • *
  • Posts: 45
    • View Profile
Re: Error while using the networking module in VS express 2015
« Reply #5 on: February 20, 2016, 11:41:40 am »
The example is complete..

Mea Culpa, I didn't notice it was a Github project. I thought he was talking about one of the examples on the official SFML tutorial page (smth like this: http://www.sfml-dev.org/tutorials/2.3/network-socket.php)

That's what you get for just skimming the posts :D