Hi all, I have created a UDP program, client and server. What i'm trying to do is, hash a string, save the hasded code and the message into a .txt file and send it over to the server.

I have created the UDP program, MD5 hash codes and writing the output into a txt file. Cant seem to figure out how i could send the file over to my server. Can someone help me?

// Imports
import java.io.*;
import java.net.*;

// Class Client Socket
public class ClientSocket
{
    public static void main(String[] args) throws IOException
	{
		// show main Menu
        System.out.println("Basic Client Socket Programming");

		// Socket Variables
        Socket clientSocket = null;  			// for sending and receiving of data
        PrintWriter  outputStream = null;       // for sending data
        BufferedReader  inputStream = null;		// for receiving data

		// Create and open socket
		// Connection to 127.0.0.1, port num=2001
        try
		{
            clientSocket = new Socket("127.0.0.1",  2001);
            outputStream = new PrintWriter(clientSocket.getOutputStream(), true);
            inputStream = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        }
		catch (UnknownHostException e)
		{
            System.err.println("Don't know about host: 127.0.0.1");
			return;
		}
		catch (IOException e)
		{
            System.err.println("Couldn't get I/O for the connection to: 127.0.0.1");
			return;
        }

		// check if connection is successful
		if((clientSocket == null) || (outputStream == null) || (inputStream == null))
		{
			System.err.println("Socket Connection not successfull");
			return;
		}
		else
		{
			System.out.println("Socket Connected");
		}

        //generate MD5 and send data
        String hash = MD5.md5("Hello");
        System.out.println("Hash code generated: "+hash);
        //outputStream.println("Hello ||" + hash);

        //creates a new BufferWriter to write the hash value and the text message into the txt file.
        BufferedWriter out = new BufferedWriter(new FileWriter("hashedCode.txt"));
        out.write(hash); //writes the hash code to the file
        out.close(); //close the bufferreader

		//Send the file to the server
		

		// receiving data
		String rcvData;
		rcvData = inputStream.readLine();
		System.out.println(rcvData);

        // close connections
        try
        {
            outputStream.close();
            inputStream.close();
            clientSocket.close();
            System.out.println("Connection closed.");
        }
        catch (UnknownHostException e)
        {
            System.err.println("Trying to connect to unknown host: " + e);
        }
        catch (IOException e)
        {
            System.err.println("IOException:  " + e);
        }

        // exit program
		return;
	}
}
// Imports
import java.io.*;
import java.net.*;

public class serverSocket
{
    public static void main(String[] args) throws IOException
	{
		// show main Menu
        System.out.println("Basic Client Socket Programming");

		// Socket Variables
        ServerSocket serverSocket = null;  		// for listen for connection
        Socket  rcvSocket = null;               // for sending n rcving data
        DataOutputStream outputStream = null; 	// for sending data
        DataInputStream inputStream = null;		// for receiving data

        // try to open a socket to listen for incoming data
        // port number must match the one that the client is connecting to
        try
        {
           serverSocket = new ServerSocket(2001);
        }
        catch (IOException e)
        {
           System.err.println(e);
        }

        // creating a socket to rcv incoming data
        while (true)
        {
            try
            {
                System.out.println("Listening");
                rcvSocket = serverSocket.accept();
                System.out.println("Connected");
                PrintWriter out = new PrintWriter(rcvSocket.getOutputStream(), true);
                BufferedReader in = new BufferedReader(new InputStreamReader(rcvSocket.getInputStream()));

                // initiate conversation with client
                String rcvData = in.readLine();
                System.out.println("Rcv Data: " + rcvData);

                out.println(rcvData);
            }
            catch (IOException e)
            {
               System.err.println(e);
            }
        }
    }
}

Recommended Answers

All 2 Replies

while waiting, i tried around to do the codes to send a file. Still can't seem to get things to work.

On the server end, i managed to create a file "receivedHashed.txt" but nothing is written to the file.

Here is my new codes.

// Imports
import java.io.*;
import java.net.*;

