code to send data to server
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);
// receiving from server ( receiveRead object)
InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
String receiveMessage, sendMessage;
while(true)
{
sendMessage = keyRead.readLine(); // keyboard reading
String enc = crypt.encrypt(sendMessage, serverPublicKey);
System.out.println("sending to server: "+enc);
pwrite.println(enc); // sending to server
pwrite.flush(); // flush the data
if((receiveMessage = receiveRead.readLine()) != null) //receive from server
{
//System.out.println(crypt.decrypt(receiveMessage, clientPrivateKey)); // displaying at DOS prompt
System.out.println(receiveMessage);
}
}
output after encryption looks like below on console
sending to server: YRJ7ZNgqSQ56nGc8ff7ktoybYEohQJS2R+Vh3YN1YfHipUS64MyFrrYAzL4CiTPv2WF7zvaJst1A
qsiPsv3/1Q==
code to receive on server
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
// sending to client (pwrite object)
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);
// receiving from server ( receiveRead object)
InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
String receiveMessage, sendMessage;
while(true)
{
if((receiveMessage = receiveRead.readLine()) != null)
{
//System.out.println(crypt.decrypt(receiveMessage, serverPrivateKey));
System.out.println(receiveMessage);
}
sendMessage = keyRead.readLine();
String enc = crypt.encrypt(sendMessage, clientPublicKey);
System.out.println("sending to clinet: "+enc);
pwrite.println(enc);
pwrite.flush();
}
but data is received like
YRJ7ZNgqSQ56nGc8ff7ktoybYEohQJS2R+Vh3YN1YfHipUS64MyFrrYAzL4CiTPv2WF7zvaJst1A
rest of
qsiPsv3/1Q==
is received when i send something from server to client, please help me locate the problem, due to truncated data the decryption fails
You do not only encrypt the input but you also Base64 encode the encrypted bytes. Your Base64 encoder inserts line breaks every 76 characters, which is the standard for Base64 transfer encoding for MIME (RFC 2045). That is why on the server side your readLine() only reads in the first 76 characters.
You need to configure your Base64 encoder to not add line breaks.
Related
The client sends data (string) to the server, and the server must read it, but in my case the server didn't read the data (value) that the client sent, and I didn't know where is the problem exactly, because normally the steps to read data are all correct in the server side
Client side:
Socket socket = new Socket(address, authenticationServerPort);
username = username + "\n"; // to send username through socket without
String h=getUserInput();
// waiting
// Send the message to the server
// send public key
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
System.out.println(h);
bw.write(h);
bw.flush();
System.out.println("Message sent to the Authentication server : "+ h);
Server side:
Socket clientSocket = null;
try {
System.out.println("Server Running");
int serverPort = 8029; // the server port we are using
ServerSocket listenSocket = new ServerSocket(serverPort);
List<BlockChain> resultList = new ArrayList<BlockChain>();
while (true) {
clientSocket = listenSocket.accept();
InputStream is = clientSocket.getInputStream();
System.out.println(is);
InputStreamReader isr = new InputStreamReader(is);
System.out.println(isr);
BufferedReader br = new BufferedReader(isr);
String request = br.readLine();
System.out.println("the msg receving from client is : "+request);
PrintWriter out;
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream())));
if (clientSocket != null) {
clientSocket.close();
}
}
catch (Exception e) {
e.printStackTrace();// TODO: handle exception
}
}
Someone tell me where is the problem exactly.
I'm having issues with this question right now.
For this question, I have to:
Client side: compress each input line from the console, send it to the server and decompress each message from server
Server side: decompress data from client, change the lower-case letters to upper case, compress it and send back to the client
The best I can do is do everything above with only ONE line.
Client Side:
/* SOCKET CONNECTING STUFF UP HERE */
/*PROBLEMS START AROUND HERE */
String line;
BufferedReader bis = new BufferedReader(new InputStreamReader(System.in));
DeflaterOutputStream compress = new DeflaterOutputStream(socket.getOutputStream(), true);
InflaterInputStream decompress = new InflaterInputStream(socket.getInputStream());
BufferedReader fromClient = new BufferedReader(new InputStreamReader(decompress));
line = bis.readLine();
line = line + "\n";
compress.write(line.getBytes(), 0, line.length());
compress.finish();
System.out.println("Message sent: " + line);
System.out.println("Message Returned : " +fromClient.readLine());
/* closing the streams here */
bis.close();
decompress.close();
compress.close();
fromClient.close();
socket.close();
}
}
Server Side:
String line = "";
OutputStream outstream = new FileOutputStream("compessserver.txt");
InflaterInputStream decompress = new InflaterInputStream(clientsocket.getInputStream());
BufferedReader fromClient = new BufferedReader(new InputStreamReader(decompress));
DeflaterOutputStream compress = new DeflaterOutputStream(clientsocket.getOutputStream());
while ((line = fromClient.readLine()) != null) {
String upperLine = line.toUpperCase();
System.out.println("Message received and converted: " + upperLine);
System.out.println();
upperLine = upperLine + "\n";
byte[] input = upperLine.getBytes();
outstream.write(input);
outstream.write("\r\n".getBytes());
compress.write(input);
System.out.println("Message returned : " + upperLine);
compress.finish();
if (upperLine.equalsIgnoreCase("x")) {
break;
}
}
decompress.close();
compress.close();
fromClient.close();
outstream.close();
socket.close();
}
}
I really need help in this please. If I try to make this multiple inputs instead, the whole code just messes up. Been at this for days.
EDIT: Forgot to mention this. What I'm supposed to do is input a line, compress it, send to server, server decompress it and upper case letters, compress it, send back to client. And then I supposed to input more lines until I put in a single letter like "Q" which in case, ends the program
I tried the following code to make it work for multiple lines
Second Try Client Side:
String line;
BufferedReader bis = new BufferedReader(new InputStreamReader(System.in));
DeflaterOutputStream compress = new DeflaterOutputStream(socket.getOutputStream(), true);
InflaterInputStream decompress = new InflaterInputStream(socket.getInputStream());
BufferedReader fromClient = new BufferedReader(new InputStreamReader(decompress));
line = bis.readLine();
while ((!line.equalsIgnoreCase("x"))) {
compress.write(line.getBytes(), 0, line.length());
System.out.println("Message sent: " + line);
System.out.println("Message returned:" +fromClient.readLine() );
line = bis.readLine();
}
bis.close();
fromClient.close();
socket.close();
}
}
Second Try Server Side:
OutputStream outstream = new FileOutputStream("compessserver.txt");
InflaterInputStream decompress = new InflaterInputStream(clientsocket.getInputStream());
BufferedReader fromClient = new BufferedReader(new InputStreamReader(decompress));
DeflaterOutputStream compress = new DeflaterOutputStream(clientsocket.getOutputStream());
while ((line = fromClient.readLine()) != null) {
String upperLine = line.toUpperCase();
System.out.println("Message received and converted: " + upperLine);
System.out.println();
upperLine = upperLine + "\n";
byte[] input = upperLine.getBytes();
outstream.write(input);
outstream.write("\r\n".getBytes());
compress.write(input);
System.out.println("Message returned : " + upperLine);
if (upperLine.equalsIgnoreCase("x")) {
break;
}
}
decompress.close();
fromClient.close();
outstream.close();
socket.close();
}
}
You can't use these streams interactively. You would have to call finish() after every write, or rather before every read, which means you could only do one write. They are designed for large one-way streams, not interactive request/response sessions.
In any case there is no advantage to be gained from compressing single lines. You need a lot of data for compression to start working.
NB compress.write(line.getBytes(), 0, line.length()) isn't valid. It assumes the number of chars in the String is the same as the number of bytes when converted, which isn't always so. It should be compress.write(line.getBytes(), 0, line.getBytes().length()), or more simply compress.write(line.getBytes()).
I'm trying to make a video file transfer but am having problems getting the server to start sending bytes.
The first step is for the client to connect, the socket gets accepted. Then the client sends the video file name but the server never reads this.
This is the code for the server up until it blocks:
try(ServerSocket serverSocket = new ServerSocket(4005))
{
Socket socket = serverSocket.accept();
System.out.println("accepted");
OutputStream os = socket.getOutputStream();
BufferedReader receiveReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("This gets printed");
String request = receiveReader.readLine();//never passes this line
System.out.println("This doesn't get printed");
and this is the client up until it blocks waiting for the server to send the video bytes:
try(Socket socket = new Socket(IPAddress, 4005))
{
byte[] messageBytes = new byte[10000];
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeBytes("REQUEST;"+videoPath);//This is the line that should send the bytes for the server to read, so it won't block.
String home = System.getProperty("user.home");
String path = home+"\\Downloads" + videoName;
path = path.trim();
FileOutputStream fos = new FileOutputStream(path);
BufferedOutputStream bos = new BufferedOutputStream(fos);
InputStream is = socket.getInputStream();
int bytesRead = 0;
System.out.println("Downloading file...");
while((bytesRead = is.read(messageBytes))!=-1)//This blocks here
Why on earth isn't the server reading the "Request" + videoPath bytes that the server is sending? I tried outputStream.flush() as well, no luck.
Usual problem. You're reading a line but you aren't writing a line. Add a line terminator to the sent message.
When you fix this you will then discover that you can't mix buffered streams and readers on the same socket. I suggest you do all the I/O via the DataInput/OutputStream classes, using read/writeUTF() for the name.
If you're sending multiple files see my answer there.
I am trying to send a "request line" and a file through a socket.
Client (sender)
Socket socket = new Socket(hostName, SOCKET_PORT);
DataOutputStream os = new DataOutputStream(socket.getOutputStream());
FileInputStream fis = new FileInputStream(fileName);
os.writeBytes("PUT c:\dev\foo\helloworld.txt" + "\r\n")
byte[] buffer = new byte[1024];
int bytes;
while((bytes = fis.read(buffer)) != -1 ) {
try {
os.write(buffer, 0, bytes);
} catch (IOException e) {e.printStackTrace();}
}
Sever (receiver)
ServerSocket serverSocket = new ServerSocket(SOCKET_PORT);
Socket clientSoc = serverSocket.accept();
InputStream inputStream = clientSoc.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String requestLine = bufferedReader.readLine();
File currentFile = (File)new ObjectInputStream(inputStream).readObject(); //This doesn't work
byteSequence = new byte[new Long(currentFile.length()).intValue()];
for(int i =0; i<currentFile.length();i++){
byteSequence[i] = (byte)clientSoc.getInputStream().read();
}
try {
FileOutputStream newFile = new FileOutputStream(currentFile.getName());
newFile.write(byteSequence,0, byteSequence.length);
} catch (IOException e) {e.printStackTrace();}
I am able to read the request line on the server but when I attempt to read the file it throws an exception (line below).
File currentFile = (File)new ObjectInputStream(inputStream).readObject();
java.io.Stream.CorruptedException:
java.io.StreamCorruptedException: invalid stream header: 0A48656C
What exactly am I doing wrong?
You are trying to access an object that was not even passed to the socket.. you only passed a byte of string to the server but you never pass an Object to the server using the ObjectOutputStream..
I have implemented a client/server to send files . When there is no more messages exchanged after sending the file , the code works perfectly , but if the client sends some string to the server directly after the code of receiving the file both client and server stop doing anything and the file is not sent it's something like if they both get stuck in deadlock but I'm not really sure :
Here is the code to send the file without sending anything after it , which works:
Client
String filename;
BufferedReader UIn = new BufferedReader(new InputStreamReader(System.in));
Socket peer = new Socket("localhost",9999);
System.out.print("Enter the file name to download :");
filename= UIn.readLine();
///////////////////////////////
DataOutputStream OutToServer;
OutToServer = new DataOutputStream(peer.getOutputStream());
OutToServer.writeBytes(filename+"\n");
FileOutputStream fos = new FileOutputStream(new File("D:/new.txt"));
BufferedOutputStream out = new BufferedOutputStream(fos);
InputStream in = peer.getInputStream();
buffer = new byte[1024];
while((count=in.read(buffer))>0)
{
fos.write(buffer, 0, count);
System.out.println(buffer);
}
fos.close();
System.out.println("done");
Server:
ServerSocket server =null;
try {
server = new ServerSocket(9999);
while(true)
{
client= server.accept();
//System.out.println("Connection accepted");
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
DataOutputStream outToclient =new DataOutputStream(client.getOutputStream());
String request=inFromClient.readLine();
file = new File(request);
if (file.exists())
{
OutputStream out = client.getOutputStream();
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
while((count =in.read(buffer)) >0)
{
out.write(buffer,0,count);
out.flush();
}
}
// System.out.println(request);
// outToclient.writeBytes("alaa\n");
}
} catch (IOException ex) {
Logger.getLogger(ServerWork.class.getName()).log(Level.SEVERE, null, ex);
}
But if I try to send anything after the loop between client and server it stops working . Is it because I'm using readLine() and writeBytes()?
You are using both DataOutputStream and OutputStream. I think that they
should work together but what I guess you should do is to flush the buffer (or close it).
After you're done writing everything you want to send, some data may still be in the buffer. Now you will have to flush the buffer, to force it to send all the data.(as it is said here).
outToclient.writeBytes("alaa\n");
outToclient.flush();