公司网络限制不能传文件,先贴部分代码

控件添加到界面就行,界面随意布局

项目结构:

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实现简单聊天软件的更多相关文章

  1. Java实现 简单聊天软件

    简单的聊天软件 //客户端 package yjd9; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; ...

  2. C#基于Socket的简单聊天室实践

    序:实现一个基于Socket的简易的聊天室,实现的思路如下: 程序的结构:多个客户端+一个服务端,客户端都是向服务端发送消息,然后服务端转发给所有的客户端,这样形成一个简单的聊天室功能. 实现的细节: ...

  3. 关于Socket编写简单聊天工具的总结(原创)

    这段时间再看socket编程,虽然现在是刚刚接触,但是还是忍不住想写一篇总结,来激励自己努力学习,写的不好的地方,还请大家指教啊! 下面针对一个简单的发送消息和文件的程序说说吧.   首先是服务器需要 ...

  4. Socket实现简单聊天

    服务端: package main.java.com.socket_dome; import java.io.IOException; import java.io.InputStream; impo ...

  5. python练习四—简单的聊天软件

    python最强大的是什么?库支持!!有了强大的库支持,一个简单的聊天软件实现就更简单了,本项目思路如下 # 项目思路 1. 服务器的工作 * 初始化服务器 * 新建一个聊天房间 * 维护一个已链接用 ...

  6. Socket实现简单的聊天通信

    最近学习了Socket后,感觉Socket挺好玩的,在博客中看到socket在实时聊天功能的很强大,于是乎就做了一个简单的聊天功能,今天贴出来,能够与大家一起共享,有不对之处,能够给予指出,谢谢! 服 ...

  7. Python Socket 简单聊天室2

    上篇文章写了一个简单的单线程的一问一答的简单聊天室.这次我们使用SocketServer模块搭建一个多线程异步的聊天室. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  8. java Socket实现简单在线聊天(二)

    接<java Socket实现简单在线聊天(一)>,在单客户端连接的基础上,这里第二步需要实现多客户端的连接,也就需要使用到线程.每当有一个新的客户端连接上来,服务端便需要新启动一个线程进 ...

  9. java Socket实现简单在线聊天(一)

    最近的项目有一个在线网页交流的需求,由于很久以前做过的demo已经忘记的差不多了,因此便重新学习一下. 我计划的大致实现步骤分这样几大步: 1.使用awt组件和socket实现简单的单客户端向服务端持 ...

随机推荐

  1. ARGB和PARGB

    原文链接: http://blog.csdn.net/lnwaycool/article/details/8610313 ARGB和PARGB是针对32位图像而言的,Windows下图像可以是1位.4 ...

  2. Fiddler高级用法-设置断点

    我们知道Fiddler是位于客户端和服务器之间的代理,它能够记录客户端和服务器之间的所有 HTTP请求,可以针对特定的HTTP请求,分析请求数据.设置断点.调试web应用.修改请求的数据,甚至可以修改 ...

  3. 文件处理-智能检测编码的工具(chardet)

    一.chardet使用方法 问:假如你不知道你要处理的文件是什么编码可怎么办呢? import chardet f = open('通讯录.txt',mode='rb') data = f.read( ...

  4. Eclipse安装PlantUML插件

    新技术的诞生和更新,新工具的发现和使用是两件让人开心的事情. 还记得Visio下苦苦的画流程图的时光吗,现在一切都变得so easy,因为有PlantUML! 官网:http://plantuml.c ...

  5. Java 8 Stream – Read a file line by line

    In Java 8, you can use Files.lines to read file as Stream. c://lines.txt – A simple text file for te ...

  6. springboot 多环境配置yml或properties

    https://www.cnblogs.com/mr-yang-localhost/p/8971327.html   springboot 多环境配置 https://blog.csdn.net/li ...

  7. 好的 IOS 学习网站

    http://www.objc.io/contributors.html codeproject. http://www.codeproject.com/KB/iPhone/

  8. IOS 视频流

    https://github.com/kolyvan/kxmovie   demo 项目

  9. python代码制作configure文件

    在lua中,一直用lua作为config文件,或承载数据的文件 - 好处是lua本身就很好阅读,然后无需额外写解析的代码,还支持在configure文件中读环境变量,条件判断等. 在lua中通过loa ...

  10. Atitit 找人软福利建设 技术团队建设大概流程

    Atitit 找人软福利建设 技术团队建设大概流程 火车公司有免费车座,餐馆有免费饭吃.. 软件公司嘛,就是软件资源,知识了...技术... 培训体系 大概的知识库体系..让他知道来到我们团队有着很高 ...