Home Forums Support HELP! – Having a conversation/Handshake – ClientServer

  • This topic has 2 replies, 2 voices, and was last updated 6 years ago by Anonymous.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #4408
    Anonymous
    Inactive

    Hello all,

    I may be an idiot, because I can’t seem to get my server to RECEIVE and then REPLY to the received packet.

    At the moment my program receives perfectly fine, however I want to setup a “handshake” process but cannot do that if my program is not able to send back :S

    I did some digging and found you can see their IP and port (the received packet) but, if I send something there I just timeout – Lost here.

    Source provided

    If anyone could provide with a simple “conversation” program client/server – I would be mega greatful

    As simple as possible please 🙂

    my code:

    CLIENT:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using NetworkCommsDotNet;
    using NetworkCommsDotNet.Connections;
    using System.Threading;
    
    namespace GenericServerBasedGameTest_Client
    {
        static class Program
        {
            static string serverInfo, serverIP, ourPort;
            static int serverPort;
    
            static void Main()
            {
    
                Console.WriteLine(">Starting Client");
                serverInfo = "192.168.0.10:7700"; //server IP:Port
                Console.WriteLine(">>serverInfo = {0}", serverInfo);
                Console.WriteLine(">>Splitting into seperate vars");
                GetServerIPAndPort(serverInfo);
    
                string MsgToSend = ("");
                SendMsg(serverIP, serverPort, MsgToSend);
    
                ShutdownConnection();
    
                Console.ReadLine();
            }
    
            static void SendMsg(string IP, int PORT, string MSG)
            {
                try
                {
                    Console.WriteLine(">Sending MSG '" + MSG + "'");
                    NetworkComms.SendObject("Message", IP, PORT, MSG);
                    Console.WriteLine(">Sent!");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
    
                }
    
            }
    
            static void ShutdownConnection()
            {
                NetworkComms.Shutdown();
            }
    
            static void GetServerIPAndPort(string serverInformation)
            {
                serverIP = serverInformation.Split(':').First();
                Console.WriteLine(">>>IP: {0}", serverIP);
                serverPort = Convert.ToInt32(serverInformation.Split(':').Last());
                Console.WriteLine(">>>Port: {0}", serverPort);
    
            }

    SERVER:

    class Program
        {
            static void Main(string[] args)
            {
                //Trigger the method PrintIncomingMessage when a packet of type 'Message' is received
                //We expect the incoming object to be a string which we state explicitly by using <string>
                NetworkComms.AppendGlobalIncomingPacketHandler<string>("Message", PrintIncomingMessage);
                //Start listening for incoming connections
                Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Any, 7700));
    
                //Print out the IPs and ports we are now listening on
                Console.WriteLine("Server listening for TCP connection on:");
                foreach (IPEndPoint localEndPoint in Connection.ExistingLocalListenEndPoints(ConnectionType.TCP))
                    Console.WriteLine("{0}:{1}", localEndPoint.Address, localEndPoint.Port);
    
                //Let the user close the server
                Console.WriteLine("Press any key to close server.");
                Console.ReadKey(true);
    
                //We have used NetworkComms so we should ensure that we correctly call shutdown
                NetworkComms.Shutdown();
            }
    
            /// <summary>
            /// Writes the provided message to the console window
            /// </summary>
            /// <param name="header">The packet header associated with the incoming message</param>
            /// <param name="connection">The connection used by the incoming message</param>
            /// <param name="message">The message to be printed to the console</param>
            private static void PrintIncomingMessage(PacketHeader header, Connection connection, string message)
            {
                Console.WriteLine("A message was received from " + connection.ToString() + " which said '" + message + "'.");
    
            //    Console.WriteLine(connection.ConnectionInfo.RemoteEndPoint);
    
                string ClientIP = Convert.ToString(((System.Net.IPEndPoint)connection.ConnectionInfo.RemoteEndPoint).Address);
    
                string ClientPORT = Convert.ToString(((System.Net.IPEndPoint)connection.ConnectionInfo.RemoteEndPoint).Port);
    
                string MsgToSend = "ASDF!";
    
                SendMsg(ClientIP, Convert.ToInt32(ClientPORT), MsgToSend);
            }
    
            static void SendMsg(string IP, int PORT, string MSG)
            {
                try
                {
                    Console.WriteLine(">Sending MSG '" + MSG + "'");
                    NetworkComms.SendObject("Message", IP, PORT, MSG);
                    Console.WriteLine(">Sent!");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
    
                }
    
            }
    
            static void ShutdownConnection()
            {
                NetworkComms.Shutdown();
            }
    
    

    I tried searching, and couldn’t find anything – sorry if this is repeated

    THANKS!
    Magic

    #4409
    Anonymous
    Inactive

    So I got it to work by basically shoehorning the server code into the client… i feel this isn’t the way it is supposed to be done – but it works

    anybody know the proper way?

    #4423
    Anonymous
    Inactive

    maybe use NetworkComms.SendReceiveObject on the client?

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.