簡體   English   中英

Android客戶端和PC Server使用套接字進行通信

[英]Android Client and PC Server communication using sockets

我在同一台PC上的android模擬器和服務器(java)上運行客戶端,並且正在使用套接字編程。 下面附上代碼。 兩者都運行良好,但是實際的數據傳輸沒有發生,也無法弄清楚。

服務器端(PC):

import java.net.*;
import java.io.*;

public class Server_tcp {
    void run(){
        {
            try {
                System.out.println("In 1st try blck");
                try {
                    System.out.println("In 2nd try blck");
                    Boolean end = false;
                    ServerSocket ss = new ServerSocket(4444);
                    System.out.println("socket created");
                    while(!end){
                        //Server is waiting for client here, if needed
                        Socket s = ss.accept();
                        System.out.println("socket accepted");
                        BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
                        PrintWriter output = new PrintWriter(s.getOutputStream(),true); //Autoflush
                        String st = input.readLine();
                        System.out.println("Tcp Example" + "From client: "+st);
                        output.println("Good bye and thanks for all the fish :)");
                        s.close();
                        if (st==null){ end = true; }
                    }
                    ss.close();
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (IOException exp) {
                // TODO Auto-generated catch block
                exp.printStackTrace();
            }
        }
    }
    public static void main(String args[])
    {
        Server_tcp server = new Server_tcp();
        while(true){
            server.run();
        }
    }
}

客戶端(Android):

package com.try3;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;

import java.io.*;
import java.net.*;

public class ClientTCPActivity extends Activity {
/** Called when the activity is first created. */

    private EditText et;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        System.out.println("Before try net");
        trynet();
        System.out.println("after try net");
    }
    public void trynet() {
        System.out.println("inside try net");
        try {
            System.out.println("inside try");
            Socket s = new Socket("127.0.0.1",4444);

            //outgoing stream redirect to socket
            OutputStream out = (OutputStream) et.getContentDescription();

            PrintWriter output = new PrintWriter(out);
            output.println("Hello Android!");
            BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));

            //read line(s)
            String st = input.readLine();
            System.out.println(st);

            //Close connection
            s.close();

            System.out.println(" try ");
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

在Android模擬器中,您應該使用的是10.0.2.2而不是127.0.0.1的PC

我不明白您要在這里做什么。

OutputStream out = (OutputStream) et.getContentDescription();
PrintWriter output = new PrintWriter(out);

您的意思是:

PrintWriter output = new PrintWriter(s.getOutputStream(),true);

??

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM