简体   繁体   中英

Sending dummy data via a socket on android to java server

I'm trying to send some very simple data from my android device to a server on my laptop via a socket however no data is displayed. Here is the android part: (the data is sent whenever a press a button hence why an intent)

protected void onHandleIntent(Intent workIntent) {
    try {
        Socket my_socket = new Socket(ip, 5000);
        DataOutputStream st = new DataOutputStream(my_socket.getOutputStream());
        st.write(21);
        st.flush();
    }catch(Exception e){
        Log.v("ERROR","Something here went wrong" + e.getMessage());
    }
}

Here is the server code :

public static void main(String[] args){
    try{
        ServerSocket ss = new ServerSocket(5000);
        System.out.println("Waiting for a client");
        Socket s = ss.accept();
        System.out.println("Client connected");
        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String input_line;
        while((input_line = in.readLine()) != null){
            System.out.println(input_line);
        }
        s.close();
    }catch(Exception e){
        System.out.println(e.getMessage());
    }
}

I'm not sure if it's the android that doesn't send the data or the server that doesn't receive the data as I tried sending data to the java server using an erlang socket and no data was received.

Client should send more data to the end of line like '₩n' For server to catch the end of line...

How about adding PrinterWriter or BufferedWriter to make client more simple.. cause they have line related methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM