Socket 编程示例(二)
利用晚上这点闲暇时间,写了一个Socket通信的小实例,该实例包含服务器端和客户端。其基本工作流程是:当服务器启动服务以后,客户端进行连接,如果连接成功,则用户可以在发送消息框中输入待发送的消息,然后点击“Send”按钮后向服务器发送消息,服务器在收到消息后立即向该客户端发送接收成功消息。其基本效果如图1.1和图1.2所示(注:下面两幅图于次日补上):
图1.1 服务器运行效果图
图1.2 客户端运行效果图
现将服务器和客户端部分代码贴出来,由于代码较简单,基本没有注释,并在此特别说明,该实例仅仅是一个简单的示例,其中的代码仍有进一步修改优化空间,同时亦欢迎各位朋友批评指正。
服务器代码部分:
using System;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SocketServer
{
public partial class frmServer : Form
{
private Thread m_serverThread;
private Socket m_serverSocket;
private string m_serverIP;
private int m_serverPort;
public delegate void ReceiveMessageDelegate(Client client);
ReceiveMessageDelegate receiveMessageDelegate;
public frmServer()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void frmServer_Load(object sender, EventArgs e)
{
string[] addresses = GetLocalAddresses();
cmbIP.Items.Clear();
if (addresses.Length > 0)
{
for (int i = 0; i < addresses.Length; i++)
{
cmbIP.Items.Add(addresses[i]);
}
cmbIP.Text = (string)cmbIP.Items[0];
}
txtPort.Text = "8899";
}
private void btnStart_Click(object sender, EventArgs e)
{
m_serverIP = cmbIP.Text;
m_serverPort = Int32.Parse(txtPort.Text);
Start();
btnStart.Enabled = false;
btnStop.Enabled = true;
}
private void btnStop_Click(object sender, EventArgs e)
{
Stop();
btnStart.Enabled = true;
btnStop.Enabled = false;
}
/// <summary>
/// 开始服务
/// </summary>
private void Start()
{
try
{
m_serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse(m_serverIP), m_serverPort);
m_serverSocket.Bind(localEndPoint);
m_serverSocket.Listen(10);
m_serverThread = new Thread(new ThreadStart(ReceiveAccept));
m_serverThread.Start();
this.AddRunningInfo(">> " + DateTime.Now.ToString() + " Server started.");
}
catch (SocketException se)
{
throw new Exception(se.Message);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 停止服务
/// </summary>
private void Stop()
{
try
{
m_serverThread.Abort(); // 线程终止
m_serverSocket.Close(); // Socket Close
this.AddRunningInfo(">> " + DateTime.Now.ToString() + " Server stoped.");
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private void ReceiveAccept()
{
while (true)
{
Client client = new Client();
try
{
client.ClientSocket = m_serverSocket.Accept();
this.AddRunningInfo(">> " + DateTime.Now.ToString() + " Client[" + client.ClientSocket.RemoteEndPoint.ToString() + "] connected.");
receiveMessageDelegate = new ReceiveMessageDelegate(ReceiveMessages);
receiveMessageDelegate.BeginInvoke(client, ReceiveMessagesCallback, "");
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
private void ReceiveMessages(Client client)
{
while (true)
{
byte[] receiveBuffer = new byte[1024];
client.ClientSocket.Receive(receiveBuffer);
string strReceiveData = Encoding.Unicode.GetString(receiveBuffer);
if (!string.IsNullOrEmpty(strReceiveData))
{
this.AddRunningInfo(">> Receive data from [" + client.ClientSocket.RemoteEndPoint.ToString()+ "]:" + strReceiveData);
string strSendData = "OK. The content is:" + strReceiveData;
int sendBufferSize = Encoding.Unicode.GetByteCount(strSendData);
byte[] sendBuffer = new byte[sendBufferSize];
sendBuffer = Encoding.Unicode.GetBytes(strSendData);
client.ClientSocket.Send(sendBuffer);
}
}
}
private void ReceiveMessagesCallback(IAsyncResult AR)
{
receiveMessageDelegate.EndInvoke(AR);
}
/// <summary>
/// 将运行信息加入显示列表
/// </summary>
private void AddRunningInfo(string message)
{
lstRunningInfo.BeginUpdate();
lstRunningInfo.Items.Insert(0, message);
if (lstRunningInfo.Items.Count > 100)
{
lstRunningInfo.Items.RemoveAt(100);
}
lstRunningInfo.EndUpdate();
}
/// <summary>
/// 获取本机地址列表
/// </summary>
public string[] GetLocalAddresses()
{
// 获取主机名
string strHostName = Dns.GetHostName();
// 根据主机名进行查找
IPHostEntry iphostentry = Dns.GetHostEntry(strHostName);
string[] retval = new string[iphostentry.AddressList.Length];
int i = 0;
foreach (IPAddress ipaddress in iphostentry.AddressList)
{
retval[i] = ipaddress.ToString();
i++;
}
return retval;
}
}
/// <summary>
/// 客户端会话信息类
/// </summary>
public class Client
{
Socket m_clientSocket;
public Client() { }
public Socket ClientSocket
{
get { return m_clientSocket; }
set { this.m_clientSocket = value; }
}
}
}
客户端部分:
using System;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace SocketClient
{
public partial class frmClient : Form
{
private Socket m_clientSocket;
private byte[] m_receiveBuffer = new byte[1024];
public frmClient()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void frmClient_Load(object sender, EventArgs e)
{
txtIP.Text = "172.18.20.234";
txtPort.Text = "8899";
}
/// <summary>
/// 连接服务器
/// </summary>
private void btnConnect_Click(object sender, EventArgs e)
{
string serverIP = txtIP.Text;
int serverPort = Int32.Parse(txtPort.Text);
m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(serverIP),serverPort);
try
{
m_clientSocket.Connect(remoteEndPoint);
if (m_clientSocket.Connected)
{
m_clientSocket.BeginReceive(m_receiveBuffer, 0, m_receiveBuffer.Length, 0, new AsyncCallback(ReceiveCallBack), null);
btnConnect.Enabled = false;
btnDisconnect.Enabled = true;
this.AddRunningInfo(">> " + DateTime.Now.ToString() + " Client connect server success.");
}
}
catch (Exception)
{
this.AddRunningInfo(">> " + DateTime.Now.ToString() + " Client connect server fail.");
m_clientSocket = null;
}
}
/// <summary>
/// 断开连接
/// </summary>
private void btnDisconnect_Click(object sender, EventArgs e)
{
if (m_clientSocket != null)
{
m_clientSocket.Close();
btnConnect.Enabled = true;
btnDisconnect.Enabled = false;
btnSend.Enabled = false;
this.AddRunningInfo(">> " + DateTime.Now.ToString() + " Client disconnected.");
}
}
/// <summary>
/// 发送信息
/// </summary>
private void btnSend_Click(object sender, EventArgs e)
{
string strSendData = txtSend.Text;
byte[] sendBuffer = new byte[1024];
sendBuffer = Encoding.Unicode.GetBytes(strSendData);
if (m_clientSocket != null)
{
m_clientSocket.Send(sendBuffer);
}
}
private void ReceiveCallBack(IAsyncResult ar)
{
try
{
int REnd = m_clientSocket.EndReceive(ar);
string strReceiveData = Encoding.Unicode.GetString(m_receiveBuffer, 0, REnd);
this.HandleMessage(strReceiveData);
m_clientSocket.BeginReceive(m_receiveBuffer, 0, m_receiveBuffer.Length, 0, new AsyncCallback(ReceiveCallBack), null);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 处理接收到的数据
/// </summary>
private void HandleMessage(string message)
{
message = message.Replace("/0", "");
if (!string.IsNullOrEmpty(message))
{
this.AddRunningInfo(">> Receive Data from server:" + message);
}
}
private void AddRunningInfo(string message)
{
lstRunningInfo.BeginUpdate();
lstRunningInfo.Items.Insert(0, message);
if (lstRunningInfo.Items.Count > 100)
{
lstRunningInfo.Items.RemoveAt(100);
}
lstRunningInfo.EndUpdate();
}
private void txtSend_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtSend.Text) && m_clientSocket!=null)
{
btnSend.Enabled = true;
}
else
{
btnSend.Enabled = false;
}
}
}
}
Socket 编程示例(二)的更多相关文章
- 多线程Java Socket编程示例
package org.merit.test.socket; import java.io.BufferedReader; import java.io.IOException; import jav ...
- Linux学习之socket编程(二)
Linux学习之socket编程(二) 1.C/S模型——UDP UDP处理模型 由于UDP不需要维护连接,程序逻辑简单了很多,但是UDP协议是不可靠的,实际上有很多保证通讯可靠性的机制需要在应用层实 ...
- Linux socket编程示例(最简单的TCP和UDP两个例子)
一.socket编程 网络功能是Uinux/Linux的一个重要特点,有着悠久的历史,因此有一个非常固定的编程套路. 基于TCP的网络编程: 基于连接, 在交互过程中, 服务器和客户端要保持连接, 不 ...
- Java Socket编程示例
一.Socket简介: 1.什么是Socket 网络上的两个程序通过一个双向的通讯连接实现数据的交换,这个双向链路的一端称为一个Socket.Socket通常用来实现客户方和服务方的连接.Socket ...
- Python 基础之socket编程(二)
Python 基础之socket编程(二) 昨天只是对socket编程做了简单的介绍,只是把socket通信的框架搭建起来,要对其中的功能进行进一步的扩充,就来看看今天的料哈! 一.基于tcp的套接字 ...
- JAVA Socket编程(二)之TCP通信
基于TCP(面向连接)的socket编程,分为客户端和服务器端. 客户端的流程如下: (1)创建套接字(socket) (2)向服务器发出连接请求(connect) (3)和服务器端进行通信(send ...
- Python Socket 编程示例 Echo Server
简评:我们已经从「Python Socket 编程概览」了解了 socket API 的概述以及客户端和服务器的通信方式,接下来让我们创建第一个客户端和服务器,我们将从一个简单的实现开始,服务器将简单 ...
- linux网络编程之socket编程(十二)
今天继续学习socket编程,期待的APEC会议终于在京召开了,听说昨晚鸟巢那灯火通明,遍地礼花,有点08年奥运会的架势,有种冲动想去瞅见一下习大大的真容,"伟大的祖国,我爱你~~~&quo ...
- python之socket编程(二)
标签(空格分隔): socket编程 SocketServer解析 SocketServer内部使用I/O多路复用,多线程,多进程来实现客户端多并发访问Socket服务端,while循环时使用I/O多 ...
随机推荐
- LeetCode_Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 ...
- libeXosip2(1-3) -- How-To send or update registrations.
How-To send or update registrations. The eXtented eXosip stack Initiate a registration To start a re ...
- jquery IE6 select.val() bug报错解决办法
原文地址:http://hi.baidu.com/kinghmx/item/395dbac3261292dcef183b52 最近在写一个页面,在出了ie6外的所有浏览器中都正常(ie7,8,9, ...
- Android 开发笔记-Eclipse中文乱码
使用eclipse时经常中文乱码网上搜罗了下解决办法: 使用Eclipse编辑文件经常出现中文乱码或者文件中有中文不能保存的问题,Eclipse提供了灵活的设置文件编码格式的选项,我们可以通过设置 ...
- 一、cocos2dx概念简介
cocos2dx概念介绍 1)scene,继承自CCScene 场景,一个游戏运行期间的显示界面,一个应用里面可以有多个场景,但是每次只能有一个是激活状态,也可以理解为一次只能显示一个界面. 例如,你 ...
- ACM-简单题之Ignatius and the Princess II——hdu1027
转载请注明出处:http://blog.csdn.net/lttree Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Othe ...
- VB中DateDiff 函数解释
VB中DateDiff 函数使用方法 DateDiff (interval, Date1 , Date2[,firstweekofyear[,firstweekofyear]]) 返回一个Varia ...
- xtrabackup备份恢复测试
http://blog.chinaunix.net/uid-20682026-id-3319204.html
- MySql命令——命令行客户机的分隔符
delimiter // create procedure productpricint() begin select avg(price) as priceaverage from product; ...
- android——仿微拍贷滑动圆形菜单
一次偶然机会接触到微拍贷的app,瞬间被其圆形可滑动菜单吸引了.一直琢磨着给弄出来. 现在弄出来了.先看看效果吧 如果你也喜欢这个菜单.去我的github找源码吧.太忙了.没时间贴代码和讲解了. ht ...