Can anyone help to change java program into c program? thanks

This is a discussion on Can anyone help to change java program into c program? thanks within the Networking/Device Communication forums, part of the General Programming Boards category; Code: import java.io.*; import java.net.*; import java.util.*; public class JavaDNS { private final int TIMEOUT = 5000; boolean isRunning = ...

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    1

    Can anyone help to change java program into c program? thanks

    Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class JavaDNS {
        private final int TIMEOUT = 5000;
        boolean isRunning = true;
        DatagramSocket socket = null;
        MonitorThread monitorThread = null;
        long startTime = 0;
        long enquiryCount = 0;
        int downCount = 0;
        boolean silentMode = false;
        HashMap <String, String> lookupHash = new HashMap<String, String>();
        
        public static void main(String [] argc) {
            boolean silent = false;
            if (argc.length == 1 && argc[0].equals("silent"));
                silent = true;
                
            JavaDNS dns = new JavaDNS(silent);
            dns.start();
        }
    
        public JavaDNS(boolean silentMode) {
            startTime = System.currentTimeMillis();
            buildHash();
        }
        
        private void buildHash() {
            log("Build HashMap");
            
            int seg1, seg2, seg3, seg4;
            for (int i=1; i<=1000000; i++) {
                seg1 = 1;
                seg2 = (int) (i / 65535);
                seg3 = (int) (i / 255); 
                seg4 = i % 255;
                lookupHash.put(i + "abc.pepddns.com", seg1 + "." + seg2 + "." + seg3 + "." + seg4);
                if (i % 10000 == 0) {
                    log(i + " records hashed");
                }
            }
        }
        
        public void stop() {
            isRunning = false;
            try {
                if (socket != null && socket.isConnected()) {
                    socket.close();
                }
            } catch (Exception e) {
            }
        }
        
        private void bindSocket() throws SocketException {
            try {
                byte [] baddr = {0x0000, 0x0000, 0x0000, 0x0000};
                InetAddress addr = InetAddress.getByAddress(baddr);
                socket = new DatagramSocket(53, addr);
                
                log("-- Bind socket on port 53");
            } catch (Exception e) {
                e.printStackTrace();
                throw new SocketException(e.getMessage());
            }
        }
        
        public void start() {
            try {
                if (monitorThread == null || !monitorThread.isAlive() || !monitorThread.isRunning()) {
                    monitorThread = null;
                    monitorThread = new MonitorThread();
                    monitorThread.start();
                }
                
                if (socket == null) {
                    bindSocket();
                }
                
                byte[] buf = new byte[512];
                while (isRunning) {
                    DatagramPacket packet = new DatagramPacket(buf, buf.length);    
                    socket.receive(packet);
                    
                    ProcessThread processThread = new ProcessThread(socket, packet);
                    processThread.start();
                    
                    enquiryCount++;
                }
            } catch (Exception e) {    
                e.printStackTrace();
                downCount++;
                
                if (isRunning) {
                    start();
                }
            }
        }
        
        private class MonitorThread extends Thread {
            private boolean running = true;
            
            public MonitorThread() {
            }
            
            public void run() {
                try {
                    while (running) {
                        if (!silentMode) {
                            log("Health Check " + new java.util.Date());
                            log("-- Enquiry Count: " + enquiryCount);
                            log("-- Processing Thread: " + Thread.currentThread().getThreadGroup().activeCount());
                            log("-- Up Time: " + ((System.currentTimeMillis() - startTime) / 1000) + " seconds");
                            log("-- Down Count: " + downCount);
                        }
    
                        Thread.sleep(60000);
                    }
                } catch (Exception e){
                    running = false;
                }
            }
            
            public boolean isRunning() {
                return running;
            }
        }
        
        private class ProcessThread extends Thread {
            DatagramSocket socket;
            DatagramPacket packet;
            
            public ProcessThread(DatagramSocket socket, DatagramPacket packet) {
                this.socket = socket;
                this.packet = packet;
            }
            
            public void run() {
                try {
                    int offset = 12;  // Ignore first 12 bytes
            
                    StringBuffer sb = new StringBuffer();
                    byte[] queryData = packet.getData();
                    
                    int len = (int) queryData[offset];
                    while (len > 0) {
                        if (sb.length() > 0) {
                            sb.append(".");
                        }
                        sb.append(new String(queryData, offset+1, len));
                        offset += len + 1;
                        len = queryData[offset];
                    }
                
                    offset = packet.getLength();    
                    //log("Receive Query from " + packet.getAddress() + " for : " + sb.toString());
                    
                    queryData[2] = (byte) 0x0081; // Response Flag #1
                    queryData[3] = (byte) 0x0080; // Response Flag #2
                    queryData[7] = (byte) 0x0001; // It's a response
                    queryData[offset++] = (byte) 0x00c0;  // Name is a pointer
                    queryData[offset++] = (byte) 0x000c;  // Pointer is to the name at offset 0x000c
                    queryData[offset++] = (byte) 0x0000;  
                    queryData[offset++] = (byte) 0x0001;  // Answer is a type A query
                    queryData[offset++] = (byte) 0x0000;  
                    queryData[offset++] = (byte) 0x0001;  // Answer is clas IN
                    queryData[offset++] = (byte) 0x0000;
                    queryData[offset++] = (byte) 0x0000;
                    queryData[offset++] = (byte) 0x0002;  // Response is valid for 0x0258 (600 sec)
                    queryData[offset++] = (byte) 0x0058;
                    queryData[offset++] = (byte) 0x0000;
                    queryData[offset++] = (byte) 0x0004;  // Address is 4 bytes long
                    
                    // Return IP on below 
    
                    String ip = lookupHash.get(sb.toString());
                    if (ip == null)
                        ip = "1.2.3.4";
                    
                    String [] ipSec = ip.split("[.]");
                    queryData[offset++] = (byte) Integer.parseInt(ipSec[0]);  // Ip Sec 1
                    queryData[offset++] = (byte) Integer.parseInt(ipSec[1]);  // Ip Sec 2
                    queryData[offset++] = (byte) Integer.parseInt(ipSec[2]);  // Ip Sec 3
                    queryData[offset++] = (byte) Integer.parseInt(ipSec[3]);  // Ip Sec 4 
                    
                    packet.setData(queryData, 0, offset);
                    
                    socket.send(packet);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        
        private void log(String msg) {
            if (!silentMode)
                System.out.println(msg);
        }
    }

    Regards,
    Man Pak Hong, Dave
    manpakhong@hotmail.com

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    This isn't how it is done here. You do the work and post specific questions when you run into trouble.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help converting a java program to C
    By rpmischris in forum C Programming
    Replies: 8
    Last Post: 08-09-2012, 08:34 PM
  2. Change Program...
    By alias in forum C Programming
    Replies: 1
    Last Post: 02-27-2012, 10:50 PM
  3. Is it possible to call a java program in C?
    By xilum68 in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2003, 12:26 AM
  4. Simple java program!!!
    By Nicholas in forum C++ Programming
    Replies: 3
    Last Post: 05-09-2002, 03:57 PM
  5. C program interface to JAVA
    By Duane in forum C Programming
    Replies: 2
    Last Post: 10-25-2001, 07:52 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21