Android上的客户端套接字问题

时间:2013-04-23 23:00:09

标签: android sockets client

    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;
    }


} 

所以这就是我,我可以刷出数据,但似乎无法从服务器接收它我是相当新的,并在过去从这个网站获得帮助。我真的被困住了,没时间了

任何帮助都会很棒

谢谢你

1 个答案:

答案 0 :(得分: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);