2012-02-26 115 views
0

我尝试使用套接字开发客户端 - 服务器通信:android socket编程:客户端服务器通信?

package com.exercise.AndroidClient; 
import java.io.BufferedInputStream; 
import java.io.BufferedReader; 
import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.Socket; 
import java.net.UnknownHostException; 

//import com.app.Client.ClientActivity.ServerThread; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class AndroidClientActivity extends Activity { 

EditText textOut; 
TextView textIn; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    textOut = (EditText)findViewById(R.id.textout); 
    Button buttonSend = (Button)findViewById(R.id.send); 
    textIn = (TextView)findViewById(R.id.textin); 
    /* Runnable showmessage=new Runnable() 
    { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 


     } 

    };*/ 
    /*Thread thd=new Thread(new ServerThread()); 
     thd.start();*/ 
buttonSend.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     DataOutputStream dataOutputStream = null; 
     try { 
      Socket socket = new Socket("192.168.1.9", 70); 
      dataOutputStream = new DataOutputStream(socket.getOutputStream()); 
     } catch (IOException e2) { 
      // TODO Auto-generated catch block 
      e2.printStackTrace(); 
     } 
     try { 

       dataOutputStream.writeUTF(textOut.getText().toString()); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
    } 
}); 
Thread thd=new Thread(new ServerThread()); 
    thd.start(); 
    /*buttonSend.setOnClickListener(buttonSendOnClickListener) 
Button.OnClickListener buttonSendOnClickListener 
= new Button.OnClickListener(){ 

@Override 
public void onClick(View arg0) { 
// TODO Auto-generated method stub 

} 
} */ 
} 
      public class ServerThread implements Runnable{ 
       @Override 
       public void run() { 
        Socket socket = null; 
        DataOutputStream dataOutputStream = null; 
        DataInputStream dataInputStream = null; 
        BufferedReader in=null; 

        try { 
        socket = new Socket("192.168.1.9", 70); 
        InputStream inputStream = socket.getInputStream(); 
       // DataInputStream in1 = new DataInputStream(inputStream); 
        char chstr[]=new char[512]; 
        //textIn.setText("rafa2"); 
        // DataInputStream inq=new DataInputStream(new BufferedInputStream(inputStream)); 
       in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

        textIn.setText("rafa1"+in.read(chstr)); 
        // textIn.setText("ramla"+socket+inq.readUTF()); 
        }catch (UnknownHostException e) { 
         System.err.println("Don't know about host: taranis."); 
         System.exit(1); 
        } catch (IOException e) { 
         System.err.println("Couldn't get I/O for " 
              + "the connection to: taranis."); 
         System.exit(1); 
        } 



        // dataInputStream = new DataInputStream(socket.getInputStream()); 

        // BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

       /*  BufferedReader stdIn = null; 
        try { 
          socket = new Socket("192.168.110.49", 4023); 
         stdIn = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
         textIn.append("gggg"+stdIn.readLine()); 
         String userInput; 
         while ((userInput =stdIn.readLine()) != null) { 
           // out.println(userInput); 
           // System.out.println("echo: " + in.readLine()); 
           textIn.append("rafa"); 
           textIn.append(userInput+stdIn.readLine()); 
        }} catch (IOException e1) { 
         // TODO Auto-generated catch block 

         e1.printStackTrace(); 
        } 
         String userInput = null; 
         /*textIn.append("rafa1"); 
         try { 
          textIn.append("jjjj"+stdIn.readLine()); 
         } catch (IOException e1) { 
          // TODO Auto-generated catch block 
          e1.printStackTrace(); 
         }*/ 
       /*  try { 
          String userInput; 
          while ((userInput =in1.readLine()) != null) { 
           // out.println(userInput); 
           // System.out.println("echo: " + in.readLine()); 
           textIn.append("rafa"); 
           textIn.append(userInput+in.readLine()); 

          } 

         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         }*/ 



       } 
      } 

} 

这里我的问题:为什么客户端无法从服务器接收消息?它接近力量。代码中的错误是什么?它正确地向服务器发送消息。我使用“Hercules”软件作为服务器。

下面是错误的logcat的:

02-26 12:34:35.956: E/AndroidRuntime(679): FATAL EXCEPTION: Thread-8 
02-26 12:34:35.956: E/AndroidRuntime(679): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
02-26 12:34:35.956: E/AndroidRuntime(679): at android.view.ViewRoot.checkThread(ViewRoot.java:2802) 
02-26 12:34:35.956: E/AndroidRuntime(679): at android.view.ViewRoot.invalidateChild(ViewRoot.java:607) 
02-26 12:34:35.956: E/AndroidRuntime(679): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633) 
02-26 12:34:35.956: E/AndroidRuntime(679): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505) 
02-26 12:34:35.956: E/AndroidRuntime(679): at android.view.View.invalidate(View.java:5139) 
02-26 12:34:35.956: E/AndroidRuntime(679): at android.widget.TextView.checkForRelayout(TextView.java:5364) 
02-26 12:34:35.956: E/AndroidRuntime(679): at android.widget.TextView.setText(TextView.java:2688) 
02-26 12:34:35.956: E/AndroidRuntime(679): at android.widget.TextView.setText(TextView.java:2556) 
02-26 12:34:35.956: E/AndroidRuntime(679): at android.widget.TextView.setText(TextView.java:2531) 
02-26 12:34:35.956: E/AndroidRuntime(679): at com.exercise.AndroidClient.AndroidClientActivity$ServerThread.run(AndroidClientActivity.java:101) 
02-26 12:34:35.956: E/AndroidRuntime(679): at java.lang.Thread.run(Thread.java:1096) 
02-26 12:34:36.426: W/IInputConnectionWrapper(679): showStatusIcon on inactive InputConnection 
+0

你甚至看过错误信息吗?您试图从UI线程以外的线程修改视图(textIn)... – claesv 2012-02-26 16:04:12

+0

作为个人建议,我建议您在继续项目之前先看看Netty项目。 – jsaye 2012-02-26 17:52:58

回答

0

我一直在看你的代码,我可以告诉你一些提示/东西可以给你造成的问题。

  • 首先您使用的是Hercules。我不知道Hercules是如何实现的,但是您必须确保服务器按照您的期望向您发送数据,相同的编码,同一时间......这意味着您必须使用InputStreamReader中的适当方法,当你想读。

  • 其次,CPU会比任何互联网连接速度更快,这 意味着你将不得不等待,直到你有一些数据,做 是这样的:

while(socket.getInputStream().available()!=0){}

的方法avilable()返回准备读取的估计字节数;即使您只应使用此方法来回答问题“是否确实有数据准备好?”。

我希望,这有点帮助。