使用TCP Socket帮助.net(C#)中的多人游戏 [英] Help in Multiplayer Game in .net (C#) using TCP Socket

查看:63
本文介绍了使用TCP Socket帮助.net(C#)中的多人游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在button2_Click事件上附加#字符。如果我尝试以相同的形式打印GameInfo.GameID

的值,则不会追加#字符。但是在下一个表单中,如果我将其值发送到服务器,#字符将作为发送字符串的第二部分发送到服务器。

任何人都可以用我的代码解释问题吗?我是


I need to append # character on button2_Click event. If i try to print value of GameInfo.GameID
in the same form the # character is not appended. But in the next form if i send its value to the server the # character is sent to the server as second part of the string sent.
Can anyone explain the problem with my code? I

public partial class Form3 : Form
    {
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        NetworkStream serverStream = default(NetworkStream);
       
        string readData = null;
        string USERNAME = null;
        string returndata = null;
        int LENGTH = 0;
        //TO CHECK IF FORM IS ALREADY SHOWN OR NOT
        bool CHECKINSTANCE = false;
        public Form3()
        {
            InitializeComponent();
        }
        private void Form3_Load(object sender, EventArgs e)
        {
      
            msg();
            clientSocket.Connect("127.0.0.1", 8888);
            serverStream = clientSocket.GetStream();

            byte[] outStream = System.Text.Encoding.ASCII.GetBytes("$DU" + LoginInfo.UserID + "$");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            Thread ctThread = new Thread(getMessage);
            ctThread.Start();


        }
        private void getMessage()
        {
            string trim = null;
            try
            {
                while (true)
                {
                    readData = null;
                    returndata = null;
                    USERNAME = null;

                    LENGTH = 0;
                    int CUT = 0;
                    serverStream = clientSocket.GetStream();
                    int buffSize = 0;
                    byte[] inStream = new byte[10025];
                    buffSize = clientSocket.ReceiveBufferSize;
                    serverStream.Read(inStream, 0, buffSize);
                    returndata = System.Text.Encoding.ASCII.GetString(inStream);
                    serverStream.Flush();
                    readData = returndata;

                    trim = readData.Substring(0, 6);
                    if (trim == "$CHAT$")
                    {
                        readData = readData.Remove(0, 6);

                        for (int i = 0; i < readData.Length; i++)
                        {

                            if (readData[i] == '&')
                            {
                                USERNAME = readData.Substring(0, readData.IndexOf("&"));
                                CUT = i + 1;
                                break;
                            }
                        }
                        if (CUT != 0)
                        {
                            readData = readData.Substring(CUT);
                        }
                        if (USERNAME != null)
                        {
                            for (int i = 0; i < USERNAME.Length; i++) ;
                            {
                                LENGTH = LENGTH + 1;
                            }
                        }
                        msg();
                    }
                    else if (trim == "$GAME$")
                    {
                        gamemsg();
                    }

                }
            }
            catch
            {
            }
        }
        private void msg()
        {
            string temp=null;
            if (this.InvokeRequired)
                this.Invoke(new MethodInvoker(msg));
            else
            {   
               
                richTextBox1.ReadOnly = false;
                if (USERNAME == null)
                {
                    richTextBox1.Text = richTextBox1.Text + Environment.NewLine + readData;
          
                }
                else if (USERNAME == LoginInfo.UserID)
                {  
                    richTextBox1.Text = richTextBox1.Text + Environment.NewLine + "Me >> " + readData;
                          }
                else
                {
                    richTextBox1.Text = richTextBox1.Text + Environment.NewLine + USERNAME + " says>> " + readData;
          
                }

            }

        }

        private void gamemsg()
        {
            if (this.InvokeRequired)
                this.Invoke(new MethodInvoker(gamemsg));
            else
            {
                bool match = false;
                  readData = readData.Remove(0, 6);
                 USERNAME = readData;

                 USERNAME = readData.Substring(0, readData.IndexOf("&") - 1);
                 for (int i = 0; i < listBox1.Items.Count; i++)
                 {
                     if (listBox1.Items[i].ToString() == "Game Created by " + USERNAME)
                         match = true;
                 }
                 if (match == false)//to save from listing duplicate games
                     listBox1.Items.Add("Game Created by " + USERNAME);
               
            }
        }

        }

              private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                GameInfo.GameID = "$POINT$" + listBox1.SelectedItem.ToString().Substring(16) + "#";
              //  this.Close();
                textBox3.Text = GameInfo.GameID;
                Form4 form4 = new Form4();
                form4.Show();
            }
            else
            {
                label3.Text = "Please Select a Game";
            }
        }

}
    
    }





以下是我在另一个winform中用于将其发送到服务器的代码





Here is the code i used in another winform to send it to the server

private void Form4_Load(object sender, EventArgs e)
        {
            string readData = null;
            int buffSize = 0;
            byte[] outStream = null;
            byte[] inStream = new byte[10025];
            int point = 0;
           
       
            clientSocket.Connect("127.0.0.1", 8888);
            serverStream = clientSocket.GetStream();
            // send code to get the card
            outStream = System.Text.Encoding.ASCII.GetBytes("$CARD$");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();
            
            buffSize = clientSocket.ReceiveBufferSize;
            serverStream.Read(inStream, 0, buffSize);
            readData = System.Text.Encoding.ASCII.GetString(inStream);
            serverStream.Flush();
            assigncard(readData);
            point=assignpoints(Code1,Code2,Code3,Code4,Code5);
            Flush(Code1, Code2, Code3, Code4, Code5);

            
            outStream = System.Text.Encoding.ASCII.GetBytes(GameInfo.GameID + LoginInfo.UserID + "#" + point);
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();
             
            Thread pointThread = new Thread(getPoints);
            pointThread.Start();
        }

推荐答案

DU+ LoginInfo.UserID +
DU" + LoginInfo.UserID + "


) ;
serverStream.Write(outStream,0,outStream.Length);
serverStream.Flush();

线程ctThread =新线程(getMessage);
ctThread .Start();


}
private void getMessage()
{
string trim = null;
try
{
while(true)
{
readData = null;
returndata = null;
USERNAME = null;

LENGTH = 0;
int CUT = 0;
serverStream = clientSocket.GetStream();
int buffSize = 0;
byte [] inStream = new byte [10025];
buffSize = clientSocket.ReceiveBufferSize ;
serverStream.Read(inStream,0,buffSize);
returndata = System.Text.Encoding.ASCII.GetString(inStream);
serverStream.Flush();
readData = returndata;

trim = readData.Substring(0,6);
if(trim ==
"); serverStream.Write(outStream, 0, outStream.Length); serverStream.Flush(); Thread ctThread = new Thread(getMessage); ctThread.Start(); } private void getMessage() { string trim = null; try { while (true) { readData = null; returndata = null; USERNAME = null; LENGTH = 0; int CUT = 0; serverStream = clientSocket.GetStream(); int buffSize = 0; byte[] inStream = new byte[10025]; buffSize = clientSocket.ReceiveBufferSize; serverStream.Read(inStream, 0, buffSize); returndata = System.Text.Encoding.ASCII.GetString(inStream); serverStream.Flush(); readData = returndata; trim = readData.Substring(0, 6); if (trim == "


CHAT


这篇关于使用TCP Socket帮助.net(C#)中的多人游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