| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1608 人关注过本帖
标题:[求助]关于Tcp通讯方面的问题
只看楼主 加入收藏
天使不落泪
Rank: 1
等 级:新手上路
帖 子:104
专家分:0
注 册:2007-7-24
结帖率:0
收藏
 问题点数:0 回复次数:8 
[求助]关于Tcp通讯方面的问题
服务端对一个 客户端的功能 我实现了
可是有2个 客户端同时连接 服务端的时候 就只有一个客户端 能收到服务端发来的信息, 想问下怎么实现 2个同时收信息啊,谢谢拉
搜索更多相关主题的帖子: Tcp 通讯 
2007-12-13 09:29
梦心
Rank: 4
来 自:福建平和
等 级:贵宾
威 望:13
帖 子:1910
专家分:0
注 册:2007-5-11
收藏
得分:0 
.............
网络通信不专业啊我

我清高和我骄傲的倔强,在风中大声的唱:我不听,我不听~~做我自己最特别,呼呼~~啦啦~~~
我的博客园地址: [url]http://[/url]
2007-12-13 09:41
xcclgl001
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2007-12-13
收藏
得分:0 
用多线程不能解决吗
2007-12-13 17:54
quanercao
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2007-10-16
收藏
得分:0 
把当前在线的客户端的IP和PORT列表保存,服务器端根据列表即时建立TCP连接,分别发送
当然如楼上所说,建立多个线程,每个线程为一个客户端服务(特定的IP和PORT)也是可行的,不过不建议采用。把你的线程数降到最少
2007-12-13 20:12
yangglemu
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:273
专家分:521
注 册:2006-9-15
收藏
得分:0 
这是网络通信并发连接处理的问题,用线程解决,每遇到一个连接请求,单独创建一个线程完成,互不干扰
2007-12-14 11:47
tanxiaolin
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2007-12-12
收藏
得分:0 
你这样写试试,我是VS2005调试通过了的。
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using
using
using
using System.Threading;
using System.Collections;

namespace TCPtest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private string RFileName = "ChatRecord.Txt";


        public void Wfile(string filename, string filecont)
        {
            FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter wfile = new StreamWriter(fs);
            wfile.BaseStream.Seek(0, SeekOrigin.End);
            wfile.WriteLine(filecont);
            wfile.Flush();
            wfile.Close();
        }//写纪录
        private void CloseWin_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }

        private void ViewText_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(RFileName);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private TcpListener Ilistener;
        private Thread Iprocessor;
        private Socket Iclientsocket2;
        private Thread Iclientservice;
        private void ExitBtn_Click(object sender, EventArgs e)
        {
            if (Iprocessor.IsAlive) Iprocessor.Abort();
            Ilistener.Stop();
            RevStr.Text = "[" + System.DateTime.Now + "]:终止服务\n";
            OpenBtn.Enabled = true;//启动键有效
            ExitBtn.Enabled = false;//停止键无效
            ViewText.Enabled = false;//纪录有效
            SendBtn.Enabled = false;//发送有效
        }

        private void OpenBtn_Click(object sender, EventArgs e)
        {
            Iprocessor = new Thread(new ThreadStart(IStartListening));
            Iprocessor.Start();
            RevStr.Text = "[" + System.DateTime.Now + "]:启动服务\n";
            if (File.Exists(RFileName)) File.Delete(RFileName);
            Wfile(RFileName, RevStr.Text);
            OpenBtn.Enabled = false;//启动键无效
            ExitBtn.Enabled = true;//停止键有效
            ViewText.Enabled = true;//纪录有效
            SendBtn.Enabled = true;//发送有效
        }
        private NetworkStream ns;
        private TcpClient clientsocket;
        private UserIP=new ((()).AddressList[0].Address);
        private void SendBtn_Click(object sender, EventArgs e)
        {
            int ToPortStr=6886;
            
            if(PortStr.Text.ToString().Trim()!="")ToPortStr=int.Parse(PortStr.Text.ToString());
            string ToIpStr=IPStr.Text;
            string ToCont=SendStr.Text.ToString();
            string ToContentStr=UserIP.ToString()+ToCont;
            SendStr.Text="";
            try
            {
                clientsocket = new TcpClient(ToIpStr,ToPortStr);
                clientsocket.SendTimeout=1000;
                clientsocket.ReceiveTimeout=1000;
                clientsocket.ReceiveBufferSize=256;
                ns = clientsocket.GetStream();
                //
                Byte[] outbytes = System.Text.Encoding.Unicode.GetBytes(ToContentStr.ToCharArray());
                int WriteLength=(int)clientsocket.ReceiveBufferSize;
                if(outbytes.Length<WriteLength)WriteLength=outbytes.Length;
                ns.Write(outbytes,0,outbytes.Length);
                RevStr.Text = "["+System.DateTime.Now+"]发:"+ToContentStr;
                Wfile(RFileName,RevStr.Text);
                clientsocket.Close();
            }
            catch(Exception ex)
            {
                if(ex!=null)MessageBox.Show("无法连接对方机器或者未启动服务","Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }//发送
        private void IServiceClient()
        {
            bool Ikeepalive=true;
            int RCharNum=256;
            string ReceiveMsg ="";
            if ((Iclientsocket2.Connected)&&(Ikeepalive))
            {
                try
                {
                    Byte[] buffer = new Byte[RCharNum];
                    Iclientsocket2.Receive(buffer,0,RCharNum,SocketFlags.None);
                    string Iclientcommand = System.Text.Encoding.Unicode.GetString(buffer);
                    ReceiveMsg = Iclientcommand;

                }
                catch(Exception e)
                {
                    if(e!=null)MessageBox.Show("传输出错"+e.ToString());
                    Ikeepalive=false;
                }
            }
            RevStr.Text = "["+System.DateTime.Now+"]\n收:"+ReceiveMsg;
            Wfile(RFileName,RevStr.Text);
            Iclientsocket2.Shutdown(SocketShutdown.Both);
            Iclientsocket2.Close();
        }//接收
        private void IStartListening()
        {
            int PortNumber = 8998;
            if (PortStr.Text.ToString().Trim() != "") PortNumber = int.Parse(PortStr.Text.ToString());
            Ilistener = new TcpListener(PortNumber);
            try
            {
                Ilistener.Start();
            }
            catch (Exception e)
            {
                if (e != null)
                {
                    MessageBox.Show("无法打开端口", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    ExitBtn_Click(null, null);
                }
            }
            while (Iprocessor.IsAlive)
            {
                try
                {
                    Iclientsocket2 = Ilistener.AcceptSocket();
                    Iclientservice = new Thread(new ThreadStart(IServiceClient));
                    Iclientservice.Start();
                }
                catch (Exception e)
                {
                    if (e != null)
                    {
                        MessageBox.Show("无法建立端口服务,服务终止", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        if (Iclientservice.IsAlive) Iclientservice.Abort();
                        break;
                    }
                }
            }
        }

        private void RevStr_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
2007-12-14 16:12
白菜粉条
Rank: 1
等 级:新手上路
帖 子:76
专家分:0
注 册:2007-5-25
收藏
得分:0 
多线程!!

我不是不想不郁闷,台湾问题没有解决啊!
2007-12-15 10:55
caiyakang
Rank: 2
等 级:新手上路
威 望:5
帖 子:2111
专家分:0
注 册:2005-3-24
收藏
得分:0 
多线程+同步
或者用异步

中国人的财富网:http://www..cn/
2007-12-15 23:20
cheng7xicx_jsp
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-12-24
收藏
得分:0 
楼上 每个人连到服务器上 每个人都有一条线程在服务器上跑起来了 人多了 服务器会不会瘫了
2007-12-26 12:34
快速回复:[求助]关于Tcp通讯方面的问题
数据加载中...
 
   



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

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