聊天室可以相互交流

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

namespace 聊天室服务端2
{
    class Program
    {
        List<Socket> socketList = new List<Socket>();
        static void Main(string[] args)
        {
            Socket mainServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            int pose = 3001;
            IPAddress ip = IPAddress.Parse("192.168.1.104");
            EndPoint point = new IPEndPoint(ip, pose);
            mainServer.Bind(point);
            mainServer.Listen(10);
            Console.WriteLine("服务器启动成功,等待连接...");

            Socket ClientVIP = mainServer.Accept();
            Console.WriteLine("连接成功");
            IPEndPoint ClientVIPip = (IPEndPoint)ClientVIP.RemoteEndPoint;
            Console.WriteLine("客户端地址{0},端口{1}", ClientVIPip.Address, ClientVIPip.Port);

            ReceiveAndSend RS = new ReceiveAndSend(ClientVIP);
            RS.Se();
            RS.Re();


            Console.ReadKey();
        }       
    }
    class ReceiveAndSend
    {
        private Socket socketObj;
        private Thread tse,tre;
        public ReceiveAndSend(Socket s)
        {
            socketObj = s;
            tse = new Thread(Se);
            tse.Start();
            tre = new Thread(Re);
            tre.Start();

        }
        public void Se()
        {
            while (true)
            {
                string sendStr = "服务器:"+Console.ReadLine();
                byte[] sendData = Encoding.Default.GetBytes(sendStr);
                socketObj.Send(sendData);//发送
            }
        }
        public void Re()
        {
            while (true)
            {
                byte[] recData = new byte[1024];
                int length = socketObj.Receive(recData);//读取
                string recStr = Encoding.Default.GetString(recData, 0, length);
                Console.WriteLine(recStr);
            }
        }
    }

}


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

namespace 聊天室客户端2
{
    class Program
    {
        
        static void Main(string[] args)
        {
            Socket mainClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            int pose = 3001;
            IPAddress ip = IPAddress.Parse("192.168.1.104");
            EndPoint point = new IPEndPoint(ip, pose);
            mainClient.Connect(point);

            ReceiveAndSend RS = new ReceiveAndSend(mainClient);
            RS.Re();
            RS.Se();

            Console.ReadKey();
        }
    }
    class ReceiveAndSend
    {
        private Socket socketObj;
        private Thread tse, tre;
        public ReceiveAndSend(Socket s)
        {
            socketObj = s;
            tse = new Thread(Se);
            tse.Start();
            tre = new Thread(Re);
            tre.Start();
        }
        public void Se()
        {
            while (true)
            {
                string sendStr = "客户端:" + Console.ReadLine();
                byte[] sendData = Encoding.Default.GetBytes(sendStr);
                socketObj.Send(sendData);//发送
            }
        }
        public void Re()
        {
            while (true)
            {
                byte[] recData = new byte[1024];
                int length = socketObj.Receive(recData);//读取
                string recStr = Encoding.Default.GetString(recData, 0, length);
                Console.WriteLine(recStr);
            }
        }
    }
}        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值