-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Client, Server 소켓 클래스 추가 Related to : #2
- Loading branch information
Showing
6 changed files
with
219 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
import Controller.GameController; | ||
import Model.GameModel; | ||
import View.GameView; | ||
|
||
public class Application { | ||
public static void main(String[] args) { | ||
GameModel model = new GameModel(); | ||
GameView view = new GameView(); | ||
GameController controller = new GameController(model, view); | ||
GameController controller = new GameController(view); | ||
controller.start(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package Model; | ||
|
||
import Controller.GameController; | ||
|
||
import java.io.*; | ||
import java.net.*; | ||
|
||
public class Client { | ||
static void connect(String address, int port) throws UnknownHostException{ | ||
try (Socket socket = new Socket(address, port); | ||
PrintWriter out = new PrintWriter(socket.getOutputStream(), true); | ||
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); | ||
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) { | ||
|
||
System.out.println("서버에 연결되었습니다."); | ||
|
||
String userInput; | ||
|
||
while ((userInput = stdIn.readLine()) != null) { | ||
out.println(userInput); | ||
System.out.println("서버로 전송한 메시지: " + userInput); | ||
|
||
String response = in.readLine(); | ||
System.out.println("서버로부터 받은 응답: " + response); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package Model; | ||
|
||
import Controller.GameController; | ||
|
||
import java.io.*; | ||
import java.net.*; | ||
public class Server { | ||
|
||
|
||
static int clientCnt = 0; | ||
static int port; | ||
static ClientHandler[] handlers = new ClientHandler[GameController.MAX_PLAYER_COUNT]; | ||
public Server(int port) { | ||
this.port = port; | ||
openServer(); | ||
} | ||
|
||
static void openServer() { | ||
|
||
try (ServerSocket serverSocket = new ServerSocket(port)) { | ||
System.out.println("서버가 " + port + " 포트에서 대기 중..."); | ||
|
||
while (clientCnt < GameController.MAX_PLAYER_COUNT) { | ||
Socket clientSocket = serverSocket.accept(); | ||
clientCnt ++; | ||
System.out.println("클라이언트 연결됨: " + clientSocket.getInetAddress().getHostAddress()); | ||
|
||
// 클라이언트와 통신하는 스레드 생성 | ||
ClientHandler curHandler = new ClientHandler(clientSocket); | ||
handlers[clientCnt - 1] = curHandler; | ||
Thread clientThread = new Thread(curHandler); | ||
clientThread.start(); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
class ClientHandler implements Runnable { | ||
private Socket clientSocket; | ||
private PrintWriter out; | ||
private BufferedReader in; | ||
|
||
public ClientHandler(Socket socket) { | ||
this.clientSocket = socket; | ||
try { | ||
out = new PrintWriter(clientSocket.getOutputStream(), true); | ||
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void run() { | ||
String message; | ||
try { | ||
while ((message = in.readLine()) != null) { | ||
System.out.println("클라이언트로부터 받은 메시지: " + message); | ||
out.println("서버에서 응답: " + message); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
try { | ||
clientSocket.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters