标题:socket问题
取消只看楼主
mipeng
Rank: 2
等 级:论坛游民
帖 子:58
专家分:13
注 册:2009-4-4
结帖率:87.5%
已结贴  问题点数:20 回复次数:0 
socket问题
客户端   
        static  Socket c;
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                int port = 2000;
                string host = "127.0.0.1";
                IPAddress ip = IPAddress.Parse(host);
                IPEndPoint ipe = new IPEndPoint(ip, port);
                c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                c.Connect(ipe);
                MessageBox.Show("与服务器连接成功!", "提示");
                this.button1.Enabled = true;
            }
            catch
            {
                MessageBox.Show("与服务器连接失败!", "提示");
                this.button1.Enabled = false ;
            }
        }
        //发送按钮
        private void button1_Click(object sender, EventArgs e)
        {
            string sendStr = textBox1.Text.Trim().ToString();
            byte[] bs = Encoding.ASCII.GetBytes(sendStr);
            c.Send(bs, bs.Length, 0);
            string recvStr = "";
            byte[] recvBytes = new byte[1024];
            int bytes;
            bytes = c.Receive(recvBytes, recvBytes.Length, 0);
            recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
            MessageBox.Show(recvStr, "提示");
        }
这样写为什么只能与服务器发生1次。在点发送按钮时就提示说“您的主机中的软件放弃了一个已经建立的链接”这该怎么改啊。
我想的是让这个客户端一直和服务器连接着
搜索更多相关主题的帖子: socket 
2009-09-04 16:00



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-284783-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.718874 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved