简体   繁体   English

从服务器向客户端发送数据?

[英]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 我在此代码中遇到问题,客户端可以将消息发送到服务器,但它没有收到从服务器发送的消息,我已经调试了服务器代码,它可以完成所有应有的工作,但是客户端可以完成所有代码,直到收到消息为止= br.readLine(),它粘在那里

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. 我建议您使用Socket.IO而不是编写自己的套接字客户端。 It's a well-tested and well-documented lib. 这是一个经过充分测试并有据可查的lib。 This will make your life pretty much easier. 这将使您的生活更加轻松。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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