반응형

MulticastServer.java


import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;


public class MulticastServer {

	public static void main(String[] args) {
		
		
		try{
			MulticastSocket mul = new MulticastSocket(9000);
			
			mul.joinGroup(InetAddress.getByName("127.0.0.1"));
			
			while(true){
				
				byte msg [] = new byte [512];
				
				DatagramPacket dp = new DatagramPacket(msg, msg.length);
				
				mul.receive(dp);
				
				//System.out.println("보낸 곳 : " + dp.getAddress());
				
				System.out.println("보낸  메세지"+ new String(dp.getData()).trim());
				 
			}
			
		}catch(Exception e){
			System.out.println(e.getMessage());}

	}

}


MulticastClient.java


import java.net.*;
import java.util.*;

public class MulticastClient {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		// socket
		MulticastSocket ms = null;
		
		
		
		Scanner scanner = new Scanner(System.in);
		System.out.print("별명을 입력하세요 :");
		String in = scanner.next();
		InetAddress ia = null;
		
		while(true){
			
		System.out.print("송신할 데이터를 입력하세요 :");
		in = scanner.next();
		
		try{
			ms = new MulticastSocket(9999);
			
			ms.joinGroup(
			ia = InetAddress.getByName("230.100.50.5"));
			
			DatagramPacket dp = new DatagramPacket(in.getBytes(),in.getBytes().length,ia,9999);
			
			ms.send(dp);
			
			ms.close();
			
		}catch(Exception e){
			System.out.println(e.getMessage());
		}//try & catch

		} //While
		//scanner.close();
	}// Main
}



'JAVA > Java' 카테고리의 다른 글

[JAVA] UDP 를 사용한 파일 전송  (0) 2014.11.18
[JAVA] 자신의 ip 확인 및 IP 출력  (0) 2014.11.18
[JAVA] TCP Server, Client (Socket)  (0) 2014.11.18
[JAVA] UDP Server, Client 만들기 (Socket)  (1) 2014.11.18
[JAVA] swing Frame  (0) 2014.11.18

+ Recent posts