コード例 #1
0
        /// <summary>
        /// Receives state and server's transfered bytes and appends UTF8 conversion to warden's message variable
        /// </summary>
        /// <param name="ARSTATE"></param>
        public static void receiveMessage(IAsyncResult ARSTATE)
        {
            try
            {
                NetworkWarden warden   = (NetworkWarden)ARSTATE.AsyncState;
                int           numBytes = warden.socket.EndReceive(ARSTATE);

                if (numBytes > 0)
                {
                    string newMessage = Encoding.UTF8.GetString(warden.buffer, 0, numBytes);
                    newMessage = newMessage.Trim();
                    newMessage = newMessage.TrimEnd();
                    //string[] lines = ss.sb.ToString().Split(new string[] {"\n"}, StringSPlitOptions.None);
                    //warden.message.Clear();
                    //warden.message.Append(newMessage);
                    Console.WriteLine("Server said: " + newMessage);
                }

                warden.callNext(warden);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Receive callback failed. Error occured: " + e);
                return;
            }
        }
コード例 #2
0
        /// <summary>
        /// This function "assists" the Send function. If all the data has been sent, end sending procedure.
        /// </summary>
        static void StopSend(IAsyncResult ARSTATE)
        {
            NetworkWarden warden = (NetworkWarden)ARSTATE.AsyncState;

            warden.socket.EndSend(ARSTATE);
            //Console.WriteLine("warden callnext: " + (warden.callNext.Method.Name));
            warden.callNext(warden);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: nerd3d/SS3500
 static void sendMessage(NetworkWarden ward)
 {
     /*while(warden == null)
      * {
      * }*/
     ward.callNext = sendMessage;
     Networking.Send(ward, Console.ReadLine() + "\n");
 }
コード例 #4
0
ファイル: Program.cs プロジェクト: nerd3d/SS3500
 static void getNameBack(NetworkWarden ward)
 {
     warden = ward;
     Console.WriteLine("server sent spreadsheet: " + warden.message);
     ward.callNext = getNameBack;
     Networking.Send(ward, "trying to send chats" + "\n");
     //Networking.Send(ward.socket, "#");
     listenWarden = new NetworkWarden(ward.socket, ward.ID);
     getData(listenWarden);
     sendMessage(warden);
     //Thread ctThread = new Thread(getData(warden));
     //ctThread.Start();
 }
コード例 #5
0
        /*
         * ConnectToServer
         * give callback function for first parameter.
         * give ip of server as 'hostname' parameter.
         */
        public static Socket ConnectToServer(NetworkWarden.callBack callbackFunction, string hostname)
        {
            System.Diagnostics.Debug.WriteLine("connecting to " + hostname);
            //Connect to server
            try
            {
                //Establish remote endpoint for socket
                IPHostEntry ipHostInfo;
                IPAddress   ipAddress = IPAddress.None;
                //determine if the server address is a URL or IP
                try
                {
                    ipHostInfo = Dns.GetHostEntry(hostname);
                    bool foundIPV4 = false;
                    foreach (IPAddress addr in ipHostInfo.AddressList)
                    {
                        if (addr.AddressFamily != AddressFamily.InterNetworkV6)
                        {
                            foundIPV4 = true;
                            ipAddress = addr;
                            break;
                        }
                    }
                    if (!foundIPV4)
                    {
                        System.Diagnostics.Debug.WriteLine("Invalid address: " + hostname);
                        return(null);
                    }
                }
                catch (Exception e1)
                {
                    System.Diagnostics.Debug.WriteLine("using IP");
                    ipAddress = IPAddress.Parse(hostname);
                }

                //create a TCP/IP socket
                Socket socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, 0);
                NetworkWarden warden = new NetworkWarden(socket, -1);

                warden.callNext = callbackFunction;
                warden.socket.BeginConnect(ipAddress, PORT, handleSuccessfulConnection, warden);
                return(socket);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Unable to connect to server. Error occured: " + e);
                return(null);
            }
        }
コード例 #6
0
 /// <summary>
 /// Allows a program to send data over a socket.
 /// Stores message into byte array and begins sending bytes.
 /// Helper function 'StopSend' will be called once all bytes have sent.
 /// </summary>
 /// <param name="socket"></param>
 /// <param name="data"></param>
 public static void Send(NetworkWarden ward, String data)
 {
     data = data.TrimEnd();//removes any leftover spaces
     byte[] bytes = Encoding.UTF8.GetBytes(data + "\n");
     try
     {
         ward.socket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, StopSend, ward);
     }
     catch (Exception e)
     {
         System.Diagnostics.Debug.WriteLine("Send Failed. Error occured: " + e);
         return;
     }
 }
コード例 #7
0
        /// <summary>
        /// If connection with server successful, complete connection.
        /// Begin listening for server data.
        /// </summary>
        /// <param name="ARSTATE"></param>
        static private void handleSuccessfulConnection(IAsyncResult ARSTATE)
        {
            NetworkWarden warden = (NetworkWarden)ARSTATE.AsyncState;

            try
            {
                //stops the attempt to connect routine
                warden.socket.EndConnect(ARSTATE);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Unable to connect to server. Error occured: " + e);
                return;
            }
            //begin receiving data.
            warden.callNext(warden);
            warden.socket.BeginReceive(warden.buffer, 0, warden.buffer.Length, SocketFlags.None, receiveMessage, warden);
        }
コード例 #8
0
 public static void getData(NetworkWarden ward)
 {
     ward.socket.BeginReceive(ward.buffer, 0, ward.buffer.Length, SocketFlags.None, receiveMessage, ward);
 }
コード例 #9
0
ファイル: Program.cs プロジェクト: nerd3d/SS3500
 static void getData(NetworkWarden ward)
 {
     listenWarden          = ward;
     listenWarden.callNext = getData;
     Networking.getData(listenWarden);
 }
コード例 #10
0
ファイル: Program.cs プロジェクト: nerd3d/SS3500
 static void cleanBuffer(NetworkWarden ward)
 {
     warden = ward;
     warden.message.Clear();
     sendMessage(warden);
 }
コード例 #11
0
ファイル: Program.cs プロジェクト: nerd3d/SS3500
 static void sendName(NetworkWarden ward)
 {
     ward.callNext = getNameBack;
     Networking.Send(ward, "Praise the Sun" + "\n");
 }