有人帮看一下吗,为什么这边请求不到数据,是读取的代码不能写在run里面吗,初学者,轻喷,谁能解决,给80个币 80C

我这边只有客户端代码,请求服务端,
服务端返回结构如下,已经能够发送,
最主要问题是打日志,
但是接收不了返回,返回结构如图:
图片说明

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;


public class Client extends Thread {
    Socket socket = null;
    public String result = "";
    public String realString = "";
    public BufferedReader RD = null;
    InputStream inputRealStream = null;
//  public static String in = "1003200172{\"trxZone\":\"00502\",\"programName\":\"e\",\"id\":\"abcd1234\",\"img1\":\"jhskagkfhdsklgjhaglhlkjdghlksdhflsdhfihoefrgih\",\"imgFace1\":\"50,60,100,120\",\"hack1\":1,\"customerAgreement\":1}";
    public String in = "";

    public Client(String host, int port, String in) {
        try {
            in = new String(in.getBytes(),"GBK");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        this.in = in;
        try {
            socket = new Socket(host, port);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

//    public void shutDown() throws IOException {
//        if (socket != null) {
//            synchronized (socket) {
//                socket.close();
//            }
//        }
//        System.out.println("StopClose.shutDown() complete");
//    }

    @Override
    public void run() {
        new sendMessThread().start();
        super.run();
        try {
            InputStream s = socket.getInputStream();
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = s.read(buf)) != -1) {
                System.out.println(new String(buf, 0, len));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    class sendMessThread extends Thread{
        @Override
        public void run() {
            super.run();
            Scanner scanner=null;
            OutputStream os= null;
            try {
                scanner=new Scanner(System.in);
                os= socket.getOutputStream();
//                String in;
//                do {
//                  in=scanner.next();
//                    os.write((""+in).getBytes());
//                    os.flush();
//                } while (!in.equals("bye"));
                for(int i =0;i<1;i++) {
                    os.write((""+in).getBytes());
                    os.flush(); 
                }

                BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream(),"GBK"));
                String str = "";
                InputStream inputStream = socket.getInputStream();
                System.out.println("RESULT AS FOLLOWING:");
                String realStr = "";
                while ((str = rd.readLine()) != null) {
                  System.out.println(str);
                  realStr = realStr + str;
                }

                setRealString(realStr);
                setRD(rd);

             // 4、关闭资源
                rd.close();
                os.close();
                socket.close();

//                str = rd.readLine();
                Client.this.result = str;
                System.out.println("Client.this.result:"+Client.this.result);
                rd.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            scanner.close();
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } 
    }
    public static void main(String[] args) {
//      Client clientTest=new Client("88.6.13.66", 13000);
//      Client clientTest=new Client("127.0.0.1", 1234,"");
//      clientTest.start();
    }

    public String getRealString() {
        return realString;
    }

    public void setRealString(String realString) {
        this.realString = realString;
    }

    public BufferedReader getRD() {
        return RD;
    }

    public void setRD(BufferedReader rD) {
        RD = rD;
    }

    public InputStream getInputRealStream() {
        return inputRealStream;
    }

    public void setInputRealStream(InputStream inputRealStream) {
        this.inputRealStream = inputRealStream;
    }

}

日志打出如下,为什么收不到值?
图片说明

2个回答

代码写的有点乱,一头雾水。首先,大致看了一下,应该是多线程导致导致的线程异步读取不到服务器返回数据。
你现在是客户端,不是服务器,你要明确的是:1、Socket建立连接;2、用 OutputStream os = socket.getOutputStream();来发送数据包,3、用户 InputStream is = socket.getInputStream();来读取服务器响应数据包。 你的内部类sendMessThread 没有必要去继承一个线程,每次都去开启一个新的线程去发送数据包给服务器.
因为你clientTest.run()方法在main跑起来的时候本身就是main方法的线程,你在发送的数据 又new sendMessThread()开启一个线程会导致线程异步,(你的代码ew sendMessThread().start()是注释掉的,那么就是没有发送数据给服务器,服务器又怎么响应你呢)

有可能是对方接口问题...........

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问