package yeelone.socket;import android.app.Activity;
import android.os.Bundle;import java.io.IOException;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;import android.view.*;
import android.widget.Button;
import android.widget.TextView;public class activity extends Activity {
    /** Called when the activity is first created. */

private Button startButton =null;
private TextView textview=null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        startButton=(Button)findViewById(R.id.startbutton);
        startButton.setOnClickListener(new startListener());
        textview=(TextView)findViewById(R.id.textview);
        
    }
    
    class startListener implements Button.OnClickListener
      { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
new ServerThread().start();
}
    
    
     }
    class ServerThread extends Thread{
    
    
     public void run()
     {
     ServerSocket serverSocket=null;
     try{
     serverSocket=new ServerSocket(4444);
     Socket socket=serverSocket.accept();
    
     InputStream inputStream=socket.getInputStream();
     byte[] buffer =new byte[1024*4];
     int temp=0;
     while((temp=inputStream.read(buffer))!= -1)
     {
     System.out.println(new String(buffer,0,temp));
    
     }
    
     }catch(IOException e)
     {
     e.printStackTrace();
     }finally{
     try{
     serverSocket.close();
     }catch( IOException e)
     {
     e.printStackTrace();
     }
    
     }
     }
    
    }运行之后 点击按钮之后就出错,android显示强制关闭。
我实在找不出错在哪里,请哪位帮我看看。
    

解决方案 »

  1.   

    用DEBUG运行,然后用断点看看是在那个地方出的问题。
      

  2.   

    搞不好是忘加:<uses-permission android:name="android.permission.INTERNET"/> 了??
      

  3.   

    第一次点应该不会出错 后面点肯定出错  这个地方改下:
    ServerThread thread = null;
            public void onClick(View v) {
                // TODO Auto-generated method stub
              if(thread==null){
             thread=new ServerThread();
             thread.start();
             }
            }
    第一个线程没关  你又重新new了一个  肯定出错了
      

  4.   


    点击一次按钮没有崩溃了,但是如果再点击一次的话,就又崩溃。<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="yeelone.socket"
          android:versionCode="1"
          android:versionName="1.0">
        <uses-sdk android:minSdkVersion="4" />    <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".activity"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
      
        </application>
           <uses-permission android:name="android.permission.INTERNET"/>
    </manifest>
      

  5.   

    另外我想问一个问题,在编写SOCKET时,android这边是server端,所以在编写客户端时必须要知道服务器端的IP地址 才能进行通信吧?我要怎么知道我的android的IP??
    package yeelone.client;import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    import java.net.Socket;public class client
    {
    public static void main(String[] args){
    try{
    Socket socket=new Socket("192.168.1.144",4444);
    InputStream input=new FileInputStream("/home/yeelone/file.txt");
        OutputStream output=socket.getOutputStream();
        byte buffer[] =new byte[1024*4];
        int temp=0;
        while((temp=input.read(buffer)) != -1){
         output.write(buffer,0,temp);
    }
        output.flush();
    }catch(IOException e )
    {
    e.printStackTrace();
    }
    }
    }
    这里面的192.168.1.144是我把自己的主机配置成web服务器,在android上用浏览器访问我的PC机,然后用netstat查出当前哪个IP连接到我的PC查出来的。
    这个程序,当我在服务器端点击了按钮之后,正常来说应该 会进行监听状态,然后我运行了客户端,却没有得到我想要的效果 。你们能帮我看看吗?
      

  6.   

    android虚拟机的IP是127.0.0.1  要是想用android当服务器的话应该把虚拟机的IP和本机邦定下
      

  7.   

    http://code.google.com/p/fastdroid-vnc/
    这个网站你看一看  之前我邦定成功一回  这次怎么不好用了  你研究下
      

  8.   

    e.printStackTrace();
    不是有这样代码吗?先看DDMS的输出啊