本文介绍了无法与Java中的服务器通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将服务器写入客户端程序,但无法与Java中的服务器进行通信.

I am trying to write server to client program but I cannot communicate with the server in Java.

这是我主体中的代码块.

Here is the code block in my main.

    InetAddress addr = InetAddress.getLocalHost();
    ipAddress = "78.162.206.164";
    ServerSocket serverSocket = new ServerSocket(0);
    String randomStringForPlayerName = RandomStringGenerator.generateRandomString();

    baseForReqOpp += ipAddress + " " + serverSocket + " " + randomStringForPlayerName;
    Socket socket = new Socket(host,2050);
    socket.setSoTimeout(100);

    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    out = new PrintWriter(socket.getOutputStream());
    out.write(baseForReqOpp);
    out.flush();

    System.out.println(in.read());

我知道服务器代码中没有问题,并且所有通讯端口都可以.

I know that there is no problem in the server code and all the communication ports are ok.

但是我无法从服务器读取任何内容.

But I cannot read anything from the server.

可能是什么问题?

推荐答案

您必须在输入流之前创建输出流

you have to create an output stream before the input stream

这篇关于无法与Java中的服务器通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 03:46