2011-08-21 5 views
0

자바로 간단한 채팅 프로그램을 구현했습니다. 내가 코드를 실행하고 클라이언트에서 메시지를 보내려고하지만 때 예를 들어 서버 측클라이언트가 자바 채팅 프로그램에서 서버로 메시지를 보낼 수 없습니다.

에 출력으로이 얻을 :

클라이언트 : 안녕하세요

서버 : ServerSocket의 [요지는 = 0.0.0.0/0.0 .0.0, 포트 = 0, localport = 2000]

내가 send..I 기본적으로 로컬 호스트

일하고있는 메시지 클라이언트에서 이러한 응답을

사람이 내 문제를 해결하는 데 도움이 수 있습니까?

내 자바 코드 :

  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입니다.

변수들 대신 좋은 방법으로 클라이언트의 응답


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 만 사용하고 있습니다. 문제가 발생합니다. –

관련 문제