日期:2014-05-18  浏览次数:21278 次

winform的TcpListener TcpClient问题,请高手帮忙解答,很着急,在线等,谢谢
请问:
1,下面的客户端服务器端连接,为什么只能连接一次?第二次就连接不上了
2,是多客户端连接,请指教,谢谢,在线等
ServerFrm.cs
C# code
/**********TcpListener和TcpClient研究********** 
 * 要点一:TcpListener起动后,如果有客户请求,就会建立一个TcpClient连接. 
 * 要点二:通过TcpClient取得NetworkStream对象 
 * 要点三:通过NetworkStream的Write和Read方法向连接的另一端发或接收数据 
 * 要点四:传输的数据只能是字符流,需要编码. 
**********************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace WinNet
{
    public partial class ServerFrm : Form
    {
        private Thread processor;
        private TcpListener tcpListener;
        string clientStr;
        public ServerFrm()
        {            
            InitializeComponent();
            ServerFrm_Load();
        }

        private void ServerFrm_Load()
        {
            //需要在新的线程里监听客户端   
            try
            {
                processor = new Thread(new ThreadStart(StartListening));
                processor.Start();
            }
            catch (Exception ex)
            {
            }
        }
        private void StartListening()
        {
            //创建一个监听对象
            tcpListener = new TcpListener(IPAddress.Any, 8081);
            tcpListener.Start();
            //循环监听   
            while (true)
            {
                try
                {
                    //取得客户端的连接   
                    TcpClient tcpClient = tcpListener.AcceptTcpClient();
                    //取得客户端发过来的字节流   
                    NetworkStream clientStream = tcpClient.GetStream();
                    //把字节流读入字节数组   
                    byte[] buffer = new byte[51];
                    clientStream.Read(buffer,0,51);
                    //不可以在此直接设置this.Text,线程问题.   
                    clientStr = System.Text.Encoding.ASCII.GetString(buffer);

                  
                    string strURL = "http://65.58.53.45:8088/data.aspx";
                    HttpWebRequest request;

                    request = (HttpWebRequest)WebRequest.Create(strURL);
                    //Post请求方式
                    request.Method = "POST";
                    //内容类型
                    request.ContentType = "application/x-www-form-urlencoded";
                    //参数经过URL编码
                    string paraUrlCoded = "value=22";//测试数据
                    byte[] payload;
                    //将URL编码后的字符串转化为字节
                    payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
                    //设置请求的ContentLength
                    request.ContentLength = payload.Length;
                    //获得请求流
                    Stream writer = request.GetRequestStream();
                    //将请求参数写入流
                    writer.Write(payload, 0, payload.Length);

                    //关闭请求流
                    writer.Close();
                }
                catch
                {
                }
            }
        }
        
    }
}





ClientFrm.cs
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Net;
using System.Net.Sockets;

namespace WinNet
{
    public partial class ClientFrm : Form
    {
        private TcpClient tcpClient;
        public ClientFrm()
        {
            InitializeComponent();
            tcpClient = new TcpClient();
            IPHostEntry host = Dns.GetHostEntry("127.0.0.1");
            tcpCl