i具有以下程序,该程序以XML的形式接收套接字流并将其插入SQL数据库。我的问题是: - 当程序以调试模式启动时,所有XML流将成功插入数据库中。 - 正常启动(无调试模式)时,该程序将插入一个XML流并错过另一个流(因此,如果我有20个流,将只插入10个流)。
此代码中的错误是什么?
client = Listener.AcceptTcpClient();
netstream = client.GetStream();
Status = "Connected to a clientn";
byte[] bytes = new byte[client.ReceiveBufferSize + 1];
netstream.Read(bytes, 0, Convert.ToInt32(client.ReceiveBufferSize));
// Return the data received from the client
string clientdata = System.Text.Encoding.ASCII.GetString(bytes);
Status="Client sent: " + clientdata ;
StorePolicy(clientdata);
Query = clientdata;
Query = Query.Replace(" ", "");
StorePolicy(Query);
Status="Received Query: " + Query;
StorePolicy("Received Query: " + Query);
netstream.Close();
client.Close();
///////////////insert into database/////////
try
{
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand Cmd = new SqlCommand();
string[] words = Query.Split(new[] { "</RECORD>" }, StringSplitOptions.None);
StorePolicy(Query);
foreach (string word in words)
{
if (!string.IsNullOrEmpty(word))
{
record = word.Replace("'", "''") + "</RECORD>";
StorePolicy(record);
StrQuery = "INSERT INTO SMSListenner(XMLText) VALUES ('" + record.Replace("'", "''") + "')";
Cmd = new SqlCommand(StrQuery, conn);
conn.Open();
Cmd.ExecuteReader();
conn.Close();
StorePolicy(StrQuery);
}
}
}
msdn清楚地说:
TCPLISTENER类提供了简单的方法来聆听和 在阻止同步模式中接受传入连接请求。
这意味着一旦连接一个客户端,它就会阻止每个客户端,直到您的"可怜的人Web服务器"用流到达。等待队列的其他客户可能会在平均时间段落。请注意,您正在做很多事情要处理请求。这意味着您将在很大的时间内阻止其他客户。
另外,您也不例外的处理,因此一个客户可以杀死您的整个"服务器"。
我的建议,如果不是学生的项目来了解插座的工作方式,请使用与连接之间隔离的适当的Web服务器,并且能够同时处理多个连接。