Vizible’s Blog

JmDNS : Service Register

Posted in Programming by wearetherock on October 23, 2008

โค้ดนี้เป็นตัวอย่างที่มาพร้อมกับ library อยู่ในโฟลเดอร์  example

package samples;

import java.io.IOException;
import javax.jmdns.JmDNS;
import javax.jmdns.ServiceInfo;

public class RegisterService {

    public static void main(String[] args) {

        try {
            System.out.println("Opening JmDNS");
            JmDNS jmdns = JmDNS.create();
            System.out.println("Opened JmDNS");
            System.out.println("\nPress r and Enter, to register HTML service 'foo'");
            int b;
            while ((b = System.in.read()) != -1 && (char) b != 'r');
            ServiceInfo info = ServiceInfo.create("_http._tcp.local.", "foo",
                                                   1268, 0, 0, "path=index.html");
                       
            jmdns.registerService(info);
            
            System.out.println("\nRegistered Service as "+info);
            System.out.println("Press q and Enter, to quit");
            //int b;
            while ((b = System.in.read()) != -1 && (char) b != 'q'); 
            System.out.println("Closing JmDNS");
            jmdns.close();
            System.out.println("Done");
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

วิธี Advertise Service, สร้าง ServiceInfo ขึ้นมาบอกว่า Service ของเรามีลักษณะอย่างไรบ้าง ชนิด(_http._tcp.local.), ชื่อ(foo), port (1268) จากนั้นเรียก jmdns.registerService(info) จบ

Advertisements
Tagged with: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: