[C++] Code to let people connect to computer

Results 1 to 13 of 13
  1. #1
    Sorcerer Supreme I-Am-No-Fool is offline
    Member +Rank
    Sep 2007 Join Date
    Location
    366Posts

    [C++] Code to let people connect to computer

    well, I am not very good at C++ and I need help making a code that will let people connect to the program and the computer running the program via the port 4007. I have the first bit of code which only uncludes writing and clearing the screen.

    Code:
    #include <conio.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        cout << "Welcome to the Server launcher\n"; 
        cout << "\nHit any key to continue\n";
        getch();
        cout << "\nLoading... Please Wait... ";
        long int i;
        for(i = 0; i < 100000000; i++);
        cout << "Done!\n\n";
        cout << "Hit any key to continue\n";
        getch();
        return 0;
    }
    So after the getch in red I'm hoping to put a connection code.

    Thanks
    Rob


  2. #2
    Grand Master Daevius is offline
    Grand MasterRank
    Jun 2007 Join Date
    NetherlandsLocation
    3,252Posts

    Re: [C++] Code to let people connect to computer

    Take a look at ENet, it's what I have been using for a bit some months ago when I needed connections.

    http://enet.bespin.org

    It's really neat when you got it working xD

  3. #3
    Sorcerer Supreme I-Am-No-Fool is offline
    Member +Rank
    Sep 2007 Join Date
    Location
    366Posts

    Re: [C++] Code to let people connect to computer

    Thanks for that :D

    EDIT: one problem, The client is already coded up in java so I cant use the enet client code which I'm sure is needed

  4. #4
    Grand Master FragFrog is offline
    Grand MasterRank
    Aug 2004 Join Date
    The NetherlandsLocation
    5,629Posts

    Re: [C++] Code to let people connect to computer

    That depends hugely on what you wish to do, and how. I somewhat fail to see why you would want to put a resource hogging for loop in there as well (since it's a server - so you're the only one that sees it - so you want to keep yourself busy?), but that is another matter I deem.

    In my experience Daevius makes a very valid point, though possibly unintentionally: "when you got it working". This is unfortunately a somewhat typical sentiment when C++ is concerned. Do you really want to write this in C++? Creating a socket listener in JAVA is as easy as saying socket = new ServerSocket(port); socket.accept(); - a much more friendly language for the new developer

  5. #5
    Member AlbertFTW is offline
    MemberRank
    Jun 2009 Join Date
    26Posts

    Re: [C++] Code to let people connect to computer

    Seriously, did you even tried Google?
    Just one thing, if your not good at C++, try to learn.

  6. #6
    YEy i has custom title^_^ zarut is offline
    Grand MasterRank
    Sep 2006 Join Date
    FinlandLocation
    537Posts

    Re: [C++] Code to let people connect to computer

    or just use C# its still easier :/ http://www.java2s.com/Code/CSharp/Ne...eTcpServer.htm
    anyway even the client is coded in java it should not matter since you code the server act just like normal server so it should work in each language / socket aslong it takes connections from right port

  7. #7
    Grand Master Daevius is offline
    Grand MasterRank
    Jun 2007 Join Date
    NetherlandsLocation
    3,252Posts

    Re: [C++] Code to let people connect to computer

    Quote Originally Posted by zarut View Post
    How is C# easier if you know C++ and no C#? This code looks almost identical to C++ code, just some syntax changes...please clarify how C# would be "easier" here?

  8. #8
    YEy i has custom title^_^ zarut is offline
    Grand MasterRank
    Sep 2006 Join Date
    FinlandLocation
    537Posts

    Re: [C++] Code to let people connect to computer

    intellisense and the error shit you can see real time helps alot which makes it easier to code

  9. #9
    Member [HDx]Adolf is offline
    MemberRank
    Jan 2009 Join Date
    43Posts

    Re: [C++] Code to let people connect to computer

    Quote Originally Posted by zarut View Post
    intellisense and the error shit you can see real time helps alot which makes it easier to code
    What an incredible argument you have there, I'm amazed o_O

  10. #10
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,097Posts

    Re: [C++] Code to let people connect to computer

    Quote Originally Posted by zarut View Post
    intellisense and the error shit you can see real time helps alot which makes it easier to code
    So easier for people who find trouble debugging code?

    I'll buy that. Though debugging is a key part to the process of learning programming. That's what makes us learn to do things the right way, the first time. Of course everyone makes mistakes, but by training yourself time-after-time to write good code, you start to resent people who live by the C# bells & whistles.

    By learning how to debug and write code more proficiently, you start to solve more and more problems beforehand just knowing their going to come up. That makes the programmer more sharp and proficient while coding.

    Just doing it in C# because it's easier is fine and dandy, the program works, right? Well I personally would rather live in a cross-compatible environment. I write code and learn to debug. I even turn off error messages in PHP and figure out the errors the hard way.. Then when I'm all done I'll see how many warning messages I failed to prevent. It's like a fun little game for me.

    And you know what, I hardly ever code an error anymore.

    We're human, I feel we should take advantage of it.

    And I know Daevius can write debugged code in C++ in the same amount of time you code it in C#. Especially if the rules state to start with a blank desktop. Then notepad++ will open waaayyy faster than visual C#. Lol idk.. just rambling now

  11. #11
    Ginger by design. jMerliN is offline
    Grand MasterRank
    Feb 2007 Join Date
    2,500Posts

    Re: [C++] Code to let people connect to computer

    Quote Originally Posted by zarut View Post
    Code samples / tutorials like that is what makes people incapable of programming in C++.

    That's just like.. really.... really bad.

  12. #12
    iNewLegend , Leo123 zolamu is offline
    Grand MasterRank
    Apr 2006 Join Date
    Холон, IsrLocation
    737Posts

    Re: [C++] Code to let people connect to computer

    its C# code and its should help you

    Code:
    /* 
    ###################################################################################################################
     * ## Developers : [SCF]Leo123 & [SCF]Icarus
     * ## MSN : loniao@walla.co.il 
     * ## email : leo1234u@gmail.com
     * ## FileName : Server.cs
     * ## Info : Create Connections
     * ## Status : 90% Finished
     * ###################################################################################################################
     */
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.Sockets;
    using System.Net;
    using System.Threading;
    using System.Drawing;
    // Local using
    using eDBS.Protocol;
    using eDBS.User;
    using eDBS.Misc;
    using eDBS.Interface;
    
    namespace eDBS.User
    {
        public class Server
        {
    
            #region Varibals
            private string Name;
            private int m_Port;
            private TcpListener m_Server;
            private Thread t_Worker;
            private Thread t_ServerWorker;
            private string m_instanceName;
            private int ClientsAlive = 0;
            private bool CoreStatus = true;
            public cLogTab m_Logger;
            #endregion Varibals
    
            #region Constructor
            public Server(int port)
            {
                m_Port = port;
            }
            #endregion Constructor
    
            #region Workers
    
            // ------------------------------------------------------------------------------------------------------------------
            //* Name ServerWorker , Job : Get Packets & Check if connection still alive
            //* Staus : The function still dont found out if client is connected or not.
            //* ------------------------------------------------------------------------------------------------------------------ */
            public void ServerWorker()
            {
                while (CoreStatus)
                {
                    byte[] Recv;
                    if (ClientsAlive > 0)
                    {
                        for (int i = 0; i != ClientsAlive; i++)
                        {
                            try
                            {
                                TcpClient Client = ServerManager.Clients[i].Connection;
                                Socket ClientSock = ServerManager.Clients[i].m_Socket;
                                if (!Client.Connected || !ClientSock.Connected)
                                {
                                    ClientsAlive--;
                                    ServerManager.ServerDel(i);
                                    continue;
                                }
                                if (Client.Available > 0)
                                {
                                    Recv = new byte[Client.Available];
                                    int ret = ClientSock.Receive(Recv, Recv.Length, SocketFlags.None);
                                    if (ret > 0)
                                    {
                                        Protocol.Protocol.ProtocolCore(i, Recv, Recv.Length);
                                    }
                                }
                            }
                            catch
                            {
                                ClientsAlive--;
                                ServerManager.ServerDel(i);
                            }
                        }
                    }
                    Thread.Sleep(1);
                }
            }
            // ------------------------------------------------------------------------------------------------------------------
            //* Name Worker , Job : Accept Clients & add them to ServerManager
            //* Staus : hmm i think finished
            //* ------------------------------------------------------------------------------------------------------------------ */
            public void Worker()
            {
                while (CoreStatus)
                {
    
                    string IpAdder;
                    if (m_Server.Pending())
                    {
                        TcpClient uClient = m_Server.AcceptTcpClient(); // Wait to client
    
                        //uClient.ReceiveTimeout = 60000;
                        IpAdder = uClient.Client.LocalEndPoint.ToString();
                        int Free = ServerManager.SearchFree();
                        if (Free != -1)
                        {
                            if (ServerManager.ServerAdd(Free, IpAdder, uClient, m_Port))
                            {
                                ClientsAlive++;
                            }
                            else
                            {
                                eFU.logMain.LogOut(Color.Red, "Invaild Index?");
                                uClient.Close();
                            }
                        }
                        else
                        {
                            eFU.logMain.LogOut(Color.Red, "[Server][{0}] MAX SERVER ERROR", IpAdder);
                            uClient.Close();
                        }
    
    
    
                    }
                    Thread.Sleep(1);
                }
            }
            #endregion Workers
    
    
    
            public bool CreateServer()
            {
                m_Server = new TcpListener(IPAddress.Any, m_Port);
                m_Server.Start();
                if (m_Port >= 5500 && m_Port <= 5599)
                {
                    eFU.logMain.LogOut(Color.DarkBlue, "[Server] DataServer{0} Start! PORT [ {1} ]", eFU.g_DataServerCount, m_Port);
                    
                }
                t_Worker = new Thread(Worker);
                t_Worker.Start();
                t_ServerWorker = new Thread(ServerWorker);
                t_ServerWorker.Start();
                m_Logger.LogOut(Color.Green, "[{0}] Server Is Active on port [ {1} ]", m_instanceName, m_Port);
                return true;
            }
            public void CreateLoginstance(string instanceName)
            {
                m_instanceName = instanceName;
                m_Logger = new cLogTab(cLogTab.TabSave, instanceName);
                m_Logger.LogInit();
    
            }
            public void DestoryServer()
            {
                CoreStatus = false;
                t_ServerWorker.Abort();
                t_Worker.Abort();
    
            }
    
        }
    }

  13. #13
    Watching from above Negata is offline
    LegendRank
    Apr 2004 Join Date
    FinlandLocation
    5,455Posts

    Re: [C++] Code to let people connect to computer

    Quote Originally Posted by zolamu View Post
    its C# code and its should help you
    How is it helping 5 months after the thread died?



Advertisement