我是c#中TCP / IP编程的新手,所以我很想解决当前的问题!
我设计了一个在Windows 7下运行的带有Sqlserver 2005数据库的C#Windows应用程序。我的应用程序试图通过TCP / IP连接向Unix实验室发送HL7记录。系统机器。
我可以连接好,然后发送HL7。但我无法得到实验室的任何答复。服务器!连接超时,错误代码为10060,以及_COMPlusExceptionCode值为-532462766。
以下是我的c#'Connect'方法示例:
buildSendHL7_TCPIP hl7Agent = new buildSendHL7_TCPIP();
// class'sbuildSendHL7_TCPIP(); '包含构建HL7段的方法,以及通过TCP连接发送和接收消息的方法
string strServerIPAddress = string.Empty;
Int32 intSocketNo = new Int32();
bool sentOK = false;
/***********************************/
/*Try send the HL7 to LIS-HORIZON...*/
/***********************************/
strServerIPAddress = "10.1.6.248";
intSocketNo = 5910;
sentOK = hl7Agent.SendHL7(strServerIPAddress, intSocketNo, strHL7_Record);
if (!sentOK)
{
_strUIMessage = "*Error* HL7 Message NOT sent to LIS!";
opsMessageBox mb = new opsMessageBox(this);
mb.ShowDialog();
mb.Close();
goto EndLabel;
}
以下是我为构建TCP连接并将HL7发送到LIS服务器而创建的方法:
public bool SendHL7(string strIPAddress, Int32 intSocket, string hl7message)
{
/* send the complete HL7 message to the server...*/
int port = (int)intSocket;
IPAddress localAddr = IPAddress.Parse(strIPAddress);
try
{
// Add the leading & trailing character field-separator and CR LineFeed' t.
string llphl7message = null;
llphl7message = "|";
llphl7message += hl7message;
llphl7message += Convert.ToChar(28).ToString();
llphl7message += Convert.ToChar(13).ToString();
// Get the size of the message that we have to send.
Byte[] bytesToSend = Encoding.ASCII.GetBytes(llphl7message);
Byte[] bytesReceived = new Byte[256];
// Create a socket connection with the specified server and port.
Socket s = ConnectSocket(localAddr, port);
// If the socket could not get a connection, then return false.
if (s == null)
return false;
// Send message to the server.
s.Send(bytesToSend, bytesToSend.Length, 0);
// Receive the response back
int bytes = 0;
s.ReceiveTimeout = 3000; /* 3 seconds wait before timeout */
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0); /* IMEOUT occurs!!! */
string page = Encoding.ASCII.GetString(bytesReceived, 0, bytes);
s.Close();
// Check to see if it was successful
if (page.Contains("MSA|AA"))
{
return true;
}
else
{
return false;
}
}
catch (SocketException e)
{
MessageBox.Show("SocketExecptionError:" + e);
return false;
}
}
private static Socket ConnectSocket(IPAddress server, int port)
{
Socket s = null;
IPHostEntry hostEntry = null;
// Get host related information.
hostEntry = Dns.GetHostEntry(server);
foreach (IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, port);
Socket tempSocket = new Socket(ipe.AddressFamily,SocketType.Stream,ProtocolType.Tcp);
tempSocket.Connect(ipe);
if (tempSocket.Connected)
{
s = tempSocket;
break;
}
else
{
continue;
}
}
return s;
}
我被告知,由于病毒问题,套接字5910无法在Windows 7中接收任何通信。这是真的?如果是这样,我尝试连接到我们的网络(PACS / RIS)套接字#5556上的另一台服务器。我收到了SAME超时错误消息。
答案 0 :(得分:0)
根据预期的协议,行为看起来像服务器不理解您的请求/不会将其识别为完整的消息。
我从您的代码中了解到您正在发送HL7消息。我不知道这个协议,根据谷歌它可能是健康级别7.我发现的例子是从一些文本开始,然后是|作为分隔符。您的消息以|开头。也许有问题......
因此,您应该查看您发送的消息是否与协议定义匹配。