2014-05-17 3 views
-1

클라이언트 - 서버에 문제가 있습니다. 내가 서버를 실행할 때, 클라이언트 후에, 서버 쓰기 클라이언트가 서버에 연결되어 있지만 콘솔에 아무것도 쓸 수 있습니다. 누구든지 문제가있는 곳을 확인할 수 있습니까? 왜냐하면 나는 자바가 처음이에요. 나는 스캐너도 시도하지만 너무 효과가 없습니다.자바 서버와 클라이언트

서버 :

package ts_server; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.ServerSocket; 
import java.net.Socket; 

public class Server 
{ 
    private static final int PORT = 50000; 
    static boolean flaga = true; 

    private static ServerSocket serverSocket; 
    private static Socket clientSocket; 

    public static void main(String[] args) throws IOException 
    { 
     serverSocket = null; 
     try 
     { 
      serverSocket = new ServerSocket(PORT); 
     } 
     catch(IOException e) 
     { 
      System.err.println("Could not listen on port: "+PORT); 
      System.exit(1); 
     } 

     System.out.print("Waiting for connection..."); 

     Thread t = new Thread(new Runnable() 
     { 
      public void run() 
      { 
       try 
       { 
        while(flaga) 
        { 
         System.out.print("."); 
         Thread.sleep(1000); 
        } 
       } 
       catch(InterruptedException ie) 
       { 
        // 
       } 

       System.out.println("\nClient connected on port "+PORT); 
      } 
     }); 
     t.start(); 

     clientSocket = null; 
     try 
     { 
      clientSocket = serverSocket.accept(); 
      flaga = false; 
     } 
     catch(IOException e) 
     { 
      System.err.println("Accept failed."); 
      t.interrupt(); 
      System.exit(1); 
     } 

     final PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),true); 
     final BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 

     t = new Thread(new Runnable() 
     { 
      public void run() 
      { 
       try 
       { 
        Thread.sleep(5000); 

        while(true) 
        { 
         out.println("Ping"); 
         System.out.println(System.currentTimeMillis()+" Ping sent"); 

         String input = in.readLine(); 

         if(input.equals("Pong")) 
         { 
          System.out.println(System.currentTimeMillis()+" Pong received"); 
         } 
         else 
         { 
          System.out.println(System.currentTimeMillis()+" Wrong answer"); 

          out.close(); 
          in.close(); 
          clientSocket.close(); 
          serverSocket.close(); 
          break; 
         } 


         Thread.sleep(5000); 
        } 
       } 
       catch(Exception e) 
       { 
        System.err.println(System.currentTimeMillis()+" Unexpected Error"); 
       } 
      } 
     }); 
     t.start(); 
    } 
} 

클라이언트 :

package ts_client; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.Socket; 

public class Client 
{ 
    private static final int PORT = 50000; 
    private static final String HOST = "localhost"; 

    public static void main(String[] args) throws IOException 
    { 
     Socket socket = null; 

     try 
     { 
      socket = new Socket(HOST, PORT); 
     } 
     catch(Exception e) 
     { 
      System.err.println("Could not connect to "+HOST+":"+PORT); 
      System.exit(1); 
     } 

     final PrintWriter out = new PrintWriter(socket.getOutputStream(),true); 
     final BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

     Thread t = new Thread(new Runnable() 
     { 
      public void run() 
      { 
       long start = System.currentTimeMillis(); 

       while (true) 
       { 
        try 
        { 
         String input = in.readLine(); 

         if (input != null) 
         { 
          System.out.println(System.currentTimeMillis() + " Server: " + input); 
         } 

         if (input.equals("Ping")) 
         { 
          if(System.currentTimeMillis()-start>30000) 
          { 
           out.println("Pon g"); 
           System.out.println(System.currentTimeMillis() + " Client: Pon g"); 
           break; 
          } 

          out.println("Pong"); 
          System.out.println(System.currentTimeMillis() + " Client: Pong"); 
         } 
        } 
        catch (IOException ioe) 
        { 
         // 
        } 
       } 
      } 
     }); 
     t.start(); 

     out.close(); 
     in.close(); 
     socket.close(); 
    } 
} 
+0

정확히 무엇이 문제입니까? 이 코드가 기대하는 것과 그 대신에 무엇을 기대합니까? –

+0

많은 예외를 무시하고 있습니다. 그렇게하지 마십시오. 그것은 눈먼 사람과 함께 오토바이를 타는 것과 같습니다. 아주 안전한 일은 아닙니다. –

답변

2

하지는 클라이언트가 실행 될 때까지 스트림 소켓을 닫습니다 마십시오.

out.close(); 
in.close(); 
socket.close(); 
+0

멀티 스레딩에서 액세스되는'static boolean flaga = true;에'volatile'을 사용하십시오. – Braj

+0

고마워, 그게 문제 야 :) – user3084640

+0

전혀. 당신은 대부분 환영합니다 – Braj

관련 문제