基本信息
源码名称:C# TCPSERVER 发送接收数据 示例
源码大小:0.06M
文件格式:.zip
开发语言:C#
更新时间:2017-05-19
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍



private void btnStart_Click(object sender, EventArgs e)//设置目标IP(Client),本地IP(Server)
        {
            try
            {
                if (protocol == "UDP Server")
                {
                    aimSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    RecUdpClient = new UdpClient(new IPEndPoint(ip, Convert.ToInt32(txtPort.Text)));
                    MessageBox.Show("UDP Server创建成功");
                    Thread thReceive = new Thread(USReceive);
                    thReceive.IsBackground = true;
                    thReceive.Start();
                }
                else if (protocol == "UDP Client")//UPD客户端,目标IP和端口  调用Socket.SendTo
                {
                    aimSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//建立通信的Socket
                    ip = IPAddress.Parse(txtIP.Text);
                    point = new IPEndPoint(ip, Convert.ToInt32(txtPort.Text));
                    MessageBox.Show("UDP Client设置成功");
                    Thread thReceive = new Thread(UCReceive);
                    thReceive.IsBackground = true;
                    thReceive.Start();
                }
                else if (protocol == "TCP Server")
                {
                    mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    point = new IPEndPoint(ip, Convert.ToInt32(txtPort.Text));//绑定端口号
                    mySocket.Bind(point);//绑定监听端口
                    MessageBox.Show("TCP Server绑定成功");
                    mySocket.Listen(10);//等待连接是一个阻塞过程,创建线程来监听
                    Thread thReceive = new Thread(TSReceive);
                    thReceive.IsBackground = true;
                    thReceive.Start();
                }
                else if (protocol == "TCP Client")
                {
                    ip = IPAddress.Parse(txtIP.Text);//目标IP
                    point = new IPEndPoint(ip, Convert.ToInt32(txtPort.Text));//目标端口
                    aimSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    aimSocket.Connect(point);//连接服务器
                    MessageBox.Show("连接成功");
                    Thread thReceive = new Thread(TCReceive);
                    thReceive.IsBackground = true;
                    thReceive.Start();
                }

            }
            catch
            {}
        }