使用Socket的Android客户端和Java桌面通信

时间:2014-01-23 09:33:47

标签: java android sockets

我正在尝试在android中创建一个与Java Desktop Server进行通信(包括消息和文件)的应用程序。我正在使用Socket编程。我在网上找到了代码。当我尝试运行Android客户端显示UI时,应用程序中没有错误。我输入一个字符串并单击它显示的提交按钮"不幸的是应用程序已停止工作"这是我的源代码:

JAVA服务器编码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Main {

    private static ServerSocket serverSocket;
    private static Socket clientSocket;
    private static InputStreamReader inputStreamReader;
    private static BufferedReader bufferedReader;
    private static String message;
    public Main(){}
    public static void main(String[] args) {

        try {
            serverSocket = new ServerSocket(7575);  //Server socket

        } catch (IOException e) {
            System.out.println("Could not listen on port: 7575");
        }

        System.out.println("Server started. Listening to the port 7575");

        while (true) {
            try {

                clientSocket = serverSocket.accept();   //accept the client connection
                inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
                bufferedReader = new BufferedReader(inputStreamReader); //get the client message
                message = bufferedReader.readLine();

                System.out.println(message);
                inputStreamReader.close();
                clientSocket.close();

            } catch (IOException ex) {
                System.out.println("Problem in message reading");
            }
        }

    }
}

Android编码

activity_simple_client.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SimpleClientActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="83dp"
        android:ems="10"
        android:hint="@string/clientmsg" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="46dp"
        android:text="@string/instrut"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp"
        android:text="@string/send" />

    </RelativeLayout>

SimpleClientActivity.java

package com.andro.SimpleClient;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity; 
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SimpleClientActivity extends Activity {

private Socket client;
private PrintWriter printwriter;
private EditText textField;
private Button button; 
private String messsage;
public SimpleClientActivity(){}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_client);
textField = (EditText) findViewById(R.id.editText1); //reference to the text field
button = (Button) findViewById(R.id.button1);   //reference to the send button

//Button press event listener
button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

messsage = textField.getText().toString(); //get the text message on the text field
textField.setText("");      //Reset the text field to blank

try {

 client = new Socket("localhost", 7575);  //connect to server
 printwriter = new PrintWriter(client.getOutputStream(),true);
 printwriter.write(messsage);  //write the message to output stream

 printwriter.flush();
 printwriter.close();
 client.close();   //closing the connection

} catch (UnknownHostException e) {
 e.printStackTrace();
} catch (IOException e) {
 e.printStackTrace();
}
}
});

}
}

SimpleClient清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.andro.SimpleClient"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.andro.SimpleClient.SimpleClientActivity"
        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>

  </manifest>

我需要修改Eclipse中的任何设置吗? 谁能帮忙?提前致谢

我的LOCCAT文件

  01-25 00:51:42.035: W/System.err(920): java.net.SocketException: socket failed: EACCES (Permission denied)
  01-25 00:51:42.035: W/System.err(920):    at libcore.io.IoBridge.socket(IoBridge.java:576)
  01-25 00:51:42.095: W/System.err(920):    at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)
  01-25 00:51:42.095: W/System.err(920):    at java.net.Socket.startupSocket(Socket.java:560)
  01-25 00:51:42.105: W/System.err(920):    at java.net.Socket.tryAllAddresses(Socket.java:128)
  01-25 00:51:42.105: W/System.err(920):    at java.net.Socket.<init>(Socket.java:178)
  01-25 00:51:42.105: W/System.err(920):    at java.net.Socket.<init>(Socket.java:150)
  01-25 00:51:42.105: W/System.err(920):    at com.android.homeapp.Sendstring.run(Sendstring.java:17)
  01-25 00:51:42.105: W/System.err(920):    at java.lang.Thread.run(Thread.java:841)
  01-25 00:51:42.105: W/System.err(920): Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
  01-25 00:51:42.105: W/System.err(920):    at libcore.io.Posix.socket(Native Method)
  01-25 00:51:42.105: W/System.err(920):    at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
  01-25 00:51:42.125: W/System.err(920):    at libcore.io.IoBridge.socket(IoBridge.java:561)

  I changed my Mainactivity.java as follows but it still display "Application has Stopped" and in my logcat "MainThread doing too much of work"

MainActivity.java

   package com.andro.asynctask;
   import java.io.IOException;
   import java.io.PrintWriter;
   import java.net.Socket;
   import java.net.UnknownHostException;
   import android.os.AsyncTask;
   import android.os.Bundle;
   import android.app.Activity;
   import android.view.Menu;
   import android.widget.EditText;
   import android.widget.Button;
   import android.view.*;
   public class MainActivity extends Activity {
   private String mMsg;
   private Socket client;
   private PrintWriter writer;
   private Asyncclass ac;
   private EditText txt;
   //private Button btn;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   txt=(EditText)findViewById(R.id.editText1);
   //btn=(Button)findViewById(R.id.button1);
   }


   class Asyncclass extends AsyncTask<String,Void, String> 
   {
       public Asyncclass(String msg)
       {  
        mMsg=msg;
       }
    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        try
        {
        hardtask(); 
        }

        catch(IOException i)
        {
            i.printStackTrace();
        }
        return null;
    }
     public void hardtask() throws IOException,UnknownHostException
     {
            client=new Socket("localhost", 7575);
            writer=new PrintWriter(client.getOutputStream(),true);
            writer.write(mMsg);
            writer.flush();
            writer.close();
     }
        }
   public void OnClick(View view)
   {
   mMsg=txt.getText().toString();
   ac=new Asyncclass(mMsg);
   ac.execute();
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
   // Inflate the menu; this adds items to the action bar if it is present.
   getMenuInflater().inflate(R.menu.main, menu);
   return true;
   }
   }

2 个答案:

答案 0 :(得分:1)

您正尝试在主线程上进行网络通信,这在Android 4.0+上是不允许的。要解决崩溃问题,只需将代码移至另一个Thread(例如,您应该查看AsyncTask)。

你可以这样做:

private class SendMessage implements Runnable {
    private String mMsg;

    public SendMessage(String msg) {
        mMsg = msg;
    }

    public void run() {
        try {

            client = new Socket("localhost", 7575);  //connect to server
            printwriter = new PrintWriter(client.getOutputStream(),true);
            printwriter.write(messsage);  //write the message to output stream

            printwriter.flush();
            printwriter.close();
            client.close();   //closing the connection

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

然后将onClick更改为:

public void onClick(View v) {
    messsage = textField.getText().toString(); //get the text message on the text field
    textField.setText("");      //Reset the text field to blank

    new Thread(new SendMessage(message)).start();      // start a new thread to make the communication in the background
}

答案 1 :(得分:0)

java.net.SocketException: socket failed: EACCES (Permission denied)

你在Emulator中调试了吗?

client = new Socket("localhost", 7575);  //connect to server

如果您使用的是Android手机,请使用 10.0.0.2 并使用计算机IP地址,并确保已连接到wifi(可以连接到计算机IP)