当前位置: 首页>>代码示例>>C#>>正文


C# StringSocket.BeginSend方法代码示例

本文整理汇总了C#中CustomNetworking.StringSocket.BeginSend方法的典型用法代码示例。如果您正苦于以下问题:C# StringSocket.BeginSend方法的具体用法?C# StringSocket.BeginSend怎么用?C# StringSocket.BeginSend使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CustomNetworking.StringSocket的用法示例。


在下文中一共展示了StringSocket.BeginSend方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Connect

        /// <summary>
        /// Connect to the server at the given hostname and port and with the given name of username and
        /// password.
        /// </summary>
        public bool Connect(String hostname, int port, String password)
        {
            try
            {
                TcpClient client;
                //Case 1: Using IP
                IPAddress address;
                if (IPAddress.TryParse(hostname, out address))
                {
                    client = new TcpClient();
                    client.Connect(address, port);
                }
                else
                {
                    //Case 2: Using DNS
                    client = new TcpClient(hostname, port);
                }

                socket = new StringSocket(client.Client, UTF8Encoding.Default);
                //PASSWORD[esc]password\n
                char esc = (char)27;
                socket.BeginSend("PASSWORD" + esc + password + "\n", (e, p) => { }, null);
                socket.BeginReceive(LineReceived, null);

                return true;
            }
            catch
            {
                return false;
            }
        }
开发者ID:unaveed,项目名称:Spreadsheet,代码行数:35,代码来源:Client.cs

示例2: Connect

 public void Connect(string hostname, int port, string clientPassword)
 {
     // initial connection to the server
     if (clientSSocket == null)
     {
         password = clientPassword;
         try
         {
             TcpClient client = new TcpClient(hostname, port);
             clientSSocket = new StringSocket(client.Client, UTF8Encoding.Default);
             clientSSocket.BeginSend("PASSWORD" + "\\e" + password + "\n", (ex, p) => { }, null);
             clientSSocket.BeginReceive(LineReceived, null);
         }
         catch (SocketException e)
         {
             Console.WriteLine(e.Message);
         }
     }
     // not sure if this part is necessary?
     else
     {
         //clientSSocket.Close(); we don't want to close the socket unless the client is disconnecting
         TcpClient client = new TcpClient(hostname, port);
         clientSSocket = new StringSocket(client.Client, UTF8Encoding.Default);
         clientSSocket.BeginSend("PASSWORD"+ "\\e" + password + "\n", (ex, p) => { }, null);
         clientSSocket.BeginReceive(LineReceived, null);
     }
 }
开发者ID:hodgeskyjon,项目名称:3505_Spring_Project,代码行数:28,代码来源:SpreadsheetClientModel.cs

示例3: Connect

        /// <summary>
        /// Connect to the server at the given hostname and port, with the given name.
        /// </summary>
        public void Connect(String hostname, int port, String name)
        {
            if (socket == null)
            {
                TcpClient client = new TcpClient(hostname, port);
                socket = new StringSocket(client.Client, UTF8Encoding.Default);

                this.name = name;
                this.hostname = hostname;

                socket.BeginSend("PLAY " + name + "\n", (e, p) => { }, null);
                socket.BeginReceive(LineReceived, null);
            }
        }
开发者ID:jimibue,项目名称:cs3505,代码行数:17,代码来源:BoggleClientModel.cs

示例4: Connect

        /// <summary>
        /// Connect to the server at the given hostname and port and with the give name.
        /// </summary>
        public void Connect(string hostname, int port, String name)
        {
            if (socket == null||!validName )
            {
                if (name.StartsWith("PLAY ") && name.Substring(5).Trim().Length > 0)
                    validName = true;
                
                TcpClient client = new TcpClient(hostname, port);
                socket = new StringSocket(client.Client, UTF8Encoding.Default);

                socket.BeginSend(name + "\n", (e, p) => { }, null);
                socket.BeginReceive(LineReceived, null);
            }
        }
开发者ID:jimibue,项目名称:cs3505,代码行数:17,代码来源:BoggleClientModel.cs

示例5: Main

        static void Main(string[] args)
        {
            //new server
            TcpListener server = new TcpListener(IPAddress.Any, 4010);

            server.Start();
            //blocks until user connects
            Socket serverSocket = server.AcceptSocket();
            Console.WriteLine("hey");

            // Wrap the two ends of the connection into StringSockets
            StringSocket sendSocket = new StringSocket(serverSocket, new UTF8Encoding());
            sendSocket.BeginSend("hi", (e, o) => { }, null);
            Console.ReadLine();
        }
