在编程过程中,客户端与服务器之间能够进行通讯,但是通讯不太正常啊!
我准备做一个简单的聊天模块,但是,我测试的时候,是下面这种情况:其中,Victor H  是服务器返回来的信息我就是想不通,为什么我发送三条消息才返回来一条我下面把相关的代码贴出来:
服务器端:
C# code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
        public void getthreadmessage(object cl)
        {
            setText d = new setText(Showmsg);//实例化一个委托            
            mydata myclient = (mydata)cl;
            Socket client = myclient.client;
            int cnt = myclient.cnt;
            int recv;//用于表示客户端发送的信息长度
            byte[] data = new byte[1024];
            while (true)//用死循环来不断的从客户端获取信息
            {
                data = new byte[1024];
                try
                {
                    recv = client.Receive(data,data.Length ,0);
                }
                catch
                {
                    break;
                }
                if (recv == 0) break;
                else
                {                    
                     stringdata = Encoding.Default.GetString(data);                  
                    this.Invoke(d, cnt, stringdata);
                }
 
                if (stringdata != "")
                {
                    Thread th = new Thread(new ParameterizedThreadStart(sendthreadmessage));
                    th.IsBackground = true;
                    th.Start(myclient);
                }
                 
            }
           client.Close();
        }
 
        public void sendthreadmessage(object cl)
        {
            setText d = new setText(Showmsg);//实例化一个委托
            //SendMessage sm = new SendMessage(SendMsg);
            mydata myclient = (mydata)cl;
            Socket client = myclient.client;
            try
            {
                string[] arry = stringdata.Split('|');
                string sendmessage;
                if (arry[0].Trim() == "Chat")
                {
                    sendmessage = "Chat|" + arry[1] + "        " + arry[2] + "|" + arry[3]+"|";
                    client.Send(Encoding.Default.GetBytes(sendmessage));
                    
                }
 
            }
            catch
            {
                MessageBox.Show(stringdata);
           }
        
           
        }客户端:
C# code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
        private void button1_Click(object sender, EventArgs e)
        {
            if (client.Connected)
            {
                if (textBox3.Text != "")
                {
                    if (textBox1.Text.Trim() != "")
                        textBox1.Text = textBox1.Text + Environment.NewLine + "我   " + DateTime.Now.ToLongTimeString().ToString() + Environment.NewLine + textBox3.Text;
                    else
                        textBox1.Text = "我   " + DateTime.Now.ToLongTimeString().ToString() + Environment.NewLine + textBox3.Text;                  
                    string sendmessage = "Chat|" + this.nick_name + "|" + DateTime.Now.ToLongTimeString().ToString() + "|"+textBox3.Text+"|";
                    this.client.Send(Encoding.Default.GetBytes(sendmessage));
                    textBox3.Text = "";
 
                }
                else
                    MessageBox.Show("不能发送空消息!");
            }
            else
                MessageBox.Show("与服务器断开连接!");
        }C# code
?
1
2
3
4
5
6
7
8
9
10
 private void hall_Load(object sender, EventArgs e)
        {
           ....
            if (client.Connected)//在连接的时候,先判断客户端是否与服务器建立连接
            {
                Thread thread = new Thread(new ThreadStart(targett));
                thread.Start();
                .......
             }
         }C# code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 public void targett()//从服务器接收信息
        {
            string stringdata;
            byte[] data;
            getmesg d = new getmesg(message);
            try
            {
 
                while (true)
                {
                    data = new byte[1024];
                    int recv = client.Receive(data, data.Length, 0); //接收从服务器返回数据的长度   
                    //将数据从连接的   Socket   接收到接收缓冲区的特定位置。
                    if (recv <= 0)
                    {
                        break;
                    }
                    else
                    {
                        stringdata = Encoding.Default.GetString(data, 0, recv);                       
                        this.Invoke(d, stringdata);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 
        public void message(string mystring)
        {
            string []arry =mystring.Split('|');
            if (arry[0].Trim() == "Chat")
            {
                if (textBox1.Text.Trim() != "")
                    textBox1.Text = textBox1.Text + Environment.NewLine + arry[1] + Environment.NewLine + arry[2];
                else
                    textBox1.Text = arry[1] + Environment.NewLine + arry[2];
            }
 
        }小弟新手,对于多线程方面也是刚刚接触,所以,谢谢大家帮忙啦!