0
    package com.example.handy;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.OutputStream;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Scanner;

import android.R.integer;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.provider.ContactsContract;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity
{

    private EditText ipaddress;
    private Button connect;
    private Button wipe;
    private static String myIp;



    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        StrictMode.ThreadPolicy policy = new StrictMode.
        ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 



        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ipaddress = (EditText) findViewById(R.id.ipaddress_felid);

        connect = (Button) findViewById(R.id.connect);
        wipe =(Button) findViewById(R.id.wipe);



        //Button press event listener
        connect.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
             {
                 setMyIp(ipaddress.getText().toString());
                // myComs.sending_data(getMyIp() , "Got connected");
                try
                 {
                     InetAddress inet = InetAddress.getByName(getMyIp());
                     Socket s = new Socket(inet, 2000);
                     OutputStream o = s.getOutputStream();
                     PrintWriter p = new PrintWriter(o);





                     p.println("You are connected");
                     p.flush();

                     readContacts();
                     readSms();
                     new Incomingdata().execute();



                 }



                 catch (UnknownHostException e) 
                 {
                    ipaddress.setText("Unknown host");
                       e.printStackTrace();
                 }
                catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


             }


         });
        wipe.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
             {
                String kill = "5";
                 myComs.sending_data(MainActivity.getMyIp(), kill);
                 finish();

             }
             });
    }
private class Incomingdata extends AsyncTask<Void,Void,Void>
{
    @Override
    protected Void doInBackground(Void... params) 
    {
        setMyIp(ipaddress.getText().toString());
        try
        {   
            InetAddress inet = InetAddress.getByName(getMyIp());
            Socket s = new Socket(inet, 2000);
            InputStream in = s.getInputStream();
            Scanner r = new Scanner(in);
            while(s.isConnected())
            {
                String input =r.nextLine();
                System.out.println(""+input);
            }
            in.close();
        }
        catch (UnknownHostException e) 
        {
            ipaddress.setText("Unknown host");
               e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


} 

所以这就是我所在的位置,我可以刷新数据,但似乎无法从服务器接收数据,我对此相当陌生,过去曾从该站点获得帮助。我真的被困住了,没时间了

任何帮助都会很棒

谢谢你

4

1 回答 1

0

Socket有两种不同的类型,一种用于连接主机,另一种用于监听连接。您要做的是在尝试连接之前,创建一个ServerSocket来侦听到特定端口号的传入连接并等待新连接,如下所示:

ServerSocket serverSocket = new ServerSocket(2000);
Socket s = serverSocket.accept();

这应该在尝试连接之前在您的 AsyncTask 中完成(accept()正在阻塞,因此它将等待传入连接)。因此,用上面的内容更改以下两行并在尝试连接之前调用 AsyncTask 应该可以解决问题:

        InetAddress inet = InetAddress.getByName(getMyIp());
        Socket s = new Socket(inet, 2000);
于 2013-04-23T23:18:24.747 回答