开发者ID:jiiehe,项目名称:cs3500,代码行数:15,代码来源:Program.cs

示例6: Connect

 /// <summary>
 /// Connect to the server at the given hostname and port and with the give name.
 /// </summary>
 public void Connect(string IPAddress, String name)
 {
     if (socket == null)
     {
         try
         {
             TcpClient client = new TcpClient(IPAddress, 2000);
             socket = new StringSocket(client.Client, UTF8Encoding.Default);
             socket.BeginSend("PLAY " + name + "\n", (e, p) => { }, null);
             socket.BeginReceive(LineReceived, null);
         }
         catch (Exception e)
         {
             //no such host event
             noSuchHostEvent(e.Message);
         }
     }
 }
开发者ID:matelau,项目名称:Boggle-Game-PS10,代码行数:21,代码来源:BoggleClientModel.cs

示例7: Connect

 /// <summary>
 /// Takes a string representing the IP Address of the server, an int with the port number and the players name.
 /// It then connects to the server and sends the play message with the players name.
 /// </summary>
 public void Connect(String password)
 {
     if (socket == null || !socket.Connected())
     {
         // Tries to connect to the server and then send the play message
         try
         {
             TcpClient client = new TcpClient(IP, port);
             socket = new StringSocket(client.Client, UTF8Encoding.Default);
             socket.BeginSend(password, (e, p) => { }, null);
             socket.BeginReceive(LineReceived, null);
         }
         // If it is unable to connect it catches the exception and throws it on
         catch (SocketException e)
         {
             throw e;
         }
     }
 }
开发者ID:keivnlee,项目名称:Spreadsheet,代码行数:23,代码来源:Class1.cs

示例8: runGame

        public void runGame()
        {
            server = new BoggleServer.BoggleServer(2000, 10, "..\\..\\..\\dictionary", "AAAABBBBCCCCDDDD");
            player1 = new TcpClient("localhost", 2000);
            player2 = new TcpClient("localhost", 2000);
            Socket p1socket = player1.Client;
            Socket p2socket = player2.Client;
            p1 = new StringSocket(p1socket, new UTF8Encoding());
            p2 = new StringSocket(p2socket, new UTF8Encoding());
            p1.BeginSend("PLAY player1\n", (e, o) => { }, null);
            p2.BeginSend("PLAY player2\n", (e, o) => { }, null);
            mre = new ManualResetEvent(false);
            receivedWords = new List<string>();
            for (int i = 0; i < 20; i++)
            {
                p1.BeginReceive(callback, p1);
            }
            mre.WaitOne(1000);

            Assert.IsTrue(receivedWords.Contains("START AAAABBBBCCCCDDDD 10 PLAYER2"));
        }
开发者ID:ryoia,项目名称:BoggleGames,代码行数:21,代码来源:UnitTest1.cs

示例9: Sender

            /// <summary>
            /// Sends strings consisting of the character 'A' + version.  Strings are sent in
            /// the pattern A, AA, AAA, and so on.  COUNT such strings are sent.
            /// </summary>
            private void Sender(int version, StringSocket socket)
            {
                // Determine what character to use
                char c = (char)('A' + version);

                int count = 0;     // Number if times callback has been called
                String msg = "";   // String to send

                // Sent COUNT strings
                for (int i = 0; i < COUNT; i++)
                {
                    // Make the message one character longer
                    msg += c;

                    // Send the message.  The callback atomically updates count.
                    socket.BeginSend(msg + "\n", (e, p) => { Interlocked.Increment(ref count); }, null);
                }

                // Wait until all of the callbacks have been called
                SpinWait.SpinUntil(() => count == COUNT);
            }
开发者ID:amozoss,项目名称:CS3505,代码行数:25,代码来源:StringSocketTest.cs

示例10: Blast2

 /// <summary>
 /// Sends COUNT strings that begin with "Blast2" followed by a number.
 /// </summary>
 public void Blast2(StringSocket s)
 {
     for (int i = 0; i < COUNT; i++)
     {
         s.BeginSend("Blast2 " + i + "\n", (e, p) => { }, null);
     }
 }
开发者ID:amozoss,项目名称:CS3505,代码行数:10,代码来源:StringSocketTest.cs

