WPF使用socket实现简单聊天软件

公司网络限制不能传文件,先贴部分代码

控件添加到界面就行,界面随意布局

项目结构:

1.解决方案

    1.1. Client

    1.2. Server 

Client:

<Window x:Class="CSharpSocketExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="375" Width="524">
    <Grid Margin="0,0,2,-20">
        <Button Name="btn_connection" Content="发送" HorizontalAlignment="Left" Margin="292,288,0,0" VerticalAlignment="Top" Width="75" Click="btn_connection_Click"/>
        <TextBlock Name="tblock_message" HorizontalAlignment="Left" Margin="34,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="208" Width="464"/>
        <Button Name="btn_refresh" Content="刷新" HorizontalAlignment="Left" Margin="292,244,0,0" VerticalAlignment="Top" Width="75" Click="btn_refresh_Click"/>
        <Label Content="昵称" HorizontalAlignment="Left" Margin="34,244,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="tb_username"  HorizontalAlignment="Left" Height="23" Margin="92,247,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="136"/>
        <Label Content="消息" HorizontalAlignment="Left" Margin="34,284,0,0" VerticalAlignment="Top"/>
        <Button Name="btn_clear" Content="清屏" HorizontalAlignment="Left" Margin="401,244,0,0" VerticalAlignment="Top" Width="75" Click="btn_clear_Click"/>
private void btn_connection_Click(object sender, RoutedEventArgs e)
        {
            int port = 6000;
            string host = "127.0.0.1";
            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, port);
            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            clientSocket.Connect(ipe);
//send message
            string sendStr =tb_username.Text + ": " + tb_message.Text;
            byte[] sendBytes = Encoding.UTF8.GetBytes(sendStr);
            clientSocket.Send(sendBytes);
            //receive message
            string recStr = "";
            byte[] recBytes = new byte[4096];
            int bytes = clientSocket.Receive(recBytes, recBytes.Length, 0);
            recStr += Encoding.UTF8.GetString(recBytes, 0, bytes);
            tblock_message.Text = recStr;clientSocket.Close();
        }
        private void btn_refresh_Click(object sender, RoutedEventArgs e)
        {
            int port = 6000;
            string host = "127.0.0.1"; IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, port);
            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            clientSocket.Connect(ipe);
            //send message
            string sendStr = "refresh";
            byte[] sendBytes = Encoding.UTF8.GetBytes(sendStr);
            clientSocket.Send(sendBytes);//receive message
            string recStr = "";
            byte[] recBytes = new byte[4096];
            int bytes = clientSocket.Receive(recBytes, recBytes.Length, 0);
            recStr += Encoding.UTF8.GetString(recBytes, 0, bytes);
            tblock_message.Text = recStr;
            clientSocket.Close();
        }

Server:

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            int port = 6000;
            string host = "127.0.0.1";

            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, port);

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Bind(ipe);
            socket.Listen(0);
            Console.WriteLine("Listen Server Open.");

            string history = "";

            while (true)
            {
                //receive message
                Socket serverSocket = socket.Accept();
                Console.WriteLine("Connection Successful.");
                history += "
";

                string history = "";

            while (true)
            {
                //receive message
                Socket serverSocket = socket.Accept();
                Console.WriteLine("Connection Successful.");
                history += "
";

                string recStr = "";
                byte[] recByte = new byte[4096];
                int bytes = serverSocket.Receive(recByte, recByte.Length, 0);
                recStr += Encoding.UTF8.GetString(recByte, 0, bytes);

                if (recStr != "refresh")
                {
                    history += recStr;
                }

                //send message
                Console.WriteLine("Receive Message:{0}", recStr);
                
                string sendStr = history.ToString();
                byte[] sendByte = Encoding.UTF8.GetBytes(sendStr);
                        
                serverSocket.Send(sendByte, sendByte.Length, 0);
                serverSocket.Close();
                //socket.Close();
原文地址:https://www.cnblogs.com/bincoding/p/7387855.html