【发布时间】:2013-12-27 13:29:34
【问题描述】:
我是 c# 中 TCP/IP 编程的新手,所以我非常渴望找到解决当前问题的方法!
我设计了一个在 Windows 7 下运行的 C# Windows 应用程序,带有 Sqlserver 2005 数据库。我的应用程序试图通过 TCP/IP 连接将 HL7 记录发送到 Unix 实验室。系统机器。
我可以连接正常,然后发送 HL7。但我无法从实验室得到任何答复。服务器!连接超时,错误代码为 10060,_COMPlusExceptionCode 值为 -532462766。
这是我的 c# 'Connect' 方法的示例:
buildSendHL7_TCPIP hl7Agent = new buildSendHL7_TCPIP();
// 类 'buildSendHL7_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 上的另一台服务器。我收到相同的超时错误消息。
【问题讨论】:
标签: c# sql-server sockets tcp