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 = ;
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[];
int bytes = clientSocket.Receive(recBytes, recBytes.Length, );
recStr += Encoding.UTF8.GetString(recBytes, , bytes);
tblock_message.Text = recStr;clientSocket.Close();
}
private void btn_refresh_Click(object sender, RoutedEventArgs e)
{
int port = ;
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[];
int bytes = clientSocket.Receive(recBytes, recBytes.Length, );
recStr += Encoding.UTF8.GetString(recBytes, , bytes);
tblock_message.Text = recStr;
clientSocket.Close();
}
Server:
namespace Server
{
class Program
{
static void Main(string[] args)
{
int port = ;
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();
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[];
int bytes = serverSocket.Receive(recByte, recByte.Length, );
recStr += Encoding.UTF8.GetString(recByte, , 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, );
serverSocket.Close();
//socket.Close();
WPF使用socket实现简单聊天软件的更多相关文章
- Java实现 简单聊天软件
简单的聊天软件 //客户端 package yjd9; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; ...
- C#基于Socket的简单聊天室实践
序:实现一个基于Socket的简易的聊天室,实现的思路如下: 程序的结构:多个客户端+一个服务端,客户端都是向服务端发送消息,然后服务端转发给所有的客户端,这样形成一个简单的聊天室功能. 实现的细节: ...
- 关于Socket编写简单聊天工具的总结(原创)
这段时间再看socket编程,虽然现在是刚刚接触,但是还是忍不住想写一篇总结,来激励自己努力学习,写的不好的地方,还请大家指教啊! 下面针对一个简单的发送消息和文件的程序说说吧. 首先是服务器需要 ...
- Socket实现简单聊天
服务端: package main.java.com.socket_dome; import java.io.IOException; import java.io.InputStream; impo ...
- python练习四—简单的聊天软件
python最强大的是什么?库支持!!有了强大的库支持,一个简单的聊天软件实现就更简单了,本项目思路如下 # 项目思路 1. 服务器的工作 * 初始化服务器 * 新建一个聊天房间 * 维护一个已链接用 ...
- Socket实现简单的聊天通信
最近学习了Socket后,感觉Socket挺好玩的,在博客中看到socket在实时聊天功能的很强大,于是乎就做了一个简单的聊天功能,今天贴出来,能够与大家一起共享,有不对之处,能够给予指出,谢谢! 服 ...
- Python Socket 简单聊天室2
上篇文章写了一个简单的单线程的一问一答的简单聊天室.这次我们使用SocketServer模块搭建一个多线程异步的聊天室. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- java Socket实现简单在线聊天(二)
接<java Socket实现简单在线聊天(一)>,在单客户端连接的基础上,这里第二步需要实现多客户端的连接,也就需要使用到线程.每当有一个新的客户端连接上来,服务端便需要新启动一个线程进 ...
- java Socket实现简单在线聊天(一)
最近的项目有一个在线网页交流的需求,由于很久以前做过的demo已经忘记的差不多了,因此便重新学习一下. 我计划的大致实现步骤分这样几大步: 1.使用awt组件和socket实现简单的单客户端向服务端持 ...
随机推荐
- 实现一个简单的android开关
近期在学习android中的graphics中绘图系列.依照大神思路.找葫芦画瓢实现了一个开关.如图下: 记录一下实现方式: 1.画背景 上图形状.分成两个半圆与一个矩形,那么代码能够写成: priv ...
- 最烂编程语言得主:javascript
C++在我脑中一直是一门缺乏设计和远见的语言,其设计者也是缺少主见的人(我承认我对c++有一定偏见),在我看来,C++从一开始就是堆叠语言特性,成为最流行的语言,,只是这个时代将它推到了最前列,我心中 ...
- hibernate显示完整的sql(转)
不完整的SQL Hibernate: /* insert com.test.bean.User */ insert into user (username, password, email, sex) ...
- mysql 8.0 java连接报错:Unknown system variable 'query_cache_size'
java连接mysql 8.0.11报错 java.sql.SQLException: Unknown system variable 'query_cache_size' at com.mysql. ...
- django http404 详解
[引子] 今天在看django的官方文档的时候看到get_object_or_404这个函数感觉比较奇怪.这个主要来自于它的功能,如果要查询的对象 存在那么就返回对象:如果对象不存在那么就要报404 ...
- laravel路由不生效,404,除了/ 都不行,关于nginx环境下laravel除了默认路由都出现404报错的处理方法
其实出现这个问题只会出现在laravel被部署在二级目录中,其原因是,除了请求根目录/ (http://www.xxx.com/public/),会请求public/index.php 你在浏览器输入 ...
- talend hive数据导入到mysql中
thiveInput->tmap->tMysqloutput thiveInput: tmap: tmysqlOutput:注意编码问题:noDatetimeStringSync=true ...
- Android SDK不能够更新
Adroid不能够更新,因为国内将google的服务器墙掉了,在 1) vim /etc/hosts(Windows上路径为:C:\Windows\System32\drivers\etc\hosts ...
- IOS6 IOS7 Mapkit draw Rout(地图划线)
IOS7 比较简单 CLLocationCoordinate2D _start2D; CLLocationCoordinate2D _end2D; NSArray *_routes; IOS6 ...
- char型指针和字符串字面量和字符数组
1.当一个char型指针指向一个字符串字面量(也就是常量字符串)时,该指针必须由const修饰,否则,系统会给出deprecated(不赞成)的警告.原因是:字符串字面量不可改变,当它被一个非cons ...