Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
//client code
public class UDPSend {
DatagramSocket clientSocket;
    DatagramPacket sendPacket, receivePacket;
    private InputStream inStream = null;
    String checkSumValue;
    public void communicate() {
        byte[] sendData = new byte[1024];
        byte[] receiveData = new byte[1024];
        String path = "text1.txt", checkSumValue;
        try {
            FileReader fr = new FileReader("client.properties");
            Properties prop = new Properties();
            prop.load(fr);
            String serverHostname = prop.getProperty("ipAddress");
            int port = Integer.parseInt(prop.getProperty("port"));
            System.out.println("Host: " + serverHostname);
            System.out.println("Port: " + port);
//            BufferedReader inFromUser =
//                    new BufferedReader(new InputStreamReader(System.in));
            clientSocket = new DatagramSocket();
            InetAddress ipAddress = InetAddress.getByName(serverHostname);
            System.out.println("Attempting to connect to " + ipAddress + " via UDP " + port);
            checkSumValue = doMD5(path);
            sendData = checkSumValue.getBytes();
            sendPacket = new DatagramPacket(sendData, sendData.length, ipAddress, port);
            clientSocket.send(sendPacket);
            System.out.println("Client Checksum sent to server : " + new String(sendPacket.getData()));
//            int reply;
            receivePacket = new DatagramPacket(receiveData, receiveData.length);
            clientSocket.receive(receivePacket);
            String res =new String(receivePacket.getData());
//            reply=receivePacket.getData();
//             reply=Integer.parseInt(res);
            System.out.println("Reply: "+ res);

            if (res.equalsIgnoreCase("n")) {
            fileTransfer(path);
            clientSocket.close();
            }
            else{
            Thread.sleep(2000);
            clientSocket.close();
            }
        } catch (Exception ex) {
            System.out.println("Exception :" + ex.getMessage());
        }
    }

    public String doMD5(String path) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            FileInputStream fis = new FileInputStream(path);

            byte[] dataBytes = new byte[1024];

            int nread = 0;
            while ((nread = fis.read(dataBytes)) != -1) {
                md.update(dataBytes, 0, nread);
            };
            byte[] mdbytes = md.digest();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < mdbytes.length; i++) {
                sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
            }
            checkSumValue = sb.toString();
            System.out.println("Digest(in hex format):: " + checkSumValue);
        } catch (Exception ex) {
            System.out.println("Exception : " + ex.getMessage());
        }
        return checkSumValue;
    }
    public void fileTransfer(String path){
        try {
            FileInputStream fstream = new FileInputStream("text1.txt");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            File file = new File("text1.txt");
            FileInputStream fis = new FileInputStream(file);
            byte[] fsize = new byte[(int) file.length()];
            int size = fis.read(fsize);
            System.out.println("Size = " + size);
            InetAddress addr = InetAddress.getByName("localhost");
            byte[] buf = new byte[10000];
            String DataLine;
            while ((DataLine = br.readLine()) != null) {
//                DatagramPacket packet = new DatagramPacket(DataLine.getBytes(), DataLine.length(), addr, 4555);
                receivePacket=new DatagramPacket(DataLine.getBytes(), DataLine.length());
                System.out.println(DataLine);
                DatagramSocket socket = new DatagramSocket();
                socket.send(receivePacket);
                System.out.println("Sent Packet: "+new String(receivePacket.getData()));
            }
        } catch (Exception ex) {
            System.out.println("Exception in file: " + ex.getMessage());
        }
    }
}

Java
//server code
public class UDPReceive {

    String checkSumValue;
    DatagramSocket serverSocket;
    DatagramPacket receivePacket, sendpacket;
    private OutputStream ouStream = null;

    public void communicate() {
        try {
            String path = "text1.txt";
            FileReader fr = new FileReader("server.properties");
            Properties prop = new Properties();
            prop.load(fr);
            int port = Integer.parseInt(prop.getProperty("port"));
            serverSocket = new DatagramSocket(port);
            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];
            receivePacket = new DatagramPacket(receiveData, receiveData.length);
            serverSocket.receive(receivePacket);
            InetAddress IPAddress = receivePacket.getAddress();
            System.out.println("Waiting for datagram packet. . .\n");
            checkSumValue = new String(doMD5(path));
            String msg = new String(receivePacket.getData());
            System.out.println("Checksum of Client: " + msg);
            if (checkSumValue.equalsIgnoreCase(msg)) {
                System.out.println("The file didn't update");
                String reply = "y";
//                String r=new String(reply.);
//                ouStream.write(reply);
                sendData = reply.getBytes();
                sendpacket = new DatagramPacket(sendData, sendData.length);
                serverSocket.send(sendpacket);
                System.out.println("Reply :" + new String(sendData));
            } else {
                String reply = "n";
//                ouStream.write(reply);
                sendData = reply.getBytes();
                System.out.println("Reply :" + new String(sendData));
                sendpacket = new DatagramPacket(sendData, sendData.length);
                serverSocket.send(sendpacket);
                System.out.println("The file updated");
                fileTransfer(path);
            }
        } catch (Exception ex) {
            System.out.println("Error msg: " + ex.getMessage());
        }

    }

    public String doMD5(String path) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            FileInputStream fis = new FileInputStream(path);
            byte[] dataBytes = new byte[1024];
            int nread = 0;
            while ((nread = fis.read(dataBytes)) != -1) {
                md.update(dataBytes, 0, nread);
            };
            byte[] mdbytes = md.digest();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < mdbytes.length; i++) {
                sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
            }
            checkSumValue = sb.toString();
            System.out.println("Digest(in hex format):: " + checkSumValue);
        } catch (Exception ex) {
            System.out.println("Error msg: " + ex);
        }
        return checkSumValue;
    }

    public void fileTransfer(String path) {
        try {
            FileWriter fw = new FileWriter(new File(
                    "text1.txt"));
//            fw.write("hi");
//            DatagramSocket Socket = new DatagramSocket(port);
            byte[] receiveData = new byte[1000000];
            while (receiveData != null) {
                DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                serverSocket.receive(receivePacket);
                String sentence = new String(receivePacket.getData());
                fw.write(sentence.trim());
                fw.flush();
                System.out.printf("RECEIVED: %s ", new String(receivePacket.getData()));
            }

            fw.flush();
            fw.close();
            serverSocket.close();

        } catch (Exception e) {
            System.err.println(e);
        }
        }

In this,first client sends the checksum value of a file to server the server checks this checksum with server's file. If values of both differs then server will replace the client's file data by server's file. But here i'm getting Null exception. Anyone help me please. . . Thanks in advance
Posted
Updated 28-Jan-15 0:20am
v2
Comments
Richard MacCutchan 28-Jan-15 7:12am    
You need to explain more detail, and show exactly where you get the exception (client or server, line of code. variables?); most of this information can be obtained by using your debugger.

You should also understand that UDP is not a secure protocol for data transfer. you need to add your own checks to ensure you receive every packet, or switch to TCP which is secure.
[no name] 30-Jan-15 18:54pm    
You're going to have a tough time passing around one megabyte datagrams over UDP.

The while loop in fileTransfer makes no sense.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900