Windows phone 8 从windows phone 8传输TCP套接字
我正在做一个示例,其中我尝试使用TCP将数据从客户端(windows phone 8)应用程序传输到windows窗体应用程序(充当服务器) 在windows窗体应用程序中,我正在创建一个类似这样的侦听器Windows phone 8 从windows phone 8传输TCP套接字,windows-phone-8,Windows Phone 8,我正在做一个示例,其中我尝试使用TCP将数据从客户端(windows phone 8)应用程序传输到windows窗体应用程序(充当服务器) 在windows窗体应用程序中,我正在创建一个类似这样的侦听器 TcpListener tcpListener = null; IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0]; try {
TcpListener tcpListener = null;
IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
try
{
// Set the listener on the local IP address
// and specifing the port.
tcpListener = new TcpListener(ipAddress, 13);
tcpListener.Start();
output = "Waiting for a connection...";
}
catch (Exception e)
{
output = "Error: " + e.ToString();
MessageBox.Show(output);
}
while (true)
{
Thread.Sleep(10);
TcpClient tcpClient = tcpListener.AcceptTcpClient();
byte[] bytes = new byte[256];
NetworkStream stream = tcpClient.GetStream();
stream.Read(bytes, 0, bytes.Length);
}
public Task SendUsingManagedSocketsAsync(string strServerIP)
{
// enable asynchronous task completion
completionSource = new TaskCompletionSource<object>();
// create a new socket
var socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
// create endpoint
var ipAddress = IPAddress.Parse(strServerIP);
var endpoint = new IPEndPoint(ipAddress, PORT);
// create event args
var args = new SocketAsyncEventArgs();
args.RemoteEndPoint = endpoint;
args.Completed += SocketConnectCompleted;
// check if the completed event will be raised. If not, invoke the handler manually.
if (!socket.ConnectAsync(args))
SocketConnectCompleted(args.ConnectSocket, args);
return completionSource.Task;
}
在WindowsPhone8应用程序中,我使用托管套接字连接到侦听器,类似这样的事情
TcpListener tcpListener = null;
IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
try
{
// Set the listener on the local IP address
// and specifing the port.
tcpListener = new TcpListener(ipAddress, 13);
tcpListener.Start();
output = "Waiting for a connection...";
}
catch (Exception e)
{
output = "Error: " + e.ToString();
MessageBox.Show(output);
}
while (true)
{
Thread.Sleep(10);
TcpClient tcpClient = tcpListener.AcceptTcpClient();
byte[] bytes = new byte[256];
NetworkStream stream = tcpClient.GetStream();
stream.Read(bytes, 0, bytes.Length);
}
public Task SendUsingManagedSocketsAsync(string strServerIP)
{
// enable asynchronous task completion
completionSource = new TaskCompletionSource<object>();
// create a new socket
var socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
// create endpoint
var ipAddress = IPAddress.Parse(strServerIP);
var endpoint = new IPEndPoint(ipAddress, PORT);
// create event args
var args = new SocketAsyncEventArgs();
args.RemoteEndPoint = endpoint;
args.Completed += SocketConnectCompleted;
// check if the completed event will be raised. If not, invoke the handler manually.
if (!socket.ConnectAsync(args))
SocketConnectCompleted(args.ConnectSocket, args);
return completionSource.Task;
}
使用ManagedSocketsAsync(字符串strServerIP)发送公共任务
{
//启用异步任务完成
completionSource=新任务completionSource();
//创建一个新套接字
var套接字=新套接字(AddressFamily.InterNetwork,
SocketType.Stream,
原型(Tcp);
//创建端点
var ipAddress=ipAddress.Parse(strServerIP);
var端点=新的IPEndPoint(ipAddress,端口);
//创建事件参数
var args=new SocketAsyncEventArgs();
args.RemoteEndPoint=端点;
args.Completed+=SocketConnectCompleted;
//检查是否将引发已完成的事件。如果未引发,请手动调用处理程序。
if(!socket.ConnectAsync(args))
SocketConnectCompleted(args.ConnectSocket,args);
返回completionSource.Task;
}
但是我得到了一个连接被拒绝的套接字错误。我的方法是否正确,如何通过windows phone连接tcp侦听器。。在这里,我无法更改windows窗体应用程序的代码,因为它是我的服务器代码。。还是强制更改我服务器的代码