Hello everyone, I am from Asia android developers, would lik

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Hello everyone, I am from Asia android developers, would lik

Postby Android_Dev » Mon Jan 18, 2010 9:18 am

-------------------------------------------------------------------------------------------------------------------------------
大家好,不知道有人可以看懂中文,我在编码过程中遇到一个问题,我知道这里聚集着很多Android 的开发者高手,希望能获得你们的帮助。我开发的程序片段是,PC 向 Android发送信息。但是Android总是拒绝连接,到底是什么原因呢
-------------------------------------------------------------------------------------------------------------------------------

Hello everyone, I am from Asia, android developer, please forgive my bad English! Expressed his regret.
Would like to ask a question on Socket
PC: Server
Android: Client
I would like to send a message to mobile phones via computer. But I encountered such a problem
Analog phones has always refused to connect, I do not know what causes this problem arise, please understand why the developers to help me. Thanks!

Android:Server
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. public class SocketDemo extends Activity {
  5.  
  6.         /** Called when the activity is first created. */
  7.  
  8.         @Override
  9.  
  10.         public void onCreate(Bundle savedInstanceState) {
  11.  
  12.                 super.onCreate(savedInstanceState);
  13.  
  14.                 setContentView(R.layout.main);
  15.  
  16.                
  17.  
  18.                 new Thread(new Runnable() {
  19.  
  20.                         public void run() {
  21.  
  22.                                 try {
  23.  
  24.                                         ServerSocket socket = new ServerSocket(2587);
  25.  
  26.                                         Socket client = socket.accept();
  27.  
  28.  
  29.  
  30.                                         InputStream is = client.getInputStream();
  31.  
  32.                                         byte[] buffer = new byte[is.available()];
  33.  
  34.                                         is.read(buffer);
  35.  
  36.                                         String info = new String(buffer);
  37.  
  38.                                         Toast.makeText(SocketDemo.this, info, Toast.LENGTH_SHORT);
  39.  
  40.  
  41.  
  42.                                         is.close();
  43.  
  44.                                         socket.close();
  45.  
  46.                                         client.close();
  47.  
  48.  
  49.  
  50.                                 } catch (IOException e) {
  51.  
  52.                                         e.printStackTrace();
  53.  
  54.                                 }
  55.  
  56.  
  57.  
  58.                         }
  59.  
  60.                 });
  61.  
  62.         }
  63.  
  64. }
  65.  
  66.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4



Next, the client, the client is written in plain JavaProject

PC: Client
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         @Test
  3.  
  4.         public void sendDate() throws Exception {
  5.  
  6.                 Socket socket = new Socket("192.168.1.101",2587);
  7.  
  8.                 OutputStream os = socket.getOutputStream();
  9.  
  10.                
  11.  
  12.                 os.write("test".getBytes());
  13.  
  14.                
  15.  
  16.                 os.close();
  17.  
  18.                 socket.close();
  19.  
  20.                
  21.  
  22.         }
  23.  
  24. }
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


The problem is like this, mobile terminal is always refuses the connection, which in the end What is the reason for this?

Server Exception:
java.net.ConnectException: Connection refused: connect
Android_Dev
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Jan 18, 2010 8:48 am

Top

Postby kaifong » Fri Jan 29, 2010 6:19 am

Did you add the permission to the androidmanifest.xml?

ie.

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
kaifong
Freshman
Freshman
 
Posts: 6
Joined: Thu Jan 14, 2010 2:29 am
Location: Taiwan

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 3 guests