E001 // 클라이언트에서 연결 요청하면 서버는 네트워크 연결 초기화
E002 // 서버 데이터가 도착할 때까지 계속 대기
E003 // (수신전용스레드와 별개로) 메인 스레드 N41 이후 실행중
E004 // 버튼을 클릭할 때까지 계속 대기
E005 // 리모컨 전원버튼 클릭
E006 // 서버로 데이터([RMTC] [리모컨] POWER) 전송
E007 // 리모컨 우측버튼 클릭
E008 // 서버로 데이터([RMTC] [리모컨] RIGHT) 전송
E009 // 프로그램 종료버튼(우측상단 X) 클릭
E010 // 메인스레드와 수신전용스레드가 모두 종료하여 프로그램 종료
import javax.swing.*;
import java.awt.event.*;
import remoteControl.RemoteControlNetwork;
public class RemoteController
{
1 public static void main( String[] args ) {
final String imagePath = "C:\\Users\\user\\Downloads\\JAVA-main\\src\\remoteControl\\image\\";
String serverIP = "localhost";
2 RemoteControlNetwork panel =
3 new RemoteControlNetwork( imagePath, serverIP );
JFrame frame = new JFrame( "리모컨(클라이언트)" );
frame.getContentPane().add( panel );
frame.addWindowListener( new WindowAdapter() {
Wb public void windowClosing( WindowEvent event ) {
W1 panel.close();
We }
} );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
4 }
}
package remoteControl;
import java.awt.event.ActionEvent;
import network.Network;
public class RemoteControlNetwork extends RemoteControllerPanel implements Runnable
{
private Network network;
private String id;
private final String header = "[RMTC] ";
RN1b public RemoteControlNetwork( String imgPath, String serverIP ) {
RN11 super( imgPath );
RN12 connectAsClient( serverIP, this.getClass().getSimpleName() );
RN1e }
RN2b public RemoteControlNetwork( RemoteControl appliance, String serverIP ) {
RN21 super( appliance );
RN22 connectAsClient( serverIP, appliance.getClass().getSimpleName() );
RN2e }
RN3b public void connectAsClient( String serverIP, String applianceName ) {
id = "[" + applianceName +"]";
RN31 network =
RN32 new Network();
RN33 network.connectAsClient( serverIP, this );
RN3e }
@Override
RN4b public void actionPerformed( ActionEvent event ) {
String message = "";
if ( event.getSource() == button[POWER] )
message = header + "POWER";
else if ( event.getSource() == button[UP] )
message = header + "UP";
else if ( event.getSource() == button[DOWN] )
message = header + "DOWN";
else if ( event.getSource() == button[LEFT] )
message = header + "LEFT";
else if ( event.getSource() == button[RIGHT] )
message = header + "RIGHT";
RN41 network.write( message );
RN4e }
@Override
RN5b public void run(){
RN51 for ( String message = null; ( message =
RN52 network.read() )
!= null; ) {
if ( !message.contains( header ) )
continue;
else if ( message.contains( "POWER" ) && ( appliance != null ) )
RN53 appliance.clickPower();
else if ( message.contains( "UP" ) && ( appliance != null ) )
RN54 appliance.clickUp();
else if ( message.contains( "DOWN" ) && ( appliance != null ) )
RN55 appliance.clickDown();
else if ( message.contains( "LEFT" ) && ( appliance != null ) )
RN56 appliance.clickLeft();
else if ( message.contains( "RIGHT" ) && ( appliance != null ) )
RN57 appliance.clickRight();
RN58 }
RN5e }
RN6b public void close() {
network.write( header + id + " 네트워크 연결 종료" );
RN61 network.close();
RN6e }
}
package network;
import java.io.*;
import java.net.*;
public class Network
{
public static final int serverPort = 7700;
protected ServerSocket serverSocket;
protected Socket socket;
private BufferedReader in;
private PrintWriter out;
private Thread waitForCounterpart;
N1b public Network() {
serverSocket = null;
socket = null;
in = null;
out = null;
waitForCounterpart = null;
N1e }
N2b public void connectAsServer( Runnable obj ) {
try {
serverSocket = new ServerSocket( serverPort );
N21 socket =
N22 serverSocket.accept();
N23 connectInOut( obj );
} catch ( Exception e ) {
e.printStackTrace();
}
N2e }
N3b public void connectAsClient( String serverIP, Runnable obj ) {
try {
N31 socket =
N32 new Socket( serverIP, serverPort );
N33 connectInOut( obj );
} catch ( Exception e ) {
e.printStackTrace();
}
N3e }
N4b public void connectInOut( Runnable obj ) {
try {
in = new BufferedReader( new InputStreamReader( socket.getInputStream() ));
out = new PrintWriter( socket.getOutputStream(), true );
waitForCounterpart = new Thread( obj );
N41 waitForCounterpart.start();
} catch ( Exception e ) {
e.printStackTrace();
}
N4e }
N5b public String read() {
try {
if ( this.isConnecting() == true )
N51 return
N52 in.readLine();
} catch ( Exception e ) {
e.printStackTrace();
}
return null;
N5e }
N6b public void write( String data ) {
try {
if ( this.isConnecting() == true )
N61 out.println( data );
} catch ( Exception e ) {
e.printStackTrace();
}
N6e }
public boolean isConnecting() {
if ( ( socket != null )
&& ( in != null )
&& ( out != null )
&& ( waitForCounterpart != null )
&& ( waitForCounterpart.getState() != Thread.State.TERMINATED ) )
return true;
else
return false;
}
N7b public void close() {
try {
if ( waitForCounterpart != null ) {
waitForCounterpart.interrupt();
waitForCounterpart = null;
}
if ( in != null ) {
in.close();
in=null;
}
if ( out != null ) {
out.flush();
out.close();
out = null;
}
if ( socket != null ) {
socket.close();
socket = null;
}
if ( serverSocket != null ) {
serverSocket.close();
serverSocket = null;
}
} catch ( Exception e ) {
e.printStackTrace();
}
N7e }
}
package remoteControl;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class RemoteControllerPanel extends JPanel implements ActionListener
{
protected JButton[] button;
public final static int POWER = 0, UP = 1, DOWN = 2, LEFT = 3, RIGHT = 4;
protected RemoteControl appliance;
R1b public RemoteControllerPanel( String imgPath ) {
this.appliance = null;
final String[] strButton = { "power.gif", "up.gif", "down.gif", "left.gif", "right.gif" };
button = new JButton[strButton.length];
for ( int i = 0; i < strButton.length; i++ ) {
button[i] = new JButton( new ImageIcon( new ImageIcon( imgPath + strButton[i] ).getImage().getScaledInstance( 30, 30, Image.SCALE_SMOOTH ) ) );
button[i].addActionListener( this );
}
this.setPreferredSize( new Dimension( 240, 120 ) );
this.setLayout( new BorderLayout() );
this.add( button[POWER], BorderLayout.CENTER );
this.add( button[UP ], BorderLayout.NORTH );
this.add( button[DOWN ], BorderLayout.SOUTH );
this.add( button[LEFT ], BorderLayout.WEST );
this.add( button[RIGHT], BorderLayout.EAST );
R1e }
R2b public RemoteControllerPanel( String imgPath, RemoteControl appliance ) {
this( imgPath );
this.appliance = appliance;
R2e }
R3b public RemoteControllerPanel( RemoteControl appliance ) {
this.appliance = appliance;
R3e }
@Override
R4b public void actionPerformed( ActionEvent event ) {
if ( ( event.getSource() == button[POWER] ) && ( appliance != null ) )
appliance.clickPower();
else if ( ( event.getSource() == button[UP] ) && ( appliance != null ) )
appliance.clickUp();
else if ( ( event.getSource() == button[DOWN] ) && ( appliance != null ) )
appliance.clickDown();
else if ( ( event.getSource() == button[LEFT] ) && ( appliance != null ) )
appliance.clickLeft();
else if ( ( event.getSource() == button[RIGHT] ) && ( appliance != null ) )
appliance.clickRight();
R4e }
}
package remoteControl;
public interface RemoteControl
{
boolean ON = true, OFF = false;
void clickPower();
void clickUp();
void clickDown();
void clickLeft();
void clickRight();
}