简体   繁体   English

Android上的TCP / IP

[英]TCP/IP on Android

I have an android application which I want to send a simple command to my `public class ActivitymainActivity extends Activity { 我有一个Android应用程序,我想发送一个简单的命令到我的`公共类ActivitymainActivity extends Activity {

private TextView textview;
private Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activitymain);

    textview=(TextView) findViewById(R.id.EmailCount);
    button=(Button) findViewById(R.id.button1);
    textview.setText("Going in");
    try{
        Socket socket = new Socket("192.168.1.66", 2727);   

        OutputStream out = socket.getOutputStream();       
        PrintWriter output = new PrintWriter(out);         

        textview.setText("Sending Data to PC");         
        output.println("Hello from Android");
        output.flush();
        output.close();
        textview.setText("Data sent to PC");            

        socket.close();                                    
        textview.setText("Socket closed"); 
    }
    catch(Exception e){System.out.print(e+"shacso");}

    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            textview.setText("Trying to send");
             try{
                    Socket socket = new Socket("192.168.1.66", 2727);   

                    OutputStream out = socket.getOutputStream();       
                    PrintWriter output = new PrintWriter(out);         

                    textview.setText("Sending Data to PC");         
                    output.println("Checking Email now:D");
                    output.flush();
                    output.close();
                    textview.setText("Data sent to PC");            

                    socket.close();                                    
                    textview.setText("Socket closed");                  }
                catch(Exception e){System.out.print(e+"");}

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activitymain, menu);
    return true;
}

} ` }

And this is my Manifest: 这是我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.emailclient"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".ActivitymainActivity"
        android:label="@string/title_activity_activitymain" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

However, I am not able to receive anything on the server. 但是,我无法在服务器上收到任何内容。 Downloaded an application from play store called "UDP TCP Server" it send packets to a specified Ip and port and I was able to get the sent data to my server. 从Play商店下载了一个名为“UDP TCP Server”的应用程序,它将数据包发送到指定的Ip和端口,我能够将发送的数据发送到我的服务器。 Anything wrong with my code? 我的代码有什么问题吗?

Code explanation: Send data went app lunches and when a button on clicked 代码说明:发送数据进入app午餐,点击按钮时

Thanks :) 谢谢 :)

All Network Communications should be done in a separate Thread! 所有网络通信都应该在一个单独的线程中完成! Not in the main UI thread. 不在主UI线程中。 That is the reason your code is not working. 这就是您的代码无法正常工作的原因。

You say: 你说:

I am not able to receive anything on the server. 我无法在服务器上收到任何内容。

Looking at your code it seems that your not actually attempting to READ the response from the server. 看看你的代码,你似乎并没有真正尝试从服务器读取响应。 After sending the message you then need to access the socket InputStream to read the response from the server. 发送消息后,您需要访问套接字InputStream以从服务器读取响应。

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

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