通过c#

时间:2016-08-02 08:12:50

标签: c# sockets

我有一台服务器具有四排游戏的所有逻辑,我希望在每个玩家转弯后显示状态我有几个课程并且我想发送带有播放位置的游戏每一回合,我都成功地向客户发送了第一步,但客户端在此之后才开枪。客户端和服务器都是控制台应用程序 在我的主服务器端,我有这个

var game = Game.CreateConsoleGame(DifficultyLevel.Hard, ActivePlayer.User, false);
            game.Play();
            Console.ReadKey();

这些线路将使用其他类板,游戏,Iodevice,裁判和玩家 制定游戏的所有逻辑 在课堂上,我有这个

一旦我获得了这个位置,我就画了一个String Builder 用这种方法

public static StringBuilder DrawStringBuilder(int nk)
            {

                cells = new CellStates[6, 7];

                var builder = new StringBuilder();
                var header = "  0        1         2         3        4        5         6";
                var divisor = "-------------------------------------------------------------------";
                builder.AppendLine(header);
                builder.AppendLine(divisor);

                int p = 0;
                for (int i = 0; i < cells.GetLength(0); i++)
                {

                    for (int j = 0; j < cells.GetLength(1); j++)
                    {

                        int counter1 = 0;
                        int counter2 = 1;
                        cells[5, nk] = CellStates.Player;
                        //cells[5, 0] = CellStates.User;
                        var str = cells[i, j] == CellStates.Empty ? "| ······· " : (cells[i, j] == CellStates.User ? "|     0   " : "|   x     ");

                        builder.Append(str);
                        counter1 = counter1 + 2;
                        counter2++;

                    }



                    builder.Append('|');
                    builder.AppendLine();
                    builder.AppendLine(divisor);



                }
                return builder;
            }

我正在使用这种方法 将其发送给客户

  public static void SendingPacket(StringBuilder packet)
            {
                IPAddress ipLocal;
                IPEndPoint ipeLocal;
                TcpListener List;
                TcpClient tc = null;
                NetworkStream ns = null;
                string s2;

                int n = 0;
                try
                {
                    ipLocal = IPAddress.Parse("127.0.0.1");
                    ipeLocal = new IPEndPoint(ipLocal, 8888);
                    List = new TcpListener(ipeLocal);


                    List.Start();
                    tc = List.AcceptTcpClient();

                    ns = tc.GetStream();
                    StreamReader reader = new StreamReader(ns);
                    StreamWriter write = new StreamWriter(ns);
                    string response = null;
                    //Console.WriteLine(response);


                    response = reader.ReadLine();
                    Console.WriteLine(response);
                    Thread.Sleep(200);
                    write.WriteLine(packet);
                    write.Flush();
                    n++;





                    Console.WriteLine("j'envoie");
                    Console.WriteLine("de player");
                    Console.ReadKey();
                }
                catch (Exception ex)
                {

                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    if ((tc != null) && (ns != null))
                    {
                        tc.Close();
                        ns.Close();

                    }

                }
            }

对于客户端我已经接收到了我在主电话中调用的receveingdata方法 为什么我不能收到至少两次我发送的字符串我发现错误 在第二个转弯处说无法从传输连接读取数据,现有连接被远程主机关闭

public static void RecevingData()
        {
            IPAddress ipServeur;
            IPEndPoint ipeServeur;
            TcpClient tc = null;
            NetworkStream ns = null;

            try
            {

                ipServeur = IPAddress.Parse("127.0.0.1");
                ipeServeur = new IPEndPoint(ipServeur, 8888);
                tc = new TcpClient();

                tc.Connect(ipeServeur);
                ns = tc.GetStream();



                string s = null;
                string s1 = "ping";
                string reponse = null;
                StreamWriter write = new StreamWriter(ns);
                StreamReader reader = new StreamReader(ns);
                //Console.WriteLine("un string svp");
                //s = Console.ReadLine();
                write.Flush();
                write.WriteLine(s1);
                write.Flush();
                do
                {
                    reponse = reader.ReadLine();
                    Console.WriteLine(reponse);
                } while (true);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }


        }

我是初学者所以也许我的问题太愚蠢但是我只是因为变得更好所以请尽量放纵,我的想法是在每个玩家轮到后以同样的方式发送stringbuilder

0 个答案:

没有答案