套接字连接测试,不工作并从超级终端接收数据

时间:2014-06-06 07:15:49

标签: android sockets

  1. 我正在android eclipse中创建一个socket连接,用于接收数据,我首先需要测试连接,但页面没有在我的应用程序上打开,我使用listactivity为我的类。
  2. 我想从我的电脑(超级终端)接收数据,我已将硬件设备连接到我的电脑,其行为类似于路由器,我想通过Wifi将我的Android手机连接到路由器并在我的手机上接收数据,因为我输入超级终端。
  3. package com.exa.bullseye;

     import java.io.IOException;
     import java.net.InetAddress;
     import java.net.Socket;
     import java.net.UnknownHostException;
     import android.app.Activity;
     import android.content.Intent;
     import android.os.Bundle;
     import android.os.Handler;
     import android.text.Editable;
     import android.text.TextWatcher;
     import android.view.View;
     import android.widget.Button;
     import android.widget.EditText;
     import android.widget.ScrollView;
     import android.widget.TextView;
     import android.widget.Toast;
     import java.io.InputStream;
     import java.io.OutputStream;
    
    public class WifiReceive extends Activity {
        EditText ip;
        EditText s_port;
        Button connect;
    
        public void onCreate(Bundle paramBundle)
          {
            super.onCreate(paramBundle);
            setContentView(R.layout.wifireceive);
            ip = ((EditText)findViewById(R.id.ip));
            s_port = ((EditText)findViewById(R.id.port));
    
     connect.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
    
                    try {
                String address = ip.getText().toString();
    
    
                InetAddress addr = InetAddress.getByName(address);
                int port = Integer.parseInt(s_port.getText().toString());
    
                // Creates a stream socket and connects it to the specified port
                // number at the specified IP address. Blocks until the connection succeeds.
    
                Socket socket = new Socket(addr, port);
    
    
    
                Toast.makeText(getApplicationContext(), "Socket Connected",
                        Toast.LENGTH_LONG).show();
                socket.close();
    
            }
            catch (UnknownHostException e) {
                Toast.makeText(getApplicationContext(), "Host not found",
                           Toast.LENGTH_LONG).show();
            }
            catch (IOException ioe) {
                Toast.makeText(getApplicationContext(), "I/O Exception",
                           Toast.LENGTH_LONG).show();
            }   
    
        }
     });
          }
    }
    

0 个答案:

没有答案