// Class Client Socket
public class ClientSocket
{
    public static void main(String[] args) throws IOException
	{
		// show main Menu
        System.out.println("Basic Client Socket Programming");

		// Socket Variables
        Socket clientSocket = null;  			// for sending and receiving of data
        PrintWriter  outputStream = null;       // for sending data
        BufferedReader  inputStream = null;		// for receiving data

		BufferedInputStream bis;
		BufferedOutputStream bos;

		// Create and open socket
		// Connection to 127.0.0.1, port num=2001
        try
		{
            clientSocket = new Socket("127.0.0.1",  2001);
            outputStream = new PrintWriter(clientSocket.getOutputStream(), true);
            inputStream = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        }
		catch (UnknownHostException e)
		{
            System.err.println("Don't know about host: 127.0.0.1");
			return;
		}
		catch (IOException e)
		{
            System.err.println("Couldn't get I/O for the connection to: 127.0.0.1");
			return;
        }

		// check if connection is successful
		if((clientSocket == null) || (outputStream == null) || (inputStream == null))
		{
			System.err.println("Socket Connection not successfull");
			return;
		}
		else
		{
			System.out.println("Socket Connected");
		}

        //generate MD5 and send data
        String hash = MD5.md5("Hello");
        System.out.println("Hash code generated: "+hash);
        //outputStream.println("Hello ||" + hash);

        //creates a new BufferWriter to write the hash value and the text message into the txt file.
        BufferedWriter out = new BufferedWriter(new FileWriter("hashedCode.txt"));
        out.write(hash); //writes the hash code to the file
        out.close(); //close the bufferreader

		//Send the file to the server
		bis = new BufferedInputStream(new FileInputStream("hashedCode.txt"));
		bos = new BufferedOutputStream(clientSocket.getOutputStream());
		byte[] byteArray = new byte[8192];

		int in;
		while ((in = bis.read(byteArray)) != -1)
		{
			bos.write(byteArray,0,in);
		}


		// receiving data
		String rcvData;
		rcvData = inputStream.readLine();
		System.out.println(rcvData);

        // close connections
        try
        {
            outputStream.close();
            inputStream.close();
            bis.close();
            bos.close();
            clientSocket.close();
            System.out.println("Connection closed.");
        }
        catch (UnknownHostException e)
        {
            System.err.println("Trying to connect to unknown host: " + e);
        }
        catch (IOException e)
        {
            System.err.println("IOException:  " + e);
        }

        // exit program
		return;
	}
}
// Imports
import java.io.*;
import java.net.*;

public class serverSocket
{
    public static void main(String[] args) throws IOException
	{
		// show main Menu
        System.out.println("Basic Client Socket Programming");

		// Socket Variables
        ServerSocket serverSocket = null;  		// for listen for connection
        Socket  rcvSocket = null;               // for sending n rcving data
        DataOutputStream outputStream = null; 	// for sending data
        DataInputStream inputStream = null;		// for receiving data

		BufferedInputStream bis;
		BufferedOutputStream bos;
		int inInt;
		byte[] data;

        // try to open a socket to listen for incoming data
        // port number must match the one that the client is connecting to
        try
        {
           serverSocket = new ServerSocket(2001);
        }
        catch (IOException e)
        {
           System.err.println(e);
        }

        // creating a socket to rcv incoming data
        while (true)
        {
            try
            {
                System.out.println("Listening");
                rcvSocket = serverSocket.accept();
                System.out.println("Connected");
                PrintWriter out = new PrintWriter(rcvSocket.getOutputStream(), true);
                BufferedReader in = new BufferedReader(new InputStreamReader(rcvSocket.getInputStream()));

				/*
                // initiate conversation with client
                String rcvData = in.readLine();
                System.out.println("Rcv Data: " + rcvData);
				*/
			byte[] receivedData = new byte[8192];
			bis = new BufferedInputStream(rcvSocket.getInputStream());
			bos = new BufferedOutputStream(new FileOutputStream("receivedHashed.txt"));

			while ((inInt = bis.read(receivedData)) != -1)
			{
				bos.write(receivedData,0,inInt);
			}

			bos.flush();

                out.println("File received!");
            }
            catch (IOException e)
            {
               System.err.println(e);
            }
        }
    }
}

UDP file transfer can be done by using DatagramSockets and DatagramPacket.

Hope it helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.