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 += "\r";

                string history = "";

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

                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();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM