网络聊天工具Socket编程心得
首先要了解TCP协议通信的流程: 1。服务器端首先创建服务器套接字
2。服务器套接字监听一个端口,等待客户端的请求 3。客户端创建一个客户端套接字 4。客户端向服务器发送请求 5。服务器确认与客户端的连接
6。客户端和服务器利用建立的连接进行通信 7。通信完毕后,客户端和服务器关闭各自的连接
Socket编程基础:
一。利用Socket建立服务器程序
1。创建一个服务器套接字,用IP地址和端口初始化服务器
IPAddress ipAddress = IPAddress.Parse(\
TcpListener listener = new TcpListener(ipAddress, 1234);
2。监听服务器端口
listener.Start();
3。确认与客户端的连接
Socket socket = listener.AcceptSocket();
4。取得客户端传送过来的信息
//将传送过来的信息存入字节数组中 byte[] buffer = new byte[1024]; socket.Receive(buffer);
5。处理客户端的请求并回应客户端
string message = \
byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(message.ToCharArray()); socket.Send(outbytes, message.Length, 0);
6。断开客户端的连接,释放客户端连接
socket.Close();
7。关闭服务器,释放服务器连接
listener.Close();
二。利用Socket建立客户端程序 1。创建客户端套接字
TcpClient tcpClient = new TcpClient();
2。连接服务器
tcpClient.Connect(IPAddress.Parse(\
3。得到与服务器通信的流通道
NetworkStream stream = tcpClient.GetStream();
4。向服务器发送数据
string cmd = \
byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(cmd.ToCharArray()); stream.Write(outbytes, 0, outbytes.Length);
5。接收从服务器发回的数据
byte[] buffer = new byte[1024];
int len = stream.Read(buffer, 0, buffer.Length);
string msg = System.Text.Encoding.ASCII.GetString(buffer, 0, len);
6。断开连接
tcpClient.Close();
服务器端窗体ChatServer.cs: 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.Threading;
namespace ChatServer
{
///
/// Form1 的摘要说明。 ///
public class ChatServerForm : System.Windows.Forms.Form {
///
/// 必需的设计器变量。 ///
private System.ComponentModel.Container components = null; // The port
static int port = 1234;
private TcpListener listener; private Socket tmpSocket;
// The maximal clients the server can hold static int MaxNum = 100;
private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox txtHost; private System.Windows.Forms.TextBox txtPort; private System.Windows.Forms.Button btnStart; private System.Windows.Forms.Button btnExit; private System.Windows.Forms.Label label3;
private System.Windows.Forms.ComboBox cmbCurUserList; private System.Windows.Forms.ListBox lstInfo; // The array clients is to save the online clients static ArrayList clients = new ArrayList();
public ChatServerForm() {
//
// Windows 窗体设计器支持所必需的 //
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // }
///
/// 清理所有正在使用的资源。 ///
protected override void Dispose( bool disposing )
{
if( disposing ) {
if (components != null) {
components.Dispose(); } }
base.Dispose( disposing ); }
#region Windows 窗体设计器生成的代码 ///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 ///
private void InitializeComponent() {
this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.txtHost = new System.Windows.Forms.TextBox(); this.txtPort = new System.Windows.Forms.TextBox(); this.btnStart = new System.Windows.Forms.Button(); this.btnExit = new System.Windows.Forms.Button(); this.label3 = new System.Windows.Forms.Label();
this.cmbCurUserList = new System.Windows.Forms.ComboBox(); this.lstInfo = new System.Windows.Forms.ListBox(); this.SuspendLayout(); //
// label1 //
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(32, 32); this.label1.Name = \
this.label1.Size = new System.Drawing.Size(54, 17); this.label1.TabIndex = 0; this.label1.Text = \主机号:\ //
// label2 //
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(32, 72); this.label2.Name = \
this.label2.Size = new System.Drawing.Size(54, 17);
this.label2.TabIndex = 1; this.label2.Text = \端口号:\ //
// txtHost //
this.txtHost.Location = new System.Drawing.Point(96, 24); this.txtHost.Name = \
this.txtHost.Size = new System.Drawing.Size(128, 21); this.txtHost.TabIndex = 2; this.txtHost.Text = \ //
// txtPort //
this.txtPort.Location = new System.Drawing.Point(96, 64); this.txtPort.Name = \
this.txtPort.Size = new System.Drawing.Size(128, 21); this.txtPort.TabIndex = 3; this.txtPort.Text = \ //
// btnStart //
this.btnStart.Location = new System.Drawing.Point(256, 24); this.btnStart.Name = \ this.btnStart.TabIndex = 4; this.btnStart.Text = \启动\ this.btnStart.Click += new System.EventHandler(this.btnStart_Click); //
// btnExit //
this.btnExit.Location = new System.Drawing.Point(256, 64); this.btnExit.Name = \ this.btnExit.TabIndex = 5; this.btnExit.Text = \退出\
this.btnExit.Click += new System.EventHandler(this.btnExit_Click); //
// label3 //
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(24, 112); this.label3.Name = \
this.label3.Size = new System.Drawing.Size(91, 17); this.label3.TabIndex = 6;
this.label3.Text = \当前在线用户:\
网络聊天工具Socket编程心得



