Android应用通过套接字与服务器进行通信 [英] Android app communicating with server via sockets

查看:160
本文介绍了Android应用通过套接字与服务器进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林作出Android应用程序与服务器通信。当Android的手机收到短信,我希望它发送直通插座到我的服务器。

Im making an Android app that communicates with a server. When the Android-phone receives a SMS I want it to send it thru a socket to my server.

公共类服务器{

public static final int PORT = 8080;
public static void main(String[] args) throws IOException {
    ServerSocket s = new ServerSocket(PORT);
    try {
        Socket socket = s.accept();
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String str = in.readLine();
            System.out.println(str);
        } 
        finally {
            socket.close();
        }
    } 
    finally {
        s.close();
    }
} 

}

这是我的客户:

public Client(String message){

    try {
        Socket socket = new Socket("127.0.0.1", 8080);
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
        out.println(message);
        socket.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

我不是让林时调用从Android应用客户端类的任何连接。
任何人都知道如何解决这个问题?

Im not getting any connection when Im calling the Client class from the android app. Anyone know how to fix this problem?

类客户是从的onReceive(上下文的背景下,意图意图)当手机上收到消息{方法调用。

The class 'Client' is called from the onReceive(Context context, Intent intent){ method when a message is received on the phone.

推荐答案

我解决了它,忘记它添加到我的androidmanifest.xlm

I solved it, forgot to add this to my androidmanifest.xlm

< uses-permission android:name="android.permission.INTERNET">< /uses-permission>

这篇关于Android应用通过套接字与服务器进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