示例11: run

            public void run(int port)
            {
                TcpListener server = null;
                TcpClient client = null;

                try
                {
                    // Create and start a server and client
                    server = new TcpListener(IPAddress.Any, port);
                    server.Start();
                    client = new TcpClient("localhost", port);

                    // Obtain the sockets from the two ends of the connection.  We are using the blocking AcceptSocket()
                    // method here, which is OK for a test case.
                    Socket serverSocket = server.AcceptSocket();
                    Socket clientSocket = client.Client;

                    // Wrap the two ends of the connection into StringSockets
                    StringSocket sendSocket = new StringSocket(serverSocket, new UTF8Encoding());
                    StringSocket receiveSocket = new StringSocket(clientSocket, new UTF8Encoding());

                    // Set up the events
                    e1 = new ManualResetEvent(false);
                    e2 = new ManualResetEvent(false);
                    e3 = new ManualResetEvent(false);

                    // Ask for two lines of text.
                    receiveSocket.BeginReceive(Callback1, null);
                    receiveSocket.BeginReceive(Callback2, null);

                    // Send some text, one character at a time
                    foreach (char c in message1 + "\n" + message2 + "\n")
                    {
                        sendSocket.BeginSend(c.ToString(), SendCallback, null);
                    }

                    // Make sure everything happened properly.
                    Assert.IsTrue(e1.WaitOne(2000));
                    Assert.IsTrue(e2.WaitOne(2000));
                    Assert.IsTrue(e3.WaitOne(2000));

                    Assert.AreEqual(message1, string1);
                    Assert.AreEqual(message2, string2);
                    Assert.AreEqual(message1.Length + message2.Length + 2, count);
                }
                finally
                {
                    server.Stop();
                    client.Close();
                }
            }
开发者ID:amozoss,项目名称:CS3505,代码行数:51,代码来源:StringSocketTest.cs

示例12: TestWordCommand6

            public void TestWordCommand6()
            {
                // Test the case in which a the two clients play the same word.

                // This will coordinate communication between the threads of the test cases
                mre3 = new ManualResetEvent(false);
                mre4 = new ManualResetEvent(false);
                mre5 = new ManualResetEvent(false);
                mre6 = new ManualResetEvent(false);

                // Create a two clients to connect with the Boggle Server.
                TcpClient TestClient1 = new TcpClient("localhost", 2000);
                TcpClient TestClient2 = new TcpClient("localhost", 2000);

                // Create a client socket and then a client StringSocket.
                Socket ClientSocket1 = TestClient1.Client;
                Socket ClientSocket2 = TestClient2.Client;
                StringSocket ClientSS1 = new StringSocket(ClientSocket1, new UTF8Encoding());
                StringSocket ClientSS2 = new StringSocket(ClientSocket2, new UTF8Encoding());

                try
                {
                    // Connect two clients together to play Boggle.
                    ClientSS1.BeginSend("PLAY Client1\n", (e, o) => { }, null);
                    ClientSS2.BeginSend("PLAY Client2\n", (e, o) => { }, null);

                    ClientSS1.BeginReceive(CompletedReceive1, 1);
                    ClientSS2.BeginReceive(CompletedReceive2, 2);
                    Assert.AreEqual(true, mre1.WaitOne(timeout), "Timed out waiting 1");
                    Assert.AreEqual(true, mre2.WaitOne(timeout), "Timed out waiting 2");

                    // Get all of the legal words from the gameboard for these two players.
                    HashSet<string> LegalWords = FindLegalWords(GameboardString);

                    // One player plays a legal word. Assert that the score changes for each
                    // player appropriately.
                    ClientSS2.BeginSend("WORD " + LegalWords.ElementAt(0) + "\n", (e, o) => { }, null);

                    ClientSS1.BeginReceive(CompletedReceive3, 3);
                    ClientSS2.BeginReceive(CompletedReceive4, 4);

                    Assert.AreEqual(true, mre3.WaitOne(timeout), "Timed out waiting 3");
                    Assert.AreEqual("SCORE 0 1", s3);

                    Assert.AreEqual(true, mre4.WaitOne(timeout), "Timed out waiting 4");
                    Assert.AreEqual("SCORE 1 0", s4);

                    ClientSS1.BeginSend("WORD " + LegalWords.ElementAt(0) + "\n", (e, o) => { }, null);

                    ClientSS1.BeginReceive(CompletedReceive5, 5);
                    ClientSS2.BeginReceive(CompletedReceive6, 6);

                    Assert.AreEqual(true, mre5.WaitOne(timeout), "Timed out waiting 5");
                    Assert.AreEqual("SCORE 0 0", s5);

                    Assert.AreEqual(true, mre6.WaitOne(timeout), "Timed out waiting 6");
                    Assert.AreEqual("SCORE 0 0", s6);
                }
                finally
                {
                    ClientSS1.Close();
                    ClientSS2.Close();
                }
            }
