1
我在計算爲什麼我只能從運行在我的計算機上的服務器應用程序(LocalHost)收到一個答覆時遇到了一些問題。我沒有這個服務器應用程序的源代碼,但它是一個Java應用程序。發送的消息是xml結構,必須以EoT標籤結束。C#套接字。僅能夠收到第一條消息
的通信:
- Client連接斷絕。
- 客戶端發送消息到服務器。
- 服務器發送消息到客戶端。
- 客戶端發送消息到服務器。
- 服務器發送一個傳輸結束字符。
- 客戶端發送消息到服務器。
- 服務器發送一個傳輸結束字符。
這是怎麼我的客戶端連接,發送和接收:
public bool ConnectSocket(string server, int port)
{
System.Net.IPHostEntry hostEntry = null;
try
{
// Get host related information.
hostEntry = System.Net.Dns.GetHostEntry(server);
}
catch (System.Exception ex)
{
return false;
}
// Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
// an exception that occurs when the host IP Address is not compatible with the address family
// (typical in the IPv6 case).
foreach (System.Net.IPAddress address in hostEntry.AddressList)
{
System.Net.IPEndPoint ipe = new System.Net.IPEndPoint(address, port);
System.Net.Sockets.Socket tempSocket = new System.Net.Sockets.Socket(ipe.AddressFamily, System.Net.Sockets.SocketType.Stream,
System.Net.Sockets.ProtocolType.Tcp);
tempSocket.Connect(ipe);
if (tempSocket.Connected)
{
m_pSocket = tempSocket;
m_pSocket.NoDelay = true;
return true;
}
else
continue;
}
return false;
}
}
public void Send(string message)
{
message += (char)4;//We add end of transmission character
m_pSocket.Send(m_Encoding.GetBytes(message.ToCharArray()));
}
private void Recive()
{
byte[] tByte = new byte[1024];
m_pSocket.Receive(tByte);
string recivemessage = (m_Encoding.GetString(tByte));
}