English 中文(简体)
无法向服务器发送java聊天方案信息的客户
原标题:Client not able to send messages to server in java chat program

I have implemented a simple chat program in Java . However when I run the code and try to send messages from client I get this as output on Server side

例如:

客户:

服务器:服务器(addr=0.0.0.0/0.0.0.0,port=0, Localport=2000)

I get such a response from the client for any message I send..I am basically working on localhost

谁能帮助解决我的问题?

My 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+"
");
        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+"
");
        System.out.print("Server : ");
        s1=stdin.readLine();
        cout.println(s1);
       }
          }

             s.close();
               echoSocket.close();
               cin.close();
            cout.close();
            stdin.close();
         }
       }
最佳回答

你获得产出是因为你正在使用错误变量。 您从服务器上打印的变量是1,而不是1。

可变数指的是袖珍,这就是为什么你拿起袖珍,而不是客户的反应。


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

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

作为一项良好做法,你应明确列出你们的变量,以便你和其他人能够永远读到该守则。 既然法典越大,你就会被 confused惑。 拥有和培养与你合作的其他工程师也是一种好兆头:

问题回答

如果不显示任何法典,它就难以说出。 请设一些员额。

然而,地址0.0.0.0看起来可疑。 如果您不再使用<条码> 当地东道,可尝试<条码>127.0.0.1





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签