Example #1
0
        private void InitializeServerConnection()
        {
            try
            {
                // Initialize Delegate
                this.displayMessageDelegate = new DisplayMessageDelegate(this.AppendLineToChatBox);
                this.updateRoomsDelegate    = new UpdateRoomsDelegate(this.UpdateRooms);
                this.createRoomDelegate     = new CreateRoomsDelegate(this.AddRoom);
                this.deleteRoomDelegate     = new DeleteRoomsDelegate(this.DeleteRoom);

                // Initialise socket
                this.clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                // Initialize server IP
                // TODO: Change To Server IP
                IPAddress serverIP = IPAddress.Parse(GetLocalIP());

                /*
                 * // TODO: Change To recieve input from upcoming text box
                 * // WORKAROUND: VPN to school network (or be on mst computer)
                 * //             and visit icanhazip.com in a browser and copy IP address here before building
                 * // IPAddress serverIP = IPAddress.Parse("131.151.89.23");
                 */

                // Initialize the IPEndPoint for the server and use port 30000
                IPEndPoint server = new IPEndPoint(serverIP, 30000);

                // Initialize the EndPoint for the server
                epServer = (EndPoint)server;

                // Initialize Login Message
                Message login = new Message();
                login.Who = UserName;
                login.Why = 200;

                string jsonMessage = JsonConvert.SerializeObject(login);

                // Encode Into Byte Array
                var    enc = new ASCIIEncoding();
                byte[] msg = new byte[1500];
                msg = enc.GetBytes(jsonMessage);

                // Send The Message
                clientSocket.BeginSendTo(msg, 0, msg.Length, SocketFlags.None, epServer, new AsyncCallback(this.SendData), null);

                // Initialize data stream
                this.dataStream = new byte[1024];

                // Begin listening for broadcasts
                clientSocket.BeginReceiveFrom(this.dataStream, 0, this.dataStream.Length, SocketFlags.None, ref epServer, new AsyncCallback(this.ReceiveData), null);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
        private void InitializeServerConnection()
        {
            try
            {
                // Initialise the ArrayList of connected clients
                this.clientList = new ArrayList();

                // Initialise the delegate which updates the Rooms
                this.updateRoomsDelegate = new UpdateRoomsDelegate(this.UpdateRooms);

                // Initialise the delegate which updates the Server Status
                this.updateStatusDelegate = new UpdateStatusDelegate(this.UpdateStatus);

                // Initialise the socket
                serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                // Initialise the IPEndPoint for the server and listen on port 30000
                IPEndPoint server = new IPEndPoint(IPAddress.Any, 30000);

                // Associate the socket with this IP address and port
                serverSocket.Bind(server);

                // Initialise the IPEndPoint for the clients
                IPEndPoint clientEP = new IPEndPoint(IPAddress.Any, 0);

                // Initialise the EndPoint for the clients
                EndPoint epSender = (EndPoint)clientEP;

                // Start listening for incoming data
                serverSocket.BeginReceiveFrom(this.dataStream, 0, this.dataStream.Length, SocketFlags.None, ref epSender, new AsyncCallback(ReceiveData), epSender);

                ServerComTextBox.Text = "Listening for Clients..." + "\n";
            }
            catch (Exception ex)
            {
                ServerComTextBox.Text = "Error" + "\n";
                MessageBox.Show("Load Error: " + ex.Message, "Time4aChat Server Command Center Error");
            }
        }