C# 简单的聊天通信

服务器:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Servers
{
    public partial class Server : Form
    {
        public Server()
        {
            InitializeComponent();
            //关闭对文本框的非法线程操作检查
            TextBox.CheckForIllegalCrossThreadCalls = false;//还未懂!
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        Thread threadWatch = null;
        Socket socketWatch = null;

        private void Serverconn_Click(object sender, EventArgs e)
        {
            socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip = IPAddress.Parse(txtIP.Text.Trim());
            int port = Convert.ToInt32(txtPORT.Text.Trim());
            IPEndPoint ipe = new IPEndPoint(ip, port);
            socketWatch.Bind(ipe);
            socketWatch.Listen(10);
            threadWatch = new Thread(Watchconnecting);
            threadWatch.IsBackground = true;
            threadWatch.Start();
            txtMsg.AppendText("开始监听!" + "\r\n");
        }

        Socket socConnection = null;

        private void Watchconnecting()
        {
            while (true)
            {
                socConnection = socketWatch.Accept();
                txtMsg.AppendText("客户端连接成功!" + "\r\n");
                //创建一个通信线程
                ParameterizedThreadStart pts = new ParameterizedThreadStart(ServerRecMsg);
                Thread thr = new Thread(pts);
                thr.IsBackground = true;
                thr.Start(socConnection);
            }
        }

        private void ServerRecMsg(object obj)
        {
            Socket socketServer = obj as Socket;
            while (true)
            {
                byte[] RecvBytes = new byte[1024 * 1024];
                int length = socketServer.Receive(RecvBytes);
                string RecvStr = Encoding.UTF8.GetString(RecvBytes, 0, length);
                txtMsg.AppendText("ZXY: " + GetTime() + "\r\n" + RecvStr + "\r\n");
            }
        }

        private void SendMsg_Click(object sender, EventArgs e)
        {
            ServerSendMsg(txtSend.Text.Trim());
        }

        private void ServerSendMsg(string SendStr)
        {
            byte[] SendString = Encoding.UTF8.GetBytes(SendStr);
            socConnection.Send(SendString);
            txtMsg.AppendText("PYT: " + GetTime() + "\r\n" + SendStr + "\r\n");
        }


        private void txtSend_TextChanged(object sender, EventArgs e)
        {

        }

        //Enter快捷键,光有代码不行,还需要将KeyDown属性设置一下!
        private void txtSend_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ServerSendMsg(txtSend.Text.Trim());
            }
        }

        //获取当前的时间
        private DateTime GetTime()
        {
            DateTime nowTime = new DateTime();
            nowTime = DateTime.Now;
            return nowTime;
        }
    }
}



客户端:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Clients
{
    public partial class Client : Form
    {
        public Client()
        {
            InitializeComponent();
            TextBox.CheckForIllegalCrossThreadCalls = false;
        }

        private void Client_Load(object sender, EventArgs e)
        {

        }

        Socket socketClient = null;
        Thread threadClient = null;

        private void beginListen_Click(object sender, EventArgs e)
        {
            socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip = IPAddress.Parse(txtIP.Text.Trim());
            int port = Convert.ToInt32(txtPORT.Text.Trim());
            IPEndPoint ipe = new IPEndPoint(ip, port);
            socketClient.Connect(ipe);
            threadClient = new Thread(RecvMsg);
            threadClient.IsBackground = true;
            threadClient.Start();
        }

        private void RecvMsg()
        {
            while(true)
            {
                byte[] arrRecMsg = new byte[1024 * 1024];
                int length = socketClient.Receive(arrRecMsg);
                string strRecMsg = Encoding.UTF8.GetString(arrRecMsg, 0, length);
                txtMsg.AppendText("PYT: " + GetTime() + "\r\n" + strRecMsg + "\r\n");
            }
        }

        private void ClientSendMsg(string sendMsg)
        {
            byte[] arrClientSendMsg = Encoding.UTF8.GetBytes(sendMsg);
            socketClient.Send(arrClientSendMsg);
            txtMsg.AppendText("ZXY: " + GetTime() + "\r\n" + sendMsg + "\r\n");
        }

        private void Send_Click(object sender, EventArgs e)
        {
            ClientSendMsg(txtCMsg.Text.Trim());
        }

        private void txtCMsg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ClientSendMsg(txtCMsg.Text.Trim());
            }
        }


        private DateTime GetTime()
        {
            DateTime nowTime = new DateTime();
            nowTime = DateTime.Now;
            return nowTime;
        }

        

    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值