我正在使用本教程:如何为 Windows Phone 创建和使用 TCP 套接字客户端应用程序NotConnected
,但在尝试访问我家用计算机上的简单 TCP 服务时收到错误消息。我的 WP8 和 PC 在同一个WiFi
网络上。我尝试在禁用防火墙的情况下使用我的公共 IP 地址和端口 7(Echo)和端口 17(每日报价)进行连接,但仍然一无所获。[我启用了简单的 TCP 服务。]
代码的相关部分似乎在 MainPage.xaml.cs 中:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TCP_Client.Resources;
namespace TCP_Client
{
public partial class MainPage : PhoneApplicationPage
{
// Constants
const int ECHO_PORT = 7; // The Echo protocol uses port 7 in this sample
const int QOTD_PORT = 17; // The Quote of the Day (QOTD) protocol uses port 17 in this sample
/// <summary>
/// Handle the btnEcho_Click event by sending text to the echo server
/// and outputting the response
/// </summary>
private void btnEcho_Click(object sender, RoutedEventArgs e)
{
// Clear the log
ClearLog();
// Make sure we can perform this action with valid data
if (ValidateRemoteHost() && ValidateInput())
{
// Instantiate the SocketClient
SocketClient client = new SocketClient();
// Attempt to connect to the echo server
Log(String.Format("Connecting to server '{0}' over port {1} (echo) ...", txtRemoteHost.Text, ECHO_PORT), true);
string result = client.Connect(txtRemoteHost.Text, ECHO_PORT);
Log(result, false);
// Attempt to send our message to be echoed to the echo server
Log(String.Format("Sending '{0}' to server ...", txtInput.Text), true);
result = client.Send(txtInput.Text);
Log(result, false);
// Receive a response from the echo server
Log("Requesting Receive ...", true);
result = client.Receive();
Log(result, false);
// Close the socket connection explicitly
client.Close();
}
}
/// <summary>
/// Handle the btnEcho_Click event by receiving text from the Quote of
/// the Day (QOTD) server and outputting the response
/// </summary>
private void btnGetQuote_Click(object sender, RoutedEventArgs e)
{
// Clear the log
ClearLog();
// Make sure we can perform this action with valid data
if (ValidateRemoteHost())
{
// Instantiate the SocketClient object
SocketClient client = new SocketClient();
// Attempt connection to the Quote of the Day (QOTD) server
Log(String.Format("Connecting to server '{0}' over port {1} (Quote of the Day) ...", txtRemoteHost.Text, QOTD_PORT), true);
string result = client.Connect(txtRemoteHost.Text, QOTD_PORT);
Log(result, false);
// Note: The QOTD protocol is not expecting data to be sent to it.
// So we omit a send call in this example.
// Receive response from the QOTD server
Log("Requesting Receive ...", true);
result = client.Receive();
Log(result, false);
// Close the socket conenction explicitly
client.Close();
}
}
#region UI Validation
/// <summary>
/// Validates the txtInput TextBox
/// </summary>
/// <returns>True if the txtInput TextBox contains valid data, otherwise
/// False.
///</returns>
private bool ValidateInput()
{
// txtInput must contain some text
if (String.IsNullOrWhiteSpace(txtInput.Text))
{
MessageBox.Show("Please enter some text to echo");
return false;
}
return true;
}
/// <summary>
/// Validates the txtRemoteHost TextBox
/// </summary>
/// <returns>True if the txtRemoteHost contains valid data,
/// otherwise False
/// </returns>
private bool ValidateRemoteHost()
{
// The txtRemoteHost must contain some text
if (String.IsNullOrWhiteSpace(txtRemoteHost.Text))
{
MessageBox.Show("Please enter a host name");
return false;
}
return true;
}
#endregion
#region Logging
/// <summary>
/// Log text to the txtOutput TextBox
/// </summary>
/// <param name="message">The message to write to the txtOutput TextBox</param>
/// <param name="isOutgoing">True if the message is an outgoing (client to server)
/// message, False otherwise.
/// </param>
/// <remarks>We differentiate between a message from the client and server
/// by prepending each line with ">>" and "<<" respectively.</remarks>
private void Log(string message, bool isOutgoing)
{
string direction = (isOutgoing) ? ">> " : "<< ";
txtOutput.Text += Environment.NewLine + direction + message;
}
/// <summary>
/// Clears the txtOutput TextBox
/// </summary>
private void ClearLog()
{
txtOutput.Text = String.Empty;
}
#endregion
// Constructor
public MainPage()
{
InitializeComponent();
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
// Sample code for building a localized ApplicationBar
//private void BuildLocalizedApplicationBar()
//{
// // Set the page's ApplicationBar to a new instance of ApplicationBar.
// ApplicationBar = new ApplicationBar();
// // Create a new button and set the text value to the localized string from AppResources.
// ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
// appBarButton.Text = AppResources.AppBarButtonText;
// ApplicationBar.Buttons.Add(appBarButton);
// // Create a new menu item with the localized string from AppResources.
// ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
// ApplicationBar.MenuItems.Add(appBarMenuItem);
//}
}
}