首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

winform的TcpListener TcpClient有关问题,请高手帮忙解答,很着急多谢

2012-05-21 
winform的TcpListener TcpClient问题,请高手帮忙解答,很着急,在线等,谢谢请问:1,下面的客户端服务器端连接

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");            tcpClient.Connect(host.AddressList[0], 8081);                 }        private void ClientFrm_Load(object sender, EventArgs e)        {            //这就是服务器与客户端的一条通讯线路                           //得到服务器端IP,然后建立一个连接                           //取得数据流               NetworkStream clientStream = tcpClient.GetStream();            //只能向服务器发送字节流,所以要编码               byte[] response = new byte[this.textBox1.Text.Length];            response = System.Text.Encoding.ASCII.GetBytes(this.textBox1.Text.ToCharArray());            //通过Write方法把客户端数据发向服务器               clientStream.Write(response, 0, this.textBox1.Text.Length);        }            }} 



[解决办法]
测试通过,两次创建客户端,均可正常连接

你是说tcp连不上,还是网址连不上
[解决办法]
怀疑你那服务器端程序根本没有继续监听
在你第一个连接连上后
你自己netstate看看
[解决办法]
那你要把服务端程序贴出来了。
[解决办法]
不好意思看错了,你把服务端代码的线程处理先去掉试试。
[解决办法]
http://www.cnblogs.com/stone/archive/2005/04/26/145391.html
这篇文章应该能满足你的要求
[解决办法]
http://topic.csdn.net/u/20100612/09/ecdb9f2b-ebb4-41a1-979c-025775bb5086.html
[解决办法]
设置一下 ExclusiveAddressUse = false 看看
[解决办法]
代码没问题啊,我这里调试可以通过。

热点排行