Jump to content

[SOLVED] Socket Errors C#


photo

Recommended Posts

After following the documentation here for setting up sockets in c#. After being unsuccessful in my own project I opted to create a blank one and just copy and paste the code in the correct location. However I still cannot run the game at that point. I receive about thirty errors for each time I try to access the Socket or Blob class. The documentation didn't mention adding any references so I'm wondering if there is something wrong with my .Net setup? When I look at the code in visual studio I receive an error telling me to either target a version that supports .Net core 2.1 or lower. Or use a version of the SDK that supports 2.2.

image.thumb.png.b5d93cc5c1eda95c16b2452127577b6c.png

image.thumb.png.f8b3fe19c37186dd0f2145133c5f4b1e.png

'Socket' does not contain a definition for 'open' and no accessible extension method 'open' accepting a first argument of type 'Socket' could be found (are you missing a using directive or an assembly reference?)

Link to comment

Greetings Andrew - could you post the code itself so we can see what you have?

Just the basic code would work, other wise it will be harder to diagnose it.

Link to comment

Here is the full AppWorldLogic.cs

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

using Unigine;

namespace UnigineApp {
    static class Constants {
        // UDP port to be used
        public const int UDP_PORT = 8889;
        // UDP receive buffer size
        public const int RECV_SIZE = 7;
        // UDP send buffer size
        public const int SEND_SIZE = 7;
    }

    /// Class representing the Server Socket
    class ServerSocket {
        Socket socket;

        /// Server constructor
        public ServerSocket() {
            // creating a UDP socket
            socket = new Socket(Socket.SOCKET_DGRAM);

            // opening a socket on the specified port with a specified broadcast address
            socket.open("127.255.255.255", Constants.UDP_PORT);

            // setting the size of the sending buffer
            socket.send(Constants.SEND_SIZE);

            // setting the socket as a broadcasting one
            socket.broadcast();

            // setting the socket as a non-blocking one
            socket.nonblock();
        }

        /// Server destructor
        ~ServerSocket() {
            // closing the socket
            socket.close();
        }

        /// method sending a message to a certain client
        public void send_message(short client_num, string message) {
            // preparing a message to be received by a client with a given client_id
            Blob blob = new Blob();
            blob.clear();
            blob.writeShort(client_num);
            blob.write(Marshal.StringToHGlobalUni(message), (uint)message.Length);

            // getting message size
            uint size = blob.getSize();

            // setting current position to start
            blob.seekSet(0);

            // sending the message
            socket.writeStream(blob.getStream(), size);

            blob.clear();
        }
    };

    /// Class representing the Client Socket
    class ClientSocket {
        Socket socket;
        int id = 0;

        /// Client constructor
        public ClientSocket() {
            // creating a UDP socket
            socket = new Socket(Socket.SOCKET_DGRAM);

            // opening a socket on the specified port 
            socket.open(Constants.UDP_PORT);

            // setting the size of the receiving buffer
            socket.recv(Constants.RECV_SIZE);

            // binding the socket to an address figured out from the host used for socket initialization
            socket.bind();

            // setting the socket as a non-blocking one
            socket.nonblock();
        }

        /// Client destructor
        ~ClientSocket() {

            // closing the socket
            socket.close();
        }

        public void setID(int num) {
            // setting client's ID
            id = num;
        }

        /// method checking for received packes from the server
        public int update() {
            // preparing a blob to read the message into
            Blob temp_blob = new Blob();
            temp_blob.clear();

            // reading data from the socket
            socket.readStream(temp_blob.getStream(), Constants.RECV_SIZE);

            if (temp_blob.getSize() > 0) {
                // setting current position to start
                temp_blob.seekSet(0);

                // getting client's ID
                short num_client = temp_blob.readShort();

                // checking if the received message is addressed to this particular client and processing it
                if (num_client == id)
                    Log.Message("\nClient[{0}] - OPERATION_CODE: {1}", id, temp_blob.readLine());
            }
            return 1;
        }
    };

    class AppWorldLogic : WorldLogic {
        //declaring server and client sockets
        ServerSocket server_socket;
        ClientSocket[] clients = new ClientSocket[5];

        // World logic, it takes effect only when the world is loaded.
        // These methods are called right after corresponding world script's (UnigineScript) methods.

        public AppWorldLogic() {
        }

        public override bool Init() {

            server_socket = new ServerSocket();
            // Write here code to be called on world initialization: initialize resources for your world scene during the world start.
            for (int i = 0; i < 5; i++) {
                clients[i] = new ClientSocket();
                clients[i].setID(i + 1);
            }
            return 1;
        }

        // start of the main loop
        public override bool Update() {
            // Write here code to be called before updating each render frame: specify all graphics-related functions you want to be called every frame while your application executes.
            Controls controls = Game.Player.getControls();

            // sending messages on keys pressed to clients 2 and 5
            if (controls.clearState(Controls.STATE_USE) == 1)
                server_socket.send_message(2, "S");
            else if (controls.clearState(Controls.STATE_FORWARD) == 1)
                server_socket.send_message(5, "F");
            else if (controls.clearState(Controls.STATE_BACKWARD) == 1)
                server_socket.send_message(5, "B");
            else if (controls.clearState(Controls.STATE_MOVE_LEFT) == 1)
                server_socket.send_message(5, "L");
            else if (controls.clearState(Controls.STATE_MOVE_RIGHT) == 1)
                server_socket.send_message(5, "R");

            // updating clients
            for (int i = 0; i < 5; i++)
                clients[i].update();

            return 1;
        }

        /* ... */

    }
}

 

Link to comment
  • silent changed the title to [SOLVED] Socket Errors C#
×
×
  • Create New...