惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
Intezer
C
Cyber Attacks, Cyber Crime and Cyber Security
The Register - Security
The Register - Security
量子位
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
MyScale Blog
MyScale Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
Jina AI
Jina AI
博客园 - 【当耐特】
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
SecWiki News
SecWiki News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
宝玉的分享
宝玉的分享
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
有赞技术团队
有赞技术团队
T
Tor Project blog
H
Hacker News: Front Page
A
Arctic Wolf
NISL@THU
NISL@THU
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
V
V2EX
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
I
InfoQ
D
Docker
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42

博客园 - Gofficer

决战紫禁之巅 为学 C#网页自动登录和提交POST信息的多种方法 一个C#写的调用外部进程类 快速实现在Windows应用程序中支持拖拽的TreeView控件(C#) PPT转图片 开发人员,敢问路在何方? C# 实现注销、关机、重启电脑功能 ultraGrid 控件中,实现单元格内容换行显示 如何用一条sql取得第10到第20条的记录? 用Sandcastle一键生成CHM帮助文档 C#中访问WEB页面 使用代理服务器 自定义Ping方法 HTTP请求和应答 Socket套接字实现服务器端连接 Socket套接字实现客户端连接 启动和停止本地系统进程 异常处理
实现服务器端与客户端对话
Gofficer · 2007-10-09 · via 博客园 - Gofficer

服务器端软件:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
// 定义新的命名空间。
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Threading;


namespace TalkServer
{
    
/// <summary>
    
/// 服务器端软件。
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.GroupBox groupBox1;
        
private System.Windows.Forms.Panel panel1;
        
private System.Windows.Forms.Button button1;
        
private System.Windows.Forms.RichTextBox richTextBox1;
        
private System.Windows.Forms.RichTextBox richTextBox2;
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;

        
public Form1()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();

            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null)
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows Form Designer generated code

        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main()
        
{
            Application.Run(
new Form1());
        }

        
// 定义私有变量。
        public bool m_bConnected = false;
        
public IPEndPoint p = null;
        
public Socket s = null;
        
public NetworkStream ns = null;
        
public TextReader r = null;
        
public TextWriter w = null;
        
public Thread t = null;
        
public void ThreadListen()
        
{
            Socket recv 
= null;
            recv 
= s.Accept();
            
if(recv != null)
                m_bConnected 
= true;
            ns 
= new NetworkStream(recv);
            r 
= new StreamReader(ns);
            w 
= new StreamWriter(ns);
            
while(m_bConnected)
            
{
                
string tmp;
                
try
                
{
                    tmp 
= r.ReadLine();
                    
if(tmp.Length != 0)
                    
{
                        
lock(this)
                        
{
                            richTextBox1.Text 
= "他说:"+tmp+"\n"+richTextBox1.Text;
                        }

                    }

                }

                
catch
                
{
                    MessageBox.Show(
"连接断开!!");
                }

            }

            recv.Shutdown(SocketShutdown.Both);
            recv.Close();
            s.Shutdown(SocketShutdown.Both);
            s.Close();
        }

        
private void button1_Click(object sender, System.EventArgs e)
        
{
            p 
= new IPEndPoint(IPAddress.Any, 34567);
            s 
= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            s.Blocking 
= true;
            s.Bind(p);
            s.Listen(
0);
            t 
= new Thread(new ThreadStart(this.ThreadListen));
            t.Start();
            button1.Enabled 
= false;
        }

        
private void richTextBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        
{
            
byte[] data;
            
if(e.KeyChar == (char)13)
            
{
                
if(m_bConnected)
                
{
                    
try
                    
{
                        
lock(this)
                        
{
                            richTextBox1.Text 
= "我说:"+richTextBox2.Text+richTextBox1.Text;
                            w.WriteLine(richTextBox2.Text);
                            w.Flush();
                        }

                    }

                    
catch
                    
{
                        MessageBox.Show(
"连接断开!!");
                    }

                }

                
else
                
{
                    MessageBox.Show(
"对不起,还没有与对方连接,不能通信!");
                }

                richTextBox2.Text 
= "";
                richTextBox2.Focus();
            }

        }

    }

}

客户端软件:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
// 添加新的命名空间。
using System.IO;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace TalkClient
{
    
/// <summary>
    
/// 客户端软件。
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.GroupBox groupBox1;
        
private System.Windows.Forms.Panel panel1;
        
private System.Windows.Forms.TextBox textBox1;
        
private System.Windows.Forms.TextBox textBox2;
        
private System.Windows.Forms.Label label1;
        
private System.Windows.Forms.Label label2;
        
private System.Windows.Forms.Button button1;
        
private System.Windows.Forms.RichTextBox richTextBox1;
        
private System.Windows.Forms.RichTextBox richTextBox2;
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;

        
public Form1()
        
{
            
// Windows 窗体设计器支持所必需的
            InitializeComponent();
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
        }

        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null)
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows Form Designer generated code

        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main()
        
{
            Application.Run(
new Form1());
        }

        
// 定义私有变量。
        public bool m_bConnected = false;
        
public Thread t = null;
        
public IPEndPoint p = null;
        
public Socket s = null;
        
public NetworkStream ns = null;
        
public TextReader r = null;
        
public TextWriter w = null;
        
public void ThreadListen()
        
{
            
string tmp;
            
while(m_bConnected)
            
{
                
try
                
{
                    tmp 
= r.ReadLine();
                    
if(tmp.Length != 0)
                    
{
                        
lock(this)
                        
{
                            richTextBox1.Text 
= "他说:"+tmp+"\n"+richTextBox1.Text;
                        }

                    }

                }

                
catch
                
{
                    MessageBox.Show(
"连接断开!!");
                }

            }

            s.Shutdown(SocketShutdown.Both);
            s.Close();
        }

        
private void button1_Click(object sender, System.EventArgs e)
        
{
            p 
= new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text));
            s 
= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            s.Connect(p);
            
if(s.Connected)
            
{
                ns 
= new NetworkStream(s);
                r 
= new StreamReader(ns);
                w 
= new StreamWriter(ns);
                t 
= new Thread(new ThreadStart(this.ThreadListen));
                t.Start();
                m_bConnected 
= true;
                button1.Enabled 
= false;
            }

        }

        
private void richTextBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        
{
            
if(e.KeyChar == (char)13)
            
{
                
if(m_bConnected)
                
{
                    
lock(this)
                    
{
                        
try
                        
{
                            richTextBox1.Text 
= "我说:"+richTextBox2.Text+richTextBox1.Text;
                            w.WriteLine(richTextBox2.Text);
                            w.Flush();
                        }

                        
catch
                        
{
                            MessageBox.Show(
"连接断开!!");
                        }

                    }

                }

                
else
                
{
                    MessageBox.Show(
"对不起,还没有与对方连接,不能通信!");
                }

                richTextBox2.Text 
= "";
                richTextBox2.Focus();
            }

        }

    }

}