简体   繁体   English

如何从手机发送TCP请求?

[英]How can I send TCP requests from my phone?

I have following task: to make Android application which connects to router, sends TCP requests for system and gets response. 我有以下任务:使Android应用程序连接到路由器,向系统发送TCP请求并获取响应。 I have made Android application already which uses PHP web-services, but I never make application for TCP requests. 我已经制作了使用PHP Web服务的Android应用程序,但从未为TCP请求制作应用程序。 Please, tell me, what means should I use for my task? 请告诉我,我应该使用什么方式执行任务? Thank you. 谢谢。

I guess what you are looking for is using Sockets to send raw data over the network. 我想您正在寻找的是使用套接字通过网络发送原始数据。 But its not clear what protocol you would be using. 但尚不清楚您将使用哪种协议。 For most of the cases you will have an existing library which will save your effort on low level socket communication. 对于大多数情况,您将拥有一个现有的库,该库将节省底层套接字通信的工作量。

In the example mentioned here , the relevant code to look at is 此处提到的示例中,要查看的相关代码是

         echoSocket = new Socket("remte-address", 80);
         out = new PrintWriter(echoSocket.getOutputStream(), true);

         out.println(userInput);

This is for writing to socket or sending a TCP request. 这用于写入套接字或发送TCP请求。 On the similar lines here is the code fragment for reading response 在类似的行上,这是读取响应的代码片段

          in = new BufferedReader(new InputStreamReader(
                                   echoSocket.getInputStream()));
           stdIn.readLine();

It will be useful if you look at the relevant socket javadoc . 如果您查看相关的套接字javadoc ,它将很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM