关于Socket的技术方案!
Socket的客户端为移动设备,通信方式为GPRS,因此通信有时由于信号的原因将很不稳定,所以我想采取每次发送数据时都重新连接服务器,好像Http这种面向无连接的协议就应该是这种方式.
首先:我不确定我这种操作方式对不对,对性能有什么影响.是否还有更完美的解决办法.
其次:我实际测试时遇到一个问题,就是对于同一个Socket对象而言,当首次连接后,我使用ShutDown和Close进行关闭后,再进行连接时就会报异常错误.我现在暂时的处理办法就是在调用Connect连接时先New一个新的Socket对象,同样,我不知道这样的处理方式会造成多大的性能问题!
请各位大侠赐教,小弟在此表示感谢啦!!!!
我的完整代码如下:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
namespace Newera.BaoSteelSmartClient.Proxy {
/// <summary>
/// Socket连接类
/// </summary>
public class SocketConnector {
private const string CMDEND="@";
private const string CMDBEGIN="@";
private System.Net.Sockets.Socket socket=new Socket(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
private System.Net.IPEndPoint ipEndPoint=null;
private System.Text.StringBuilder sb=new StringBuilder(1024);
System.Text.Decoder myDecoder=System.Text.Encoding.Default.GetDecoder();
private string m_RemoteIP=null;
private int m_RemotePort;
public string RemoteIP {
get {
return this.m_RemoteIP;
}
set {
try {
System.Net.IPAddress.Parse(value);
}
catch {
throw;
}
this.m_RemoteIP=value;
this.ipEndPoint=null;
}
}
public int RemotePort {
get {
return this.m_RemotePort;
}
set {
if (value<=0 || value>65535) {
throw new Exception("远程主机端口非法");
}
this.m_RemotePort=value;
this.ipEndPoint=null;
}
}
public bool Connected {
get {
return this.socket.Connected;
}
}
public SocketConnector() {
}
public SocketConnector(string remoteIP,int remotePort) {
this.RemoteIP=remoteIP;
this.RemotePort=remotePort;
}
public void Connect() {
if (ipEndPoint==null) {
ipEndPoint=new IPEndPoint(IPAddress.Parse(this.m_RemoteIP),this.m_RemotePort);
}
try {
if (this.socket.Connected) {
this.socket.Shutdown(SocketShutdown.Both);
this.socket.Close();
}
this.socket.Connect(this.ipEndPoint);
if (socket.Connected==false) {
throw new Exception("连接"+this.m_RemoteIP+":"+this.m_RemotePort.ToString()+"远程主机失败!");
}
}
catch {
throw new Exception("连接"+this.m_RemoteIP+":"+this.m_RemotePort.ToString()+"远程主机失败!");
}
}
public void DisConnect() {
try {
if (this.socket.Connected) {
this.socket.Shutdown(SocketShutdown.Both);
this.socket.Close();
}
}
catch {
throw;
}
}
public string SendCommand(string cmd) {
this.sb.Remove(0,this.sb.Length);
this.sb.Capacity=1024;
int bytes=0;
cmd=SocketConnector.CMDBEGIN+cmd+SocketConnector.CMDEND;
byte[] byteSend=System.Text.Encoding.Default.GetBytes(cmd);
try {
this.Connect();
this.socket.Send(byteSend);
//循环读取,直到接收完所有数据
while(true) {
byte[] recvBytes=new byte[this.socket.Available+1];
bytes=this.socket.Receive(recvBytes);
if(bytes==0) {
throw new Exception("与服务器连接已经断开!");
}
char[] tSaveC=new char[myDecoder.GetCharCount(recvBytes,0,bytes)];
myDecoder.GetChars(recvBytes,0,bytes,tSaveC,0);
sb.Append(tSaveC);
if(sb.Length>=2) {
if(sb.ToString().Substring(sb.Length-1,1)==SocketConnector.CMDEND)
break;
}
}
}
catch(System.Net.Sockets.SocketException ex) {
throw new Exception("发送数据到"+this.m_RemoteIP+":"+this.m_RemotePort.ToString()+"远程主机失败:"+ex.Message);
}
finally {
this.DisConnect();
}
//去掉开头和结尾的字符
sb.Remove(0,1);
sb.Remove(sb.Length-1,1);
return sb.ToString();
}
}
}