자바 서버-클라이언트 소켓통신 질문입니다.

조회수 631회

소켓통신 서버와 클라이언트 간의 양방향 통신을 구현하고 있는데, 서버에서 보내는 메세지는 클라이언트에 출력이 되지만 클라이언트에서 서버로 메세지를 보내면 출력되지 않습니다ㅠㅠ 어느 부분이 잘못된건지 모르겠습니다,, 도움 부탁드려요,,ㅠㅠ!!

<서버>

public class Server { ServerSocket serverSocket = null; //연결 수락 소켓 초기화

public static void main(String[] args) {
    OutputStream os;
    DataOutputStream dataos;

    Scanner sc = new Scanner(System.in);
    String msg = null;
    try {

        Server server = new Server();
        server.serverSocket = new ServerSocket(1117); //디폴트 생성자로 객체 생성
        //serverSocket.bind(new InetSocketAddress(1117)); //bind()메소드 호출 후 port번호 대입



        System.out.println("클라이언트 연결 요청 대기중....");


        Socket socket = server.serverSocket.accept(); //클라이언트 연결 수락
        System.out.println("연결되었습니다."); //연결 성공 메시지 출력

        os = socket.getOutputStream();
        dataos = new DataOutputStream(os);
        //Thread.sleep(100);
        dataos.writeUTF("Welcome to Chatting World!!"); //첫 화면 메시지 출력

            //메시지 보내기
            while(true) {
                msg = sc.nextLine(); //입력한 메세지 msg에 저장
                dataos.writeUTF(msg); //입력한 메세지 msg를 클라이언트로 보내기
                System.out.println("[Message to Client] : " + msg);
            }

    }catch(Exception e) {
}

}

class MessageListener extends Thread{ Socket socket;

OutputStream os;
DataOutputStream dataos;
InputStream is;
DataInputStream datais;

String msg =null;

MessageListener(Socket socket){
    this.socket = socket;
}

//메시지 받기
public void run() { try {

        is = this.socket.getInputStream();
        datais = new DataInputStream(is);

        System.out.println(this.socket.toString());


        while(true) {

            String msg = datais.readUTF();
            System.out.println("[Message from Client] :" + msg);
        }           
    }
    catch(IOException e) {
        e.printStackTrace();
    }
    }
}

}

<클라이언트>

public class Client {

Socket clientSocket = null;

public static void main(String[] args) {

    Client client = new Client();
    MessageListener msgListener = null;

    OutputStream os;
    InputStream is;
    String msg = null; //메세지 String 변수

    DataOutputStream dataos;
    DataInputStream datais;

    Scanner sc = new Scanner(System.in); //메시지 입력받기 위해 스캐너 선언


    //예외 발생 구간
    try { 
        System.out.println("서버에게 연결 요청중...."); //연결 요청 메시지 출력
        client.clientSocket = new Socket("192.168.25.30", 1117); //서버에게 연결 요청
        System.out.println("연결되었습니다."); //연결 성공 메시지 출력

        msgListener = new MessageListener(client.clientSocket);
        msgListener.start();


        os = client.clientSocket.getOutputStream();
        dataos = new DataOutputStream(os);
        Thread.sleep(100);
        dataos.writeUTF("Welcome to Chatting World!!"); //첫 화면 메시지 출력

         //메시지 보내기
                while(true) {
                    msg = sc.nextLine(); //입력한 메세지 msg에 저장
                    dataos.writeUTF(msg); //입력한 메세지 msg를 서버로 보내기
                    System.out.println("[Message to Server] : " + msg);
                }

    //예외처리
    }catch(Exception e) { 
    }
} 

}

class MessageListener extends Thread{ Socket socket;

InputStream is;
DataInputStream datais;

String msg =null;

MessageListener(Socket socket){
    this.socket = socket;
}


    //메시지 받기  
    public void run() {
        try {
            is = this.socket.getInputStream(); 
            datais = new DataInputStream(is);

            while(true) {
                msg = datais.readUTF(); //서버로부터 받은 메세지를 msg에 읽어오기
                System.out.println("[Message from Server] : " + msg); //받은 메세지 출력
            }
        }catch(IOException e) {

        }


    }

}

  • (•́ ✖ •̀)
    알 수 없는 사용자
  • 사버쪽에 MessageListener 클레스를 실행하지 않은거 아닌가요? 강지원 2020.8.18 20:13

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)