【问题标题】:Client not able to send messages to server in java chat program客户端无法在 Java 聊天程序中向服务器发送消息
【发布时间】:2011-08-21 03:50:53
【问题描述】:

我用 Java 实现了一个简单的聊天程序。但是,当我运行代码并尝试从客户端发送消息时,我将其作为服务器端的输出

例如:

客户:嗨

服务器:ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=2000]

对于我发送的任何消息,我都会从客户端收到这样的响应。我基本上是在本地主机上工作

谁能帮助解决我的问题?

我的java代码:

         class Client
             {
             public static void main(String args[]) throws IOException
               {

           BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
           System.out.println("enter the key value");
           int key=Integer.parseInt(br.readLine());
           int random=(int)(Math.random()*50);
           System.out.println(random);
           int response=((int)random)%(key);
           System.out.println(key);
           System.out.println("response generated is "+response);
           System.out.println("Authentication begins");
           Socket echoSocket = new Socket("127.0.0.1", 2500);
           BufferedReader sin=new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
           PrintStream sout=new PrintStream(echoSocket.getOutputStream());
           BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
           PrintWriter out = null;
           String s;
           DataOutputStream clientout=new DataOutputStream(echoSocket.getOutputStream());
           clientout.writeInt(random);
           clientout.writeInt(key);
          clientout.writeInt(response);
          System.out.println("client is"+response);
          System.out.println("chat is started");

        while (true)
    {
        System.out.print("Client : ");
        s=stdin.readLine();
        sout.println(s);
        s=sin.readLine();
        System.out.print("Server : "+s+"\n");
        if ( s.equalsIgnoreCase("BYE") )
           break;
    }

            echoSocket.close();
            sin.close();
            sout.close();
            stdin.close();
         }
    }

          class Server
         {
            public static void main(String args[]) throws IOException
              {
              int random3=(int)(Math.random()*50);
              int response2;
              int response3;
              int random2;
              int key2;
              ServerSocket s= new ServerSocket(2500);
              Socket echoSocket=s.accept();
              DataInputStream clientin=new     DataInputStream(echoSocket.getInputStream());
              BufferedReader cin=new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
         PrintStream cout=new PrintStream(echoSocket.getOutputStream());
        BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
      String s1;
            random2=clientin.readInt();
            key2=clientin.readInt();
            response2=clientin.readInt();
            System.out.println(key2);
         response3=(random2)%(key2);
            System.out.println("server is"+response2);
            if(response2==response3)
           {
            System.out.println("client is authenticated..chat starts");
                 while (true)
       {
        s1=cin.readLine();
        if (s1.equalsIgnoreCase("END"))
        {
            cout.println("BYE");
                break;
         }
        System. out.print("Client : "+s+"\n");
        System.out.print("Server : ");
        s1=stdin.readLine();
        cout.println(s1);
       }
          }

             s.close();
               echoSocket.close();
               cin.close();
            cout.close();
            stdin.close();
         }
       }

【问题讨论】:

  • 请发布相关代码,以便我们开始帮助您解决问题。
  • momo : 我贴了代码
  • Parth_90,我把答案放在下面。我认为你基本上只是把变量弄混了

标签: java sockets client


【解决方案1】:

您得到输出是因为您使用了错误的变量。您应该从服务器打印的变量是 s1 而不是 s。

变量 s 指的是套接字,这就是为什么你得到的是套接字信息而不是客户端的响应


s1=cin.readLine();
if (s1.equalsIgnoreCase("END"))
{
    cout.println("BYE");
    break;
}

System. out.print("Client : "+s1+"\n"); // note that this should be s1 and not s

作为一个好习惯,您应该清楚地命名您的变量,以便您和其他人始终可以阅读代码。随着代码变得越来越大,拥有 s、s1 等只会让你感到困惑。拥有并让与您一起工作的其他工程师更快乐也是一个好习惯:)

【讨论】:

    【解决方案2】:

    如果不显示任何代码,几乎无法分辨。请发一些。

    但是,0.0.0.0 地址看起来很可疑。如果您正在使用localhost,请尝试使用127.0.0.1

    【讨论】:

    • 我发布了代码..你能运行并告诉我为什么问题来自服务器端我能够正确发送消息但从客户端异常来了
    • 我只使用 127.0.0.1 ..我仍然遇到问题
    猜你喜欢
    • 1970-01-01
    • 2021-03-23
    • 2016-07-23
    • 2014-09-15
    • 2020-01-06
    • 2021-09-23
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    相关资源
    最近更新 更多