作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面的代码用于将数据发送到网关服务器并作为响应接收确认。但有时没有收到确认,在这种情况下,我认为必须再次发送请求。下面的代码肯定不处理那部分。我想知道这个问题的任何可能的解决方案。
class TestSMS
{
public static string strIp = "10.00.105.00";
public static int Port = 1009;
//-----------------connect------------------//
public static Socket Connect(string host, int port)
{
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
Console.WriteLine("Establishing Connection to {0}",
host);
socket.Connect(host, port);
clsError.LogError("TestSMS:Connect()", "Connection established");
return socket;
}
//---------------End connect----------------//
//-------------------send--------------------//
public static int SendReceiveTest(Socket server,string Message)
{
byte[] connectionString = Encoding.UTF8.GetBytes("CON^`!XML^`!ABCD#END#");
byte[] msg = Encoding.UTF8.GetBytes(Message);
byte[] bytes = new byte[1024];
string response = string.Empty;
int byteCount = 0;
int i = 0;
try
{
// Blocks until send returns.
i = server.Send(connectionString, connectionString.Length, SocketFlags.None);
clsError.LogError("TestSMS:SendReceiveTest()", "Connection string sent");
// Get reply from the server.
byteCount = server.Receive(bytes, server.Available,
SocketFlags.None);
if (byteCount > 0)
{
response = Encoding.UTF8.GetString(bytes);
if (response.IndexOf("CON!0") != 0)
{
i = server.Send(msg, msg.Length, SocketFlags.None);
clsError.LogError("TestSMS:SendReceiveTest()", "Message data sent");
byteCount = server.Receive(bytes, server.Available,
SocketFlags.None);
response = Encoding.UTF8.GetString(bytes);
if ((response.ToString().IndexOf("ACK!1") == 0) || (response.ToString().IndexOf("ACK!MSGSTATUS=TRUE") == 0))
{
clsError.LogError("TestSMS:SendReceiveTest()", "Message sent successfully: " + response);
}
//log
}
else
{
clsError.LogError("TestSMS:SendReceiveTest()", "Could not handshake with SMS Server");
}
}
else
{
clsError.LogError("TestSMS:SendReceiveTest()", "No Acknowledgement recieved");
}
}
catch (SocketException e)
{
clsError.LogError("TestSMS:SendReceiveTest()", e.Message.ToString());
return (e.ErrorCode);
}
return 0;
}
//-----------------End send------------------//
static void Main(string[] args)
{
try
{
string strXmlFormat = " MSG^`!<DEPT>IPL</DEPT><APPID>TLC</APPID><MOBILE>918879440021</MOBILE><DEPTMSGID>14092111115JH21075</DEPTMSGID><MESSAGE>This is a test message</MESSAGE><FROMDATETIME></FROMDATETIME><TODATETIME></TODATETIME><NODELIVERYTIMEFROM>2200</NODELIVERYTIMEFROM><NODELIVERYTIMETO>0700</NODELIVERYTIMETO><HTTPMODE>S</HTTPMODE><REMARKS></REMARKS><REMARKS1></REMARKS1><REMARKS2></REMARKS2><TRN_GENERATE_TIMESTAMP>2015-12-10 16:48:54 </TRN_GENERATE_TIMESTAMP>#END#";
clsError.LogError("Inside Main:Msg in XML format", strXmlFormat);
Socket Socket;
Socket = TestSMS.Connect(TestSMS.strIp, TestSMS.Port);
TestSMS.SendReceiveTest(Socket, strXmlFormat);
}
catch (Exception ex)
{
clsError.LogError("Inside Main:Main()", ex.Message.ToString());
throw ex;
}
}
}
最佳答案
问题:
您的主要问题在 server.Receive
陈述(有两个)
byteCount = server.Receive(bytes, server.Available, SocketFlags.None); //only read when byte is available
server.Receive
行,但等待响应。因此它不会转到 if (byteCount > 0)
线。 Form
右边是我的模拟网关服务器。它表明它收到了消息。 if (byteCount > 0)
但是,看看那个(!),byteCount
是零!这是核心的错误。我为 SendReceiveTest
做了一个循环看看接下来发生了什么... Form
显示客户端又发送了一条测试消息并且服务器没有给它任何回复但它直接转到 if (byteCount > 0)
行并查看 byteCount
值(value)。它是 10(这是“DummyReply”消息长度!),也就是 上一页 我从服务器发送的回复! server.Receive
为您的系统带来问题。
ASync
来解决这个问题。而不是
Sync
,但允许您的程序无法通过
ASync
解决的可能性出于某种原因,以下是我将根据您的代码执行的操作:
//-------------------send--------------------//
public static int SendReceiveTest(Socket server, string Message) {
byte[] connectionString = Encoding.UTF8.GetBytes("CON^`!XML^`!ABCD#END#");
byte[] msg = Encoding.UTF8.GetBytes(Message);
byte[] bytes = new byte[1024];
string response = string.Empty;
int byteCount = 0;
int i = 0;
try {
i = server.Send(connectionString, connectionString.Length, SocketFlags.None); // Blocks until send returns.
clsError.LogError("TestSMS:SendReceiveTest()", "Connection string sent");
// Get reply from the server.
int retryCount = 0, retryLimit = 5; //declare these two new variables
while (retryCount < retryLimit) { //repeats the waiting as long as it is within the retry limit
if (server.Available > 0) //check for the available byte first, please change 0 with number of bytes which you expected
byteCount = server.Receive(bytes, server.Available, SocketFlags.None); //only read when byte is available
retryCount++;
//Log the retry count here if your wish: clsError.LogError("Retry Count: " + retryCount.ToString(), "Retry attempt to get response");
if (byteCount > 0 && retryCount < retryLimit) //if there is byte received at this point or condition violated, break
break;
System.Threading.Thread.Sleep(1000); //no byte received re-check in another second;
}
if (byteCount <= 0) { // no byte received
clsError.LogError("TestSMS:SendReceiveTest()", "No Acknowledgement recieved");
return -1; //change this with your own error code
} //passing this point means you receive something
response = Encoding.UTF8.GetString(bytes);
if (response.IndexOf("CON!0") == 0) {
clsError.LogError("TestSMS:SendReceiveTest()", "Could not handshake with SMS Server");
return -2; //change this with your own error code
} //passing this points means response.IndexOf("CON!0") != 0
retryCount = 0; //reset retry count, change retryLimit as per necessary if needed be
byteCount = 0; //reset byte count
i = server.Send(msg, msg.Length, SocketFlags.None); //"Message data sent"
while(retryCount < retryLimit) { //similar concept with above
if (server.Available > 0) //check for the available byte first, please change 0 with number of bytes which you expected
byteCount = server.Receive(bytes, server.Available, SocketFlags.None); //only read when byte is available
//Log the retry count here if your wish: clsError.LogError("Retry Count: " + retryCount.ToString(), "Retry attempt to get message");
retryCount++;
if (byteCount > 0 && retryCount < retryLimit) //if there is byte received at this point or condition violated, break
break;
System.Threading.Thread.Sleep(1000); //no byte received re-check in another second;
}
if (byteCount <= 0) {
//Log here accordingly. i.e.: clsError.LogError("TestSMS:GetMessage()", "No message received");
return -3; //change this with your error code
} //passing this point means you receive the message
response = Encoding.UTF8.GetString(bytes);
if ((response.ToString().IndexOf("ACK!1") == 0) || (response.ToString().IndexOf("ACK!MSGSTATUS=TRUE") == 0)) {
clsError.LogError("TestSMS:SendReceiveTest()", "Message sent successfully: " + response);
}
//log
} catch (SocketException e) {
clsError.LogError("TestSMS:SendReceiveTest()", e.Message.ToString());
//return (e.ErrorCode); //not sure if this is a good idea, I rather change this.
//If you really need e.ErrorCode, it would have been better to create `out` parameter in the method
return -4; //Socket exception
}
return 0; // at this point, ideally there is no error at all
}
//-----------------End send------------------//
if { /*simple case*/ return errorCode; } //continue
switch
关于如何处理不同错误代码的处理程序。 IE。if (errorCode != 0){ //if there is error code.
switch(errorCode){
case -1: //no ack
//do something, such as repeating the process X times
break;
case -2: //fail to handshake
//do something
break;
... and so on
}
}
exception
上),因为它会干扰我定义的错误代码。但是如果需要,这个套接字错误代码可以返回 out
方法中的关键字。然后,在 case -4:
下处理此套接字错误信息(使用我的例子)。case -1:
if (repeatNumber < repeatLimit) {
SendReceiveTest(server, message); //repeats
repeatNumber++; //reset this number when successful
} else {
//do something, it fails!
}
break;
server.Available
作为在处理之前首先检查是否有任何可用数据的一种方式if (server.Available > 0) //check for the available byte first, please change 0 with number of bytes which you expected
byteCount = server.Receive(bytes, server.Available, SocketFlags.None); //only read when byte is available
server.Receive
命令接收之前的字节数size
你期望的最低限度。while
中。在 retryCount
的帮助下,用于重试目的的循环, retryLimit
, 和 System.Threading.Thread.Sleep
.int retryCount = 0, retryLimit = 5;
while (retryCount < retryLimit) { //repeats the waiting as long as it is within the retry limit
if (server.Available > 0) //check for the available byte first, please change 0 with number of bytes which you expected
byteCount = server.Receive(bytes, server.Available, SocketFlags.None); //only read when byte is available
retryCount++;
//Log the retry count here if your wish: clsError.LogError("Retry Count: " + retryCount.ToString(), "Retry attempt to get response");
if (byteCount > 0 && retryCount < retryLimit) //if there is byte received at this point or condition violated, break
break;
System.Threading.Thread.Sleep(1000); //no byte received re-check in another second;
}
if (byteCount <= 0) { // no byte received
clsError.LogError("TestSMS:SendReceiveTest()", "No Acknowledgement recieved");
return -1; //change this with your own error code
} //passing this point means you receive something
关于c# - 如果没有及时收到网关服务器的确认,可能需要通过Socket重新发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34332435/
我正在尝试使用 jsmart渲染Smarty客户端有 3 个模板。如果您没有使用它们的经验,请继续阅读,因为这可能只是我犯的一个简单的 JavaScript 错误。 它适用于简单的模板: 我创建模板(
对于每个 http 请求,ASP .NET 页面是否及时编译(JITting),或者在第一次请求页面时,或者在应用程序启动时编译? 我找不到任何相关资源。 最佳答案 ASP.NET automatic
我正在使用 Pandas 来管理一组具有多个属性的文件: import pandas as pd data = {'Objtype' : ['bias', 'bias', 'flat', 'fla
有没有办法找出单循环动画 GIF 需要多长时间才能完成? 最佳答案 好吧,具体情况取决于您使用什么接口(interface)来操作这些动画 GIF(我不知道原生 Java/AWT/Swing 中真正巧
我有三个相关列:时间、ID 和交互。我如何创建一个新列,其 id 值在给定时间窗口中的“交互”列中为“1”? 应该看起来像这样: time id vec_len quadrant int
我是一名优秀的程序员,十分优秀!