ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Java Communication] Server & Client
    Project 2010. 3. 9. 19:09

    스레드가 어려움
    몇시간을 해도 통신이 잘 안됨
    등등

    프로젝트를 진행하면서 이런 문제점이 생길거 같아서 미리 이클래스를 작성 하였습니다.

    현재로도 하나의 테스트 프로그램만을 작성해 보았으므로

    다른 프로그램에서는 어떻게 동작할지는 예상할 수 없습니다 ^^;;

    작성해본 간단한 채팅 프로그램의 서버와 클라이언트 입니다.

    이것만 보셔도 대충 이해가 가실거라고 생각 됩니다.

    --- Server ---

    package yhg.comm.test;
    
    import java.io.IOException;
    
    import yhg.comm.message.MSGChat;
    import yhg.comm.message.MSGEnterClient;
    import yhg.comm.message.MSGLeaveClient;
    import yhg.comm.message.Message;
    import yhg.comm.server.CommClientManager;
    import yhg.comm.server.CommServer;
    import yhg.comm.server.ICommClientManagerEvent;
    import yhg.comm.server.ICommServerEvent;
    
    public class ChatServer {
    	public static void main(String[] args){
    		try {
    			final CommServer server = new CommServer(1000);
    			
    			server.setClientManagerEvent(new ICommClientManagerEvent(){
    				public void onReceiveMessage(CommClientManager cm, Message msg) {
    					int sender = server.getClientNumber(cm);
    					MSGChat content = (MSGChat)msg;
    					
    					System.out.println("["+cm.getSocket().getLocalAddress().toString()+"] receive Message : "+msg);
    					
    					switch(msg.getType()){
    						case Message.type_MSGChat:
    							MSGChat chat = new MSGChat();
    							chat.setMessage("["+sender+"] "+content.getMessage());
    							
    							try {
    								server.sendAll(sender, chat);
    							} catch (IOException e) {
    								e.printStackTrace();
    							}
    							break;
    					}
    				}
    				
    				public void onSendMessage(CommClientManager cm, Message msg) {
    					
    				}
    			});
    			server.setServerEvent(new ICommServerEvent(){
    				public void onEnterClient(CommClientManager cm) {
    					int newClient = server.getClientNumber(cm);
    					MSGEnterClient ent = new MSGEnterClient();
    					ent.setNumber(newClient);
    					
    					try {
    						server.sendAll(newClient, ent);
    						System.out.println("["+cm.getLocalAddress().toString()+"] Connect");
    					} catch (IOException e) {
    						e.printStackTrace();
    					}
    				}
    
    				public void onLeaveClient(CommClientManager cm) {
    					int clientNum = server.getClientNumber(cm);
    					MSGLeaveClient ent = new MSGLeaveClient();
    					ent.setNumber(clientNum);
    					
    					try {
    						server.sendAll(clientNum, ent);
    						System.out.println("["+cm.getLocalAddress().toString()+"] Disconnect");
    					} catch (IOException e) {
    						e.printStackTrace();
    					}
    				}
    			});
    			
    			server.start();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    --- Client ---

    package yhg.comm.test;
    
    import java.io.IOException;
    import java.net.UnknownHostException;
    import java.util.NoSuchElementException;
    import java.util.Scanner;
    
    import yhg.comm.client.CommClient;
    import yhg.comm.client.ICommClientEvent;
    import yhg.comm.message.MSGChat;
    import yhg.comm.message.MSGEnterClient;
    import yhg.comm.message.MSGLeaveClient;
    import yhg.comm.message.Message;
    
    public class ChatClient {
    	public static void main(String[] args){
    		try {
    			CommClient client = new CommClient("127.0.0.1",1000);
    			client.setClientEvent(new ICommClientEvent(){
    				public void onReceiveMessage(Message msg) {
    					try{
    						switch(msg.getType()){
    							case Message.type_MSGEnterClient:
    								MSGEnterClient enterMsg = (MSGEnterClient)msg;
    								System.out.println("-- "+enterMsg.getNumber()+" 님이 입장 하였습니다 --");
    								break;
    							case Message.type_MSGLeaveClient:
    								MSGLeaveClient leaveMsg = (MSGLeaveClient)msg;
    								System.out.println("-- "+leaveMsg.getNumber()+" 님이 퇴장 하였습니다 --");
    							case Message.type_MSGChat:
    								MSGChat chat = (MSGChat)msg;
    								System.out.println(chat.getMessage());
    								break;
    						}
    					} catch(ClassCastException e){}
    				}
    
    				public void onSendMessage(Message msg) {
    					
    				}
    			});
    			client.start();
    			
    			Scanner scanner = new Scanner(System.in);
    			
    			while(true){
    				String input = scanner.next();
    				
    				if(!input.equals("")){
    					MSGChat chat = new MSGChat();
    					chat.setMessage(input);
    					client.send(chat);
    				}
    			}
    		} catch (UnknownHostException e) {
    		} catch (IOException e) {
    		} catch (NoSuchElementException e){
    		}
    	}
    }
    

    문제점이 있으시면 바로바로 알려주세요~

    댓글 0

Designed by Tistory.