码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2017-08-18 11:17:12      阅读:372      评论:0      收藏:0      [点我收藏+]

标签: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实现简单聊天软件

标签:coding   win   refresh   soc   nec   app   out   cep   ogr   

原文地址:http://www.cnblogs.com/bincoding/p/7387855.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!