实现服务器端与客户端对话

服务器端软件:
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();
            }

        }

    }

}

posted on 2007-10-09 15:13  Gofficer  阅读(682)  评论(3)    收藏  举报