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);
}
}
}
}