客户端无法在java聊天程序中向服务器发送消息

时间:2011-08-21 03:50:53

标签: java sockets client

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

例如:

客户:您好

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

我从客户端收到我发送的任何消息的响应。我基本上是在使用localhost

任何人都可以帮助解决我的问题吗?

我的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();
         }
       }

2 个答案:

答案 0 :(得分:1)

没有显示任何代码,几乎无法分辨。请发一些。

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

答案 1 :(得分: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等等,只会随着代码变大而让你感到困惑。拥有和让与你合作的其他工程师更快乐也是一个好习惯:)