WPF使用socket实现简单聊天软件
Posted 致林
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
以上是关于WPF使用socket实现简单聊天软件的主要内容,如果未能解决你的问题,请参考以下文章
Java系列讲座二:Socket网络通信实现聊天软件项目讲解(UDP)
Java系列讲座二:Socket网络通信实现聊天软件项目讲解(UDP)
游戏开发实战Unity使用Socket通信实现简单的多人聊天室(万字详解 | 网络 | TCP | 通信 | Mirror | Networking)