-
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.
소켓 통신 직렬화 객체 추가 Related to : #3
- Loading branch information
Showing
3 changed files
with
143 additions
and
27 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,30 +1,83 @@ | ||
package Controller; | ||
|
||
import Controller.GameController; | ||
|
||
import java.io.*; | ||
import java.net.*; | ||
|
||
public class Client { | ||
public 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))) { | ||
import Exceptions.NotEmptySpaceException; | ||
import Exceptions.OnProgressException; | ||
|
||
public class Client implements Runnable{ | ||
|
||
|
||
private Socket curSocket; | ||
private ObjectOutputStream out; | ||
private ObjectInputStream in; | ||
private SerializeObject obj = null; | ||
|
||
private SerializeObject result = null; | ||
private boolean flag = true; | ||
|
||
// 서버에 객체 전달 | ||
public synchronized void returnObj (SerializeObject obj){ | ||
this.obj = obj; | ||
flag = false; | ||
while(!flag){ | ||
} | ||
} | ||
|
||
public SerializeObject getAnswer(){ | ||
return result; | ||
} | ||
|
||
System.out.println("서버에 연결되었습니다."); | ||
|
||
String userInput; | ||
|
||
while ((userInput = stdIn.readLine()) != null) { | ||
out.println(userInput); | ||
System.out.println("서버로 전송한 메시지: " + userInput); | ||
public synchronized void setFlag() { | ||
flag = true; | ||
notify(); // 메인 스레드에 flag 변경 알림 | ||
} | ||
|
||
public void connect(String address, int port, String name) throws UnknownHostException, NotEmptySpaceException, OnProgressException{ | ||
try { | ||
curSocket = new Socket(address, port); | ||
this.out = new ObjectOutputStream(curSocket.getOutputStream()); | ||
this.in = new ObjectInputStream(curSocket.getInputStream()); | ||
|
||
SerializeObject answer = (SerializeObject)in.readObject(); | ||
String answerStr = (String)answer.getEventObject(); | ||
returnObj(new SerializeObject("received", "String")); | ||
if(answerStr.equals("방이 가득찼습니다.")) { | ||
throw new NotEmptySpaceException(); | ||
} | ||
else if(answerStr.equals("현재 게임이 진행중입니다.")){ | ||
throw new OnProgressException(); | ||
} | ||
|
||
System.out.println("서버에 연결되었습니다."); | ||
|
||
out.writeObject(new SerializeObject(name, "String")); | ||
}catch (IOException e){ | ||
e.printStackTrace(); | ||
} catch (ClassNotFoundException e) { | ||
System.out.println("클래스가 맞지 않습니다."); | ||
} | ||
} | ||
|
||
String response = in.readLine(); | ||
System.out.println("서버로부터 받은 응답: " + response); | ||
@Override | ||
public void run() { | ||
try { | ||
while (obj != null) { | ||
out.writeObject(obj); | ||
result = (SerializeObject) in.readObject(); | ||
obj = null; | ||
setFlag(); | ||
} | ||
} catch (IOException e) { | ||
} catch (IOException | ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
try { | ||
curSocket.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package Controller; | ||
|
||
import java.io.Serializable; | ||
|
||
// 직렬화 객체 | ||
class SerializeObject implements Serializable { | ||
private Object eventObject; // 실제 객체를 포장하는 필드 | ||
private String objectType; // 객체의 유형을 나타내는 필드 | ||
|
||
public SerializeObject(Object eventObject, String objectType) { | ||
this.eventObject = eventObject; | ||
this.objectType = objectType; | ||
} | ||
|
||
public Object getEventObject() { | ||
return eventObject; | ||
} | ||
public String getObjectType() { | ||
return objectType; | ||
} | ||
} |
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