Simple Client-Server application in Java - java

I started to learn about server-client application and I used this Java code example for client and server:
http://docs.oracle.com/javase/tutorial/networking/sockets/examples/EchoServer.java
http://docs.oracle.com/javase/tutorial/networking/sockets/examples/EchoClient.java
I ran both applications on two instances of Eclipse.
The code remain the same, except for the change:
//out.println(inputLine);
System.out.println(inputLine);
In the server side.
And to the printings "Hello", "Bye" in the beginning and end of the main() in the server-side. And of course, I set the IP used by the Client to the IP of my computer (used ipconfig - not unique IP)
From what I understand, everything the client sends should appear on the server standard output until the client types "Cntrl C" (unless I understand wrong).
here is the input I wrote in the client side:
boooooooooo
baaaaaa
paaaaaaa
And this is what I get in the Server-side:
Hello
boooooooooo
Something in here is unexpected to me-
the server seem to run in the main(),
but from some reason after the first printing it stops.
I tried o set breakpoints inside the Client side code on the while-line
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
but the program flow encountered the while only twice an then didn't reach there anymore.
when it did enter in, userInput was not evaluated, I suspect because readline() returned null. But why should it? if EOF character wasn't inserted to the standard input in the Client side.

Related

Java Client/Server - Implementing a read/receive loop

I am working on a client/server project for class. I have the server set up so that it is listening for inputs and will relay appropriate message/messages back to the client. For example, client sends input "help" to the server, the server will respond with 5 lines of strings back to the client's console. This is done with the code:
while (true) {
System.out.print('>');
inputLine = bufferedReader.readLine();
toServer.write(inputLine + '\n');
toServer.flush();
while ((serverInput = fromServer.readLine()) != null) {
System.out.println(serverInput);
}
So I believe the problem with this is that it is indefinitely waiting for serverInputs at the end. I would like it to go back to reading inputs from the console after all the appropriate messages have been received. Any help is greatly appreciated. Thank you

client cant read from server with readLine

I have a made a socket connection between client and server. Everything is working good if i introduce the first comand on client, receiving the message i want from the server.
#server
public PrintWriter out;
out.println(res);
#client
public BufferedReader in = null;
String line;
line = in.readLine();
At the second time i run it won't show the message i send from server cause it will read \n so it will be an empty string. If i change this:
#server
out.println("\n"+res);
The first time i run now it will jump a line, printing just the \n. And the second time i run it will show the right message.
If i change now to:
out.println("\n\n"+res);
It will just show when i introduce something to send to the server and receive back after the 3rd time (the first 2 times it prints \n).
Don't know what to do to show always the message i send from the server. Any advice?

Getting a `500 Unknown Command` when invoking `GET` in Java

Alright, so I'm given a Java FtpClient class that I am supposed to finish/modify so that the finished product will serve as a WebServer.
The following is a method that lets me interact with the server through command lines.
/*
* Send ftp command
* #param command: the full command line to send to the ftp server
* #param expected_code: the expected response code from the ftp server
* #return the response line from the ftp server after sending the command
*/
private String sendCommand(String command, int expected_response_code){
String response = "";
try {
// send command to the ftp server
controlWriter.writeBytes(command);
// get response from ftp server
response = controlReader.readLine();
if (DEBUG) {
System.out.println("Current FTP response: " + response);
}
// check validity of response
if (!response.startsWith(String.valueOf(expected_response_code)))
{
throw new IOException(
"Bad response: " + response);
}
} catch (IOException ex) {
System.out.println("IOException: " + ex);
}
return response;
}
However, when I invoke the GET command, i.e.
sendCommand("get " + __file__name__ + "\r\l", 200);),
I get the following response:
500 Unknown command.
I am almost 100% sure this issue has nothing to do with the method I've posted above, but I only posted it so you'll know what I am referring to by the sendCommand method).
Has anyone had a similar issue with this command before? If so, how did you work around it?
I've done a very similar side project to the one you're doing here, and I've encountered the same problem you've discussed here. I still haven't figured out why I wasn't able to simly invoke GET and read off the data stream, but here's my get-around.
First, you'll need to use RETR instead of GET. If you're not familiar with what RETR does, it basically lets you send a file as a packet of bytes through a temporary port you'll generate for data transmission.
To instantiate a temporary port, you will need to be im Passive Mode. So, type in:
quote pasv
Your output would look something like the following:
227 Entering Passive Mode (127,0,0,1,143,155).
A quick glance at the numbers shown between the two parentheses will probably not mean anything to you, however, two details can be derived from there.
The first 3 numbers represent your localhost which is always 127.0.0.1, an the other two are referred to p1 and p2. In this case, we have p1 = 143 and p2 = 155. These two numbers can be used to figure out which port has been assigned to us for data transfer.
Fire up your command line interface
To find out the port number, plug in the numbers in the following formula:
PORT = p1 * 256 + p2
So, our port number in this case is (143 * 256) + 155 ==> (36763).
Now that we have a transfer port open for us and ready for data transfer, you can go ahead and instantiate a new Socket with the port number derived from the formula mentioned above (please note that the numbers will be different every time you run the quote pasv, so don't assume these are constants).
The next step here is to send the file from yourself to the client. Note that you're not directing the data packet to a specific client, rather, any client that's currently connected will be receiving the packets.
To send the file, type in the following command:
quote retr
now you can use the DataInputStream class from your Socket to read all the bytes, display them, then them into an identical copy of the original file, or do whatever you're planning to do with them.
Note.. Note... Note... : the commands listed above were meant to be entered from the command line But since you want your application to handle all the job (I assume), the same commands can be passed from your Java application with a little bit of tweaking around. You will basically only need to take the word quote out of all the commands we've used them in.
I think I've typed enough tonight. I am headed to bed now. Let me know if you need further help in a comment below and I will try to respond as soon as possible. Also, let me know if anything I have said is not making sense to you.

Java Socket : Write to client not working

I currently have a problem with a Java server thingy.
It's a simple TCP server which send images. Problem is, I don't have the code of the client program... Moreover, it seems that there is no way to check the client socket for writing event nor the amount of data already sent to the client.
Do someone have any idea about what could prevent the client to get the image correctly ?
Here's my code :
byte[] response = process ( cmd );
if ( response == null )
{
controlSock.close();
dataSock.close();
stop = true;
}
else if ( dataSock != null )
{
dos.write( response );
dos.flush();
}
By the way, the server is working fine with Telnet.
If the server works fine with telnet then your server is fine.
The problem is more likely to be in the assumptions the client is making are not the same as yours. for example the client might assume you are sending the size first in big or little endian format (as an int, short or long) Perhaps it expects the name of the file/image in some format as well.
The only way to know this for sure is to read the code for the client or ask someone who knows what assumptions the client makes.

Network output from server can't be read by client

So, I have a simple socket server and a socket. I run the socket server, successfully. The client socket connects and sends a string - this works. I want the server to write back different information based on this string. I can check what the string is and get an OutputStream to the client, but whenever I write to it and flush, the InputStream client-side is NEVER in a ready state, and will never get a message back... I just don't see what I'm doing wrong.
All the code is at http://pastebin.com/u/omegazero
NetworkAgent.java is the client, SimbadAgent.java is the server, and UserAgent.java is the actual implementation of said server (the server is abstract for other reasons).
Compile everything, then run UserAgent followed by NetworkAgent and you will see what happens.
Executed your code (after commenting the reference to StringQueue in SimbadAgent) and I got the following output.
wrote get_cmd
Input shutdown? false
iS()iI()iM()iB()iA()iD()i ()iB()iO()iO()iY()iA()NETWORKAGENT: Response to "get_cmd": "SIMBAD BOOYA"

Categories