我目前正在制作一个需要通过 TCP/IP 连接到 Windows PC 的 Android 应用程序,其中 Windows PC 是服务器,Android 是客户端。问题出在连接的某个地方!每当我尝试启动连接时,它都会等待客户端,当在客户端中单击连接按钮时,什么都没有发生!
我正在处理的服务器端代码是,
TcpListener listner = new TcpListener(IPAddress.Any, 8126);
NetworkStream netstream;
TcpClient client;
byte[] datalength = new byte[4];
public void btnstart_Click(object sender, EventArgs e)
{
Thread startconnection = new Thread(new ThreadStart(connect));
startconnection.Start();
}
public void connect()
{
try
{
listner.Start(); //Starts Listening to any IP with port 8126
//Invoked Just because of CrossThreading Error
Invoke(new MethodInvoker(delegate
{
this.lbl_status.Text = "Status : Waiting for Client";
}));
//Invoke Complete.
listner.Server.Listen(1);
client = listner.AcceptTcpClient();
if (client.Connected)
{
Invoke(new MethodInvoker(delegate
{
this.lbl_status.Text = "Status : Connected Successfully";
}));
//serverReceive();
}
}
catch (Exception ex)
{
//lbl_status.Text = "Status : " + ex.Message;
MessageBox.Show(ex.Message);
}
}
客户端代码为,
TcpClient tcpclient;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button btnConnectClick = FindViewById<Button>(Resource.Id.btnConnect);
btnConnectClick.Click += BtnConnectClick_Click;
}
private void BtnConnectClick_Click(object sender, System.EventArgs e)
{
Toast.MakeText(this, "Connection waiting", ToastLength.Long);
try
{
IPAddress ip = IPAddress.Parse(Resource.Id.ipadd.ToString()); //Ip from textbox
tcpclient.Connect(ip, 8126);
if (tcpclient.Connected)
{
Toast.MakeText(this, "Connected Successfully!", ToastLength.Long);
}
}
catch (System.Exception ex)
{
Toast.MakeText(this, ex.Message, ToastLength.Long);
}
}
我不知 Prop 体问题出在哪里!
最佳答案
我在 PC 站点上使用 WPF 应用程序作为服务器,在真实设备上使用 Xamarin.Android 应用程序作为客户端测试了您的代码。
在我身边工作正常。我在您的代码中发现的唯一问题是,在您的客户端,您没有初始化 tcpclient,当您编写 tcpclient.Connect 时, (但它不会抛出异常),我只是像这样修改了你的代码:tcpclient 为 null (ip, 8126);
IPAddress ip = IPAddress.Parse("..."); //Ip from textbox
tcpclient = new TcpClient();
tcpclient.ConnectAsync(ip, 8126);
您可以试试,如果还是不行,请确保您的设备在同一网络中。
关于c# - 无法通过 TCP 连接连接到 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45907665/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m