开发者ID:jon-whit,项目名称:PS8,代码行数:64,代码来源:BoggleServerTests.cs

示例13: run

            public void run(int port)
            {
                // Create and start a server and client.
                TcpListener server = null;
                TcpClient client = null;

                try
                {
                    server = new TcpListener(IPAddress.Any, port);
                    server.Start();
                    client = new TcpClient("localhost", port);

                    // Obtain the sockets from the two ends of the connection.  We are using the blocking AcceptSocket()
                    // method here, which is OK for a test case.
                    Socket serverSocket = server.AcceptSocket();
                    Socket clientSocket = client.Client;

                    // Wrap the two ends of the connection into StringSockets
                    StringSocket sendSocket = new StringSocket(serverSocket, new UTF8Encoding());
                    StringSocket receiveSocket = new StringSocket(clientSocket, new UTF8Encoding());

                    // This will coordinate communication between the threads of the test cases
                    mre1 = new ManualResetEvent(false);
                    mre2 = new ManualResetEvent(false);

                    // Make two receive requests
                    receiveSocket.BeginReceive(CompletedReceive1, 1);
                    receiveSocket.BeginReceive(CompletedReceive2, 2);

                    // Now send the data.  Hope those receive requests didn't block!
                    String msg = "Hello world\nThis is a test\n";
                    foreach (char c in msg)
                    {
                        sendSocket.BeginSend(c.ToString(), (e, o) => { }, null);
                    }

                    // Make sure the lines were received properly.
                    Assert.AreEqual(true, mre1.WaitOne(timeout), "Timed out waiting 1");
                    Assert.AreEqual("Hello world", s1);
                    Assert.AreEqual(1, p1);

                    Assert.AreEqual(true, mre2.WaitOne(timeout), "Timed out waiting 2");
                    Assert.AreEqual("This is a test", s2);
                    Assert.AreEqual(2, p2);
                }
                finally
                {
                    server.Stop();
                    client.Close();
                }
            }
开发者ID:jimibue,项目名称:cs3505,代码行数:51,代码来源:StringSocketTest.cs

示例14: PlayerGameHistoryWebPage

        /// <summary>
        /// WebPage containing an individual player's Game History
        /// </summary>
        private void PlayerGameHistoryWebPage(StringSocket ss, string playerName)
        {
            using (MySqlConnection connect = new MySqlConnection(connectionString))
            {
                // Open MySqlConnection to Database
                connect.Open();

                using (MySqlCommand command = connect.CreateCommand())
                {
                    // Query for Player ID and Count
                    command.CommandText = "select PlayerID, count(*) from Players where PlayerName = @playerName;";
                    command.Parameters.Clear();
                    command.Parameters.AddWithValue("@playerName", playerName);
                    command.Prepare();

                    int numRowsWithPlayerID = 0;
                    int playerID = 0;

                    MySqlDataReader reader;

                    // Read query results and store Player ID
                    using (reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            numRowsWithPlayerID = reader.GetInt32(1);

                            try
                            {
                                playerID = reader.GetInt32(0);
                            }
                            catch
                            {
                            }
                        }
                    }

                    // If Query for PlayerID yields no rows, send to ErrorWebPage
                    if (numRowsWithPlayerID == 0)
                    {
                        ErrorWebPage(ss);
                    }
                    // Else build WebPage
                    else
                    {
                        // Begin to send HTML Web Page
                        ss.BeginSend("HTTP/1.1 200 OK\r\n", (ee, pp) => { }, null);
                        ss.BeginSend("Connection: close\r\n", (ee, pp) => { }, null);
                        ss.BeginSend("Content-Type: text/html; charset=UTF-8\r\n", (ee, pp) => { }, null);
                        ss.BeginSend("\r\n", (ee, pp) => { }, null);

                        // WebPage as List
                        List<String> webPage = new List<String>();

                        webPage.Add("<html>");
                        webPage.Add("<title>"+ playerName + ((playerName[playerName.Length-1]==('s'))?"\'":"\'s") + " Game History</title>");
                        webPage.Add("<body>");
                        webPage.Add("<h1>"+ playerName + ((playerName[playerName.Length-1]==('s'))?"\'":"\'s") + " Game History</h1>");
                        webPage.Add("<table border=\"1\">");

                        // Table column headers
                        webPage.Add("<tr><td>Game ID</td><td>Date</td><td>Time</td><td>Opponent Name</td><td>Player Score</td><td>Opponent Score</td></tr>");

                        string opponentName = "";
                        bool isPlayerA = false;
                        //get the game IDs associate to the playerID
                        List<int> gameIDs = new List<int>();
                        command.CommandText = "select GameID from GameInfo where (PlayerAID = @playerID or PlayerBID = @playerID);";
                        command.Parameters.Clear();
                        command.Parameters.AddWithValue("@playerID", playerID);
                        command.Prepare();
                        using (reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                gameIDs.Add(reader.GetInt32(0));
                            }
                        }

                        // Get EndDate Time, PlayerA's Score, and Player B's Score For Each game

                        foreach (int gameID in gameIDs)
                        {
                            // Query for EndDate Time, PlayerA's Score, and Player B's Score
                            opponentName = CheckPlayerAOrB(command, ref reader, gameID, playerID, ref isPlayerA);
                            command.CommandText = "select EndDateTime, ScoreA, ScoreB from GameInfo where GameID = @gameID;";
                            command.Parameters.Clear();
                            command.Parameters.AddWithValue("@gameID", gameID);
                            command.Prepare();

                            string endDateTime = "";
                            int playerAscore = 0;
                            int playerBscore = 0;
                            using (reader = command.ExecuteReader())
                            {
                                if (reader.Read())
                                {
//.........这里部分代码省略.........
开发者ID:ryoia,项目名称:BoggleGames,代码行数:101,代码来源:BS.cs

示例15: ErrorWebPage

        /// <summary>
        /// WebPage containing Error Message and Helpful Information
        /// </summary>
        private void ErrorWebPage(StringSocket ss)
        {
            using (MySqlConnection connect = new MySqlConnection(connectionString))
            {
                // Begin to send HTML Web Page
                ss.BeginSend("HTTP/1.1 200 OK\r\n", (ee, pp) => { }, null);
                ss.BeginSend("Connection: close\r\n", (ee, pp) => { }, null);
                ss.BeginSend("Content-Type: text/html; charset=UTF-8\r\n", (ee, pp) => { }, null);
                ss.BeginSend("\r\n", (ee, pp) => { }, null);

                // Store Web Page as List
                List<String> webPage = new List<String>();

                webPage.Add("<html>");
                webPage.Add("<title>Exist, this does not!</title>");
                webPage.Add("<body>");
                webPage.Add("<center>");
                webPage.Add("<h1>\"This isn't the page you're looking for.\"</h1>");
                webPage.Add("<img src = \"https://dl.dropboxusercontent.com/u/87615358/Images/ObiWan.jpg\">");
                webPage.Add("</center>");
                webPage.Add("<p>");
                webPage.Add("<h3>This URL contains nothing.</h3></br>Please change the URL to match the following format: </br>");
                webPage.Add("<ul style=\"list-style-type:circle\">");
                webPage.Add("<li><b>/players</b></li><ul><li><i>a web page of the Player Records will display</i></li></ul></br>");
                webPage.Add("<li><b>/games?player=PLAYERNAME</b> (Replacing PLAYERNAME with a legit player name)</li><ul><li><i>a web page containing their Games History will display</i></li></ul></br>");
                webPage.Add("<li><b>/game?id=GAMEID</b> (Replacing GAMEID with a legit game ID)</li><ul><li><i>a web page containing a Game Summary will display</i></li></ul>");
                webPage.Add("</ul>");

                webPage.Add("<a href=\"/players\">Boggle Records</a>");

                webPage.Add("</body>");
                webPage.Add("</html>\n");

                StringBuilder webPageAsString = new StringBuilder();

                foreach (String line in webPage)
                {
                    webPageAsString.Append(line);
                }

                // Send rest of Web Page
                ss.BeginSend(webPageAsString.ToString(), (ee, pp) => { CloseSocketCallback(ss); }, null);
            }
        }
开发者ID:ryoia,项目名称:BoggleGames,代码行数:47,代码来源:BS.cs


注:本文中的CustomNetworking.StringSocket.BeginSend方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。