WebService调用Socket问题
首先写Socket的方法
public static void CommunicateToCrossServer(string GroupServerIPAddr, int GroupServerIPPort, byte[] SendData, ref byte[] ReceiveData, ref int ReceiveDataLen, ref string errorStr)
在写WebService
/// <summary>
/// 和道口通信服务器通信
/// </summary>
/// <param name="GroupServerIPAddr">输入参数:通信服务器地址</param>
/// <param name="GroupServerIPPort">输入参数:通信服务器端口号</param>
/// <param name="SendData">输入参数:发送了注册信息</param>
/// <param name="ReceiveData">输出参数:接收数据byte[]</param>
/// <param name="ReceiveDataLen">输出参数:接收数据长度</param>
/// <param name="errorStr">输出参数:出错信息</param>
[WebMethod]
public void CommunicateToCrossServer(string GroupServerIPAddr, int GroupServerIPPort, byte[] SendData, ref byte[] ReceiveData, ref int ReceiveDataLen, ref string errorStr)
{
Domain.CommunicateToGroupServer.CommunicateToCrossServer(GroupServerIPAddr, GroupServerIPPort, SendData, ref ReceiveData, ref ReceiveDataLen, ref errorStr);
}
前台程序:
如果直接调用CommunicateToCrossServer程序正常;
Domain.CommunicateToGroupServer.CommunicateToCrossServer(GroupServerIPAddr, GroupServerIPPort, SendData, ref ReceiveData, ref ReceiveDataLen, ref errorStr);这是正常!
但是通过Webservice调用CommunicateToCrossServer发现返回的数据全部为0,也就是ReceiveData数组里面的值全部为0;不知道哪里出了问题?
//通过WebService通信
WebServiceToCommunication.Service1SoapClient w = new RailwayCross2011.AppForServer.WebServiceToCommunication.Service1SoapClient();
w.CommunicateToCrossServer(GroupServerIPAddr, GroupServerIPPort, SendData, ref ReceiveData, ref ReceiveDataLen, ref errorStr);
这个出错,ReceiveData数组里面的值全部为0;
下面是CommunicateToCrossServer方法:
public static void CommunicateToCrossServer(string GroupServerIPAddr, int GroupServerIPPort, byte[] SendData, ref byte[] ReceiveData, ref int ReceiveDataLen, ref string errorStr)
{
IPHostEntry ipHostInfo = Dns.Resolve(GroupServerIPAddr);
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, GroupServerIPPort);
ReceiveDataLen = 0;
errorStr = "";
//创建一个基于TCP的Socket
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
//尝试连接
client.Connect(remoteEP);
}
catch (Exception se)
{
errorStr = "连接错误" + se.Message;
return;
}
try
{
//向主机发送请求
client.Send(SendData, SendData.Length, 0);
}
catch (Exception ce)
{
errorStr = "发送错误:" + ce.Message;
}
int bytes = 0;
try
{
List<byte[]> tempList = new List<byte[]>();
while (true)
{
byte[] temp = new byte[1024];
//每次读取1024
bytes = client.Receive(temp, 1024, 0);
if (bytes <= 0)
{