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

用户名

我正在尝试在android中创建一个与Java桌面服务器通信的应用程序(消息和文件),为此我使用了套接字编程。我在网上找到了代码。当我尝试运行显示客户端的android客户端时,应用程序中没有错误。我输入了一个字符串,然后单击显示“不幸的是,应用程序已停止工作”的提交按钮,这是我的源代码:

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

您正在尝试在主线程上进行网络通信,这在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
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用网络服务发现在Java服务器和Android客户端之间进行通信

转到服务器和Java客户端通信

无法使用SSL在Java客户端和python服务器之间执行通信

使用Xamarin Android与客户端证书进行SSL通信

无法在 Android 客户端和 Node.js socket.io 服务器之间通信

Server(Python)-使用套接字的客户端(Java)通信

无法使用 socket.io 在 nodejs 服务器和客户端之间通信我的数据

Android Socket客户端无法发送和接收消息

在Android上使用socket.io客户端库

连接2个单独的节点进程和与客户端通信的socket.io的最佳方法

如何在Java服务器和JavaScript客户端之间使用套接字通信?

Spring Web Socket Java客户端

Java socket.io客户端

使用Web和Android客户端使用Java开发应用程序

使用Select的Socket客户端

使用socket.io和C#客户端与Compute Engine上的Node.js服务器代码进行通信

如何在Unity / C#服务器和Android / Java客户端之间创建Socket连接?

如何使用 socket.io 库区分客户端 TCP 套接字和客户端 websocket?

对 Java 客户端和 WebSphere MQ 使用 SSL 支持

MongoDB Java 客户端使用 $regex 和 $in

C ++客户端和Java服务器之间的通信

在Java服务器和PHP客户端之间进行通信

如何使用grpc在python服务器和php客户端之间进行通信?

使用javascript在服务器和客户端之间进行XML通信

无法滚动Android应用使用Java客户端Appium

Python 服务器和客户端无法通信

PHP WOPI主机和WOPI客户端通信

服务器和客户端之间的实时通信

服务器和客户端之间的持续通信