2011-08-21 112 views
0

我已经在Java中实现了一个简单的聊天程序。然而,当我运行的代码,并尝试从客户端发送邮件,我得到这个作为输出在服务器端客户端无法在java聊天程序中向服务器发送消息

例如:

客户:您好

服务器:ServerSocket的[地址= 0.0.0.0/0.0 .0.0,端口= 0,将localPort = 2000]

我得到任何消息我send..I我基本上是在本地主机上

工作这样的来自客户端的响应

谁能帮助解决我的问题?

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

请发布相关代码,以便我们可以开始帮助你解决问题。 – momo

+0

momo:我发布代码 –

+0

Parth_90,我在下面给出了答案。我认为你基本上只是把变量混合起来 – momo

回答

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等等,随着代码变大,会让你感到困惑。这也是一个很好的习惯,让其他工程师与你一起工作更快乐:)

1

没有显示任何代码,这是几乎不可能告诉。请张贴一些。

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

+0

我发布了代码..可以运行并告诉我为什么从服务器端发出问题,我可以正确发送消息,但是从客户端发送异常 –

+0

我我只使用127.0.0.1 ..我得到这个问题 –

相关问题