关于Socket的技术方案!

wumylove1234 2006-09-05 04:19:39
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();
}
}
}
...全文
299 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
YAOHE 2006-09-06
  • 打赏
  • 举报
回复
对,用UDP算了,还用什么TCP
www_123du_com 2006-09-06
  • 打赏
  • 举报
回复
那还不如用UDP

TCP连接时会有三方握手,有点不划算

另外,Socket等非托管资源的封装类基本都是这样,一旦Close之后就不能再用,必须重新new
wumylove1234 2006-09-06
  • 打赏
  • 举报
回复
为什么晕?是我表述的有问题吗?
顺便顶一下.
wumylove1234 2006-09-06
  • 打赏
  • 举报
回复
UDP是所谓的无连接协议,不保证数据传输的完整性与准确性,不知道会不会有问题.
tshark 2006-09-05
  • 打赏
  • 举报
回复
头晕忽忽的...

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