its eror: Basic Client Socket Programming Socket Connected Error msg: java.io.Fi
ID: 3918321 • Letter: I
Question
its eror:
Basic Client Socket Programming
Socket Connected
Error msg: java.io.FileNotFoundException: Hello (Böyle bir dosya ya da dizin yok)
Hash code generated: null
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
Exception in thread "main" java.lang.NullPointerException
at java.io.Writer.write(Writer.java:157)
at Udp.ClientSocket.main(ClientSocket.java:51)
pls help me why this code dont run
package Udp;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
// 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("localhost",7777);
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.doMD5("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
FileInputStream fin = new FileInputStream("hashedCode.txt");
Scanner sc = new Scanner(fin);
DataOutputStream out1 = new DataOutputStream(clientSocket.getOutputStream());
while (sc.hasNextLine()) {
String line = sc.nextLine();
out1.writeUTF(line);
}
sc.close();
out1.close();
// 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;
}
}
package Udp;
//Imports
import java.io.*;
import java.net.*;
public class serverSocket {
public static void main(String[] args) throws IOException {
// 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(7777);
} 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);
}
}
}
}
package Udp;
import java.io.FileInputStream;
import java.security.MessageDigest;
public class MD5 {
private static String checkSumValue;
public String getCheckSumValue() {
return checkSumValue;
}
public void setCheckSumValue(String checkSumValue) {
this.checkSumValue = checkSumValue;
}
public static 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;
}
}
Explanation / Answer
/*
* This is an open source project
* Anybody can view, download this project
* Authors : Ram Prasad Gudiwada
*/
package chegg.july;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.io.FileInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
*
* @author ram bablu
*/
public class Chegg394 {
}
/*
its eror:
Basic Client Socket Programming
Socket Connected
Error msg: java.io.FileNotFoundException: Hello (Böyle bir dosya ya da dizin yok)
Hash code generated: null
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
Exception in thread "main" java.lang.NullPointerException
at java.io.Writer.write(Writer.java:157)
at Udp.ClientSocket.main(ClientSocket.java:51)
pls help me why this code dont run
*/
//package Udp;
// Class Client Socket
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("localhost",7777);
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
// this is not necessary,
// if ((clientSocket == null) || (outputStream == null) || (inputStream == null)) {
// System.err.println("Socket Connection not successfull");
// return;
// } else {
// System.out.println("Socket Connected");
// }
System.out.println("Socket Connected");
// generate MD5 and send data
String hash = MD5.doMD5("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.
try (BufferedWriter out = new BufferedWriter(new FileWriter("hashedCode.txt"))) {
out.write(hash); // writes the hash code to the file
}
// Send the file to the server
FileInputStream fin = new FileInputStream("hashedCode.txt");
Scanner sc = new Scanner(fin);
DataOutputStream out1 = new DataOutputStream(clientSocket.getOutputStream());
while (sc.hasNextLine()) {
String line = sc.nextLine();
out1.writeUTF(line);
}
sc.close();
out1.close();
// 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);
}
}
}
class MD5 {
private static String checkSumValue;
public String getCheckSumValue() {
return checkSumValue;
}
public void setCheckSumValue(String checkSumValue) {
MD5.checkSumValue = checkSumValue;
}
public static 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();
// String builder is much efficient than stringbuffer
StringBuilder sb = new StringBuilder();
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 (NoSuchAlgorithmException | IOException ex) {
System.out.println("Error msg: " + ex);
}
return checkSumValue;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.