简体   繁体   中英

Sending data from a server to a client?

I have problem in this code that client could send message to server but it doesn't receive the message sent from server I've debugged the server code it does everything as well as it should be but client sided it does all the code until message=br.readLine() , it sticks on there

server code

    package server; 
    import java.io.BufferedReader;
    import java.io.EOFException;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    public class server {
    static Socket s;
    static ServerSocket ss;
    static InputStreamReader isr;
    static BufferedReader br;
    static String message;
    static PrintWriter pw;
    public server(){
try {
    ss=new ServerSocket(8080);
    while(true){
        s=ss.accept();
        isr=new InputStreamReader(s.getInputStream());
        br=new BufferedReader(isr);
        message=br.readLine();
        System.out.println(message);
        pw=new PrintWriter(s.getOutputStream(),true);
        pw.write("returned back from server to client");
        System.out.println("message sent");
        pw.flush();
        pw.close();
        isr.close();
        s.close();
    }
} catch (EOFException eofException) {
    // TODO Auto-generated catch block
    eofException.printStackTrace();
}catch(IOException e){
    e.printStackTrace();
}
 }
    public static void main(String[]args){
server s=new server();
  }
  }

client code

    package com.example.myapplication;
    import android.os.AsyncTask;
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;

    public class ClientSender extends AsyncTask<String,Void,Void> {
Socket s;
DataOutputStream dos;
InputStreamReader isr;
BufferedReader br;
PrintWriter pw;
@Override
protected Void doInBackground(String... voids) {
    String message = voids[0];
    try {
        System.out.println("1");

        s = new Socket("0.0.0.0", 8080);
        pw = new PrintWriter(s.getOutputStream());
        pw.write(message);
        pw.flush();
        pw.close();
        s.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        System.out.println("2");
        s = new Socket("0.0.0.0", 8080);
        System.out.println("connected to the server");
        isr = new InputStreamReader(s.getInputStream());
        System.out.println("3");
        br = new BufferedReader(isr);
        System.out.println("4");
        message = br.readLine();
        System.out.println(message);
        isr.close();
        s.close();
        System.out.println("5");
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}
}

I would suggest you to use Socket.IO instead of writting your own socket client. It's a well-tested and well-documented lib. This will make your life pretty much easier.

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