C# TCP实现多个客户端与服务端 数据 传输

实现一个客户端和一个服务端通信
可以同时收发多条信息
使用C#语言,通过socket进行通信,基于TCP协议
服务端代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace testServer
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建服务端
            EndPoint point = new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6000);
            server.Bind(point);//绑定端口号和IP
            server.Listen(10);//开启监听
            Console.WriteLine("服务器开启。。。。");
            Socket client = server.Accept();//保存连接进来的客户端
            client.Send(Encoding.UTF8.GetBytes("你已经进入服务"));//给客户端发送消息,提示客户端连接成功


            Thread t1 = new Thread(reciveMsg);
            t1.Start(client);//开启线程接收消息

            Thread t2 = new Thread(sendMsg);
            t2.Start(client);//开启线程发送消息

        }

        static void reciveMsg(object obj)
        {
            Socket client = obj as Socket;
            while (true)
            {
                byte[] buffer = new byte[1024];
                try
                {
                    int length = client.Receive(buffer);
                    string res = Encoding.UTF8.GetString(buffer, 0, length);
                    Console.WriteLine("接收到的消息:" + res);
                }
                catch (Exception e)
                {
                    IPEndPoint point = client.RemoteEndPoint as IPEndPoint;
                    Console.WriteLine(point.ToString()+"断开连接。。。");
                    break;
                }

            }

        }

        static void sendMsg(object obj)
        {
            Socket client = obj as Socket;
            while (true)
            {
                string s = Console.ReadLine();
                client.Send(Encoding.UTF8.GetBytes(s));
            }
        }
    }
}

服务端代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace testClient
{
    class Program
    {
       static Socket client;
       static Thread t1;
       static Thread t2;
       static bool isLife = false;//标记是否连接到服务器
        static void Main(string[] args)
        {
             client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建客户端
            client.Connect(new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6000));//将客户端连接到服务器


            isLife = true;
             t1 = new Thread(reciveMsg);
            t1.Start();//开启线程接收消息

             t2 = new Thread(sendMsg);
            t2.Start();//开启线程发送消息

            //Console.ReadKey();
        }

        static void reciveMsg()
        {
            while (isLife)
            {
                byte[] buffer = new byte[1024];
                try{
                    int length = client.Receive(buffer);
                    string res = Encoding.UTF8.GetString(buffer, 0, length);
                    Console.WriteLine("接收到的消息:" + res);
                }catch(Exception e){
                    Console.WriteLine("reciveMsg:  End");
                    break;
                }

            }

        }

        static void sendMsg() {
            while (isLife)
            {
                string s = Console.ReadLine();
                try {
                    client.Send(Encoding.UTF8.GetBytes(s));
                }catch (Exception e){
                    Console.WriteLine("sendMsg:  End");
                    break;
                }

                if(s.Equals("886")){
                    isLife = false;
                    client.Close();
                }
            }
        }
    }
}

效果图:
这里写图片描述
资源下载:
http://download.csdn.net/detail/u011484013/9487030

  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天涯过客TYGK

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值