(Sockets) In java program pop3 commands only retrieve one message - java

I wrote a java program without the javax.mail package. To connect to my Gmail account I am using socket. To retrieve my messages I'm using a for loop.
for(int i=1; i<=NumOfMsg; i++){
output.println("RETR "+i);
do{
answer = response();
System.out.println(answer);
}
while (true);
response is method which look :
String response() throws IOException {
response = input.readLine();
if(response == null)
return null;
else
return answer;
}
When this program is conducted it only returns the first letter. After that the program won't repeat the loop. Cursor is blinking and I can't understand is he waiting another input of retr hanging. If I leave the program for 5 minutes it's starting eternal cycle and prints null.
Any suggestions would help.

Your logic is incorrect. You assign answer to the first result from response(), and then never update it, unless the response is null.
If you want to build an answer from the response, you should have a loop that appends the response to the answer variable.
for(int i = 1; i <= NumOfMsg; ++i){
output.println("RETR " + i);
do {
answer = response();
System.out.println(answer);
}
while (true);
}
String response() throws IOException {
response = input.readLine();
return response == null ? answer : answer + response;
}

Related

Could not make private broadcast method work

I modified an open-source project on github for a school project to fit my needs
it had a broadcast() method to send messages and it was called in the run() method in a while loop but the problem is that broadcast() sends a message to all users in a userList<>i wanted to add the ability to send a private message to one of the users by writing #username.
Here is the code for broadcast method:
private synchronized void broadcast(String msg) {
for (int i = 0; i < clientList.size(); i++) {
clientList.get(i).write(msg);
}
System.out.println("Log: Message broadcast --> " + msg);
}
and here is the run() method
public void run() {
System.out.println("Log: Got input/output streams for connected client.");
/** Get the first message from the client, attempt communication */
String clientMsg = null;
boolean accepted = false;
/** Allow client to create an account, login, or quit */
do {
clientMsg = client.read();
if (clientMsg.equals("QUIT")) {
System.out.println("Log: Client disconnected without signing in.");
client.disconnect();
return;
}
else if (clientMsg.startsWith("NEWUSER: ")) {
createUser(clientMsg);
}
else if (clientMsg.startsWith("LOGIN: ")) {
accepted = authenticate(clientMsg);
}
else
{
System.out.println("Log: Unexpected client message -> " + clientMsg);
client.disconnect();
return;
}
} while(!accepted);
/** Run main chat loop. Will read from the client, and broadcast each read
* until the client disconnects. */
while (true) {
int i=0;
String username= clientList.get(i).getUsername();
String line = client.read();
if (line == null) break;
else if(line.startsWith("#"+username)){
broadcastp(line,username);
}
else {
broadcast(line);
}
i++;
}
/** The only way for the client to exit the above loop is to disconnect.
* Therefore, call the handler's exit routine */
exit();
}
Here is the broadcastp() method that i tried to implement this feature with, but it doesn't work. It compiles and runs perfectly though just without the private chat feature.
private synchronized void broadcastp(String msg,String username) {
for (int i = 0; i < clientList.size(); i++) {
username = clientList.get(i).getUsername();
if(msg.startsWith("#"+username))
{clientList.get(i).write(msg);}
else {
continue;
}}
System.out.println("Log: Message broadcast --> " + msg);}
I do not have the full picture of how your program works, but you say the program runs perfectly, but does not do the private messaging part.
If I look at your code, in the while loop you always take the first username from the clientList (i = 0) and only call broadcastp if the line starts with that name.
First of all.. is broadcastp ever invoked? In broadcastp you have another loop, but that will always match on i == 0 given the way you invoke it (with the line and username from the while loop).
The problem seems to be be there. So something like this within the while loop might work for you (remove the i variable, and no need for broadcastp):
boolean isPrivate = false;
String line = client.read();
for (User user : clientList) {
if (line.startsWith("#" + user.getUsername())) {
user.write(line);
isPrivate = true;
break;
}
}
if (!isPrivate) {
broadcast(line);
}

When sending integer through PrintWriter from server to client, no parsing is possible

I am programming a little server-client-programm, which sends a text from one client who is writing on a file, to the other clients with the same filename, and got the following error
But I am just sending an integer and no other characters...
Here's the code:
Server
String[] splitter = scanText.split("\n");
String length = splitter.length + "";
//sending scanText to clients
for (PrintWriter pw2 : userMap.get(filename) ) {
if(!pw2.equals(pw))
{
pw2.println(length + "\n" + scanText);
}
}
Client
class "UpdateInBackground" is a class which is in the Client-class
class UpdateInBackground extends Thread {
#Override
public void run() {
int lines; //to know how much lines are send from the server
String scanText;
while (!this.isInterrupted()) {
scanText = "";
lines = Integer.parseInt(sc.nextLine()); //here I get the error
while (lines-- > 0) {
scanText += sc.nextLine() + "\n";
}
output.setText(scanText);
}
}
}
#asparagus, please define sc in line sc.nextLine(), considering this is an object from class Scanner, I need to know the input. The question must be self explainable with the definitions of variables and what are the inputs.
In Class UpdateInBackground,
lines = Integer.parseInt(sc.nextLine());// here nextLine() is for any String , please refer documentation
Reason for NumberFormatException : You are converting the value to int, without knowing, what is getting as input.
Try to use exception handling, to know what types of errors, might just come, to avoid the program getting struck.

Why do I get a "No line found" Exception in this code?

The following method causes a No line found Exception:
private static String getRebaseAnswer(boolean isFirst, boolean isLast) {
System.out.println("Would you like to (c)ontinue, (s)kip this commit, or"
+ " change this commit's (m)essage?");
Scanner in = new Scanner(System.in);
String answer;
while (true) {
answer = in.nextLine(); // <--- This Line
if (answer.equals("c") || answer.equals("m")) {
in.close();
return answer;
} else if (answer.equals("s") && !isFirst && !isLast) {
in.close();
return answer;
} else {
System.out.println("Would you like to (c)ontinue, (s)kip this commit, or"
+ " change this commit's (m)essage?");
}
}
}
I am calling the method in this method:
...
String answer;
Scanner in;
currHead = branchHeads.get(arg);
while (toRebase != null) {
System.out.println("Currently replaying:");
toRebase.getNode().printInfo();
answer = getRebaseAnswer(isFirst, toRebase.getParent() == null); // <--- This Line
...
What is causing the error?? Shouldn't the scanner wait for me to input a line before continuing the getRebaseAnswer method? A different method in my code has the exact same structure as the above method and encounters no problems. I've checked multiple other posts about this problem but their suggestions are all not pertinent to this problem or do not solve it.
This method runs with no problems:
private static boolean handleDangerous() {
System.out.println("Warning: The command you entered may alter the files in your"
+ " working directory. Uncommitted changes may be lost. Are you sure you"
+ " want to continue? (yes/no)");
Scanner in = new Scanner(System.in);
String answer;
while (true) {
answer = in.nextLine();
if (answer.equals("yes")) {
in.close();
return true;
} else if (answer.equals("no")) {
in.close();
return false;
} else {
System.out.println("Not a valid answer, please enter (yes/no).");
}
}
}
When you create a scanner connected to System.in and close it, you also close System.in. Therefore, subsequent attempts to read from System.in will result in the exception you observe.
The way is avoid this is to create the Scanner only once, and never close it until your program is finished. This Scanner should be passed to whichever function needs to read from System.in.
don't close scanner otherwise Stream will also be closed.
in.close();
remove this line from current location and put it in main method at the end so after all the operation stream will be closed..
You might be calling some other method which have already closed the stream and then you are calling this method.

Android Java - Two socketmessages when sending one

I send a message over sockets from an android app to a java program. This message is sent with a printwriter.
here is how I send my message from android to java program:
out.println("Hello there");
out.flush();
This is how I receive the message:
while(true){
String msg = in.readLine();
System.out.println("MSG: " + msg);
...some if-statements...
}
The output of the System.out.println is:
MSG: Hello there
*MSG: null*
How come I get null there? I'm only sending "hello there"..
Help would be appreciated, thanks!
EDIT: Actual code of the java program.
while (run) {
String msg = in.readLine();
String[] parts;
String username;
String password = null;
System.out.println("MSG: " + msg);
parts = msg.split("\\*");
username = parts[0];
password = parts[1];
boolean validUser = false;
validUser = db.authenticate(username, password);
if (validUser) {
db.updateIP(username, socket.getInetAddress().getHostAddress());
out.println("done");
out.flush();
} else {
out.println("loginfail");
out.flush();
closeSocketConnection();
}
}
}
private void closeSocketConnection() throws IOException {
in.close();
out.close();
socket.close();
run = false;
}
What is your exit condition for your while loop?
It looks to me like it is repeating and readLine will return null when the end of the stream has been reached causing MSG: null to be printed.
Updated given new information:
Assuming user*123 is a valid username/password combo then closeSocketConnection() is never called, the loop returns to the top, readLine returns null and you see your erroneous output. Followed by a NullPointerException on parts = msg.split("\\*");
Your loop should read:
while ((line = in.readLine()) != null)
{
// ...
}
At the moment you're processing the null that indicates end of stream inside the loop, hence you're printing it as a message when it isn't.

Socket, BufferedReader hangs at readLine()

I have a server which initially does this:-
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
for (;;) {
String cmdLine = br.readLine();
if (cmdLine == null || cmdLine.length() == 0)
break;
...
}
later it passes the socket to another class "foo"
This class wait for application specific messages.
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
appCmd=br.readLine();
My client sends this sequence:
"bar\n"
"how are u?\n"
"\n"
"passing it to foo\n"
"\n"
The problem is that sometimes "foo" does not get its response. It hangs in the readLine().
What is the chance that readLine() in the server is buffering up the data using the read ahead and "foo" class is getting starved?
If I add a sleep in the client side, it works. But what is the chance that it will always work?
"bar\n"
"how are u?\n"
"\n"
sleep(1000);
"passing it to foo\n"
"\n"
How to fix the problem? Appreciate any help on this regard.
eee's solution works perfectly. I was trying to read output from an SMTP conversation but it would block on:
while ((response = br.readLine()) != null) {
...Do Stuff
}
Changing to:
while (br.ready()) {
response = br.readLine();
...Do Stuff
}
I can read everything just fine. br is a BufferedReader object, BTW.
There is data already in the first BufferedReader (that has been read from the socket, and is no longer available from the socket), so pass the BufferedReader created in the first example to the class that reads the app specific messages, rather then creating a new BufferedReader from the socket.
I had the same problem and here is my solution:
try {
StringBuilder response = new StringBuilder();
response.append("SERVER -> CLIENT message:").append(CRLF);
//Infinite loop
while (true) {
//Checks wheather the stream is ready
if (in.ready()) {
//Actually read line
lastLineFromServer = in.readLine();
//If we have normal behavior at the end of stream
if (lastLineFromServer != null) {
response
.append(lastLineFromServer)
.append(CRLF);
} else {
return response.toString();
}
} else {//If stream is not ready
//If number of tries is not exceeded
if (numberOfTry < MAX_NUMBER_OF_TRIES) {
numberOfTry++;
//Wait for stream to become ready
Thread.sleep(MAX_DELAY_BEFORE_NEXT_TRY);
} else {//If number of tries is exeeded
//Adds warning that things go weired
response
.append("WARNING \r\n")
.append("Server sends responses not poroperly.\r\n")
.append("Response might be incomplete.")
.append(CRLF);
return response.toString();
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
return "";
}
The answer might be late but this is the simplest and latest answer in 2020, just use the simple way to receive the data from the socket server or client using the input stream read() method.
EOFException will be thrown when the client is disconnected or the server closed the connection.
private String waitForData() throws IOException {
String data = "";
do {
int c = inputStream.read();
if (c > -1) data += (char) c;
else throw new EOFException();
} while (inputStream.available() > 0);
return data;
}

Categories

Resources