WPF使用socket實現簡單聊天軟體

來源:互聯網
上載者:User

標籤:coding   win   refresh   soc   nec   app   out   cep   ogr   

公司網路限制不能傳檔案,先貼部分代碼

項目結構:

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

 

WPF使用socket實現簡單聊天軟體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.