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)
{
if (ReceiveDataLen != 0)
{
ReceiveData = new byte[ReceiveDataLen];
int TempLen = 0;
//合并数组
for (int i = 0; i <= tempList.Count - 1; i++)
{
tempList[i].CopyTo(ReceiveData, TempLen);
TempLen = TempLen + tempList[i].Length;
}
}
break;
}
else
{
if (bytes == 1024)
{
tempList.Add(temp);
}
else
{
byte[] temp2 = new byte[bytes];
Array.Copy(temp, 0, temp2, 0, bytes);
tempList.Add(temp2);
}
ReceiveDataLen = bytes + ReceiveDataLen;
}
}
}
catch (Exception ce)
{
errorStr = "接收错误" + ce.Message;
//禁用Socket
client.Shutdown(SocketShutdown.Both);
//关闭Socket
client.Close();
return;
}
//禁用Socket
client.Shutdown(SocketShutdown.Both);
//关闭Socket
client.Close();
}
[解决办法]
看了半天没看懂,帮你顶一下
[解决办法]
在WebService里不建议使用Ref类型的参数。
你想返回数据,把要返回的数据全部放在以xml里返回。
public xmldocuemnt CommunicateToCrossServer(string GroupServerIPAddr, int GroupServerIPPort, byte[] SendData)
返回结果都在xml存放。
[解决办法]
应该是 ref的问题,你单步调试一下不就知道了。
[解决办法]