I've developed my first Client/Server application using socket programming
the application works as follows:the client sends a string in lower case to the server and the server replies with the same string in upper case letters
I've developed these two classes
and the Server.java
My problem is that i don't know how can i run this application through command prompt or NetBeans.
so, I'd be grateful if you can help me in this
thanks in advance...................
I Hear I Forget, I See I Remember, I Do I Understand.
You run them as you run any application: "java Server" for the server, "java Client" for the client. You'll need two command windows of course.
But your protocol is flawed. The sending and receiving parts do not match. You send in byte form, then try to read using a Reader (text form). Also, you try to read a single line but the sent data never has any line breaks (BufferedReader.readLine strips away the line breaks). The receiving code hangs. You can solve this by sending "\n" as well (which you are already doing from the server):
This will work on most systems, but it's not guaranteed. You're still mixing binary and text data. Switch to a Writer for the sending instead:
There is now one issue, and one warning.
The issue: you are assuming that the systems the client and server will run on have the same system encoding. They may not. It's better to use an explicit encoding when creating the InputStreamReader and OutputStreamWriter objects.
The warning: you are now using "\n" as an explicit line break. This will work with this little example since readLine() can handle both "\n" and "\r\n", but most known services (like web servers) use "\r\n". If you then send "\n" they will have problems.
Thanks Rob Spoor for your valuable help
I've modified the two classes as you said
I've replaced DataOutputStream with Writer in the two classes and I've used flush() method but when i run the server class it throws an exception :
Exception in thread "main" java.net.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
what is this?
how can i get out of this problem?
Thanks Rob , you really helped me
my apllication is now works well
but i want to improve this to send more strings and the server reply to each one
I've modified the code in client as follow
and also in server
but it is still sends one string and after this the client sends and the server doesn't respond
this is the output of using this modified code
FROM SERVER: HELLO
how are you
here the server supposed to respond but it doesn't
Where is the problem?
I don't know; it works for me. The code I've used, taken from your initial post and your last update. Note the System.out.print in the client part; that makes it easier for me to see the client is waiting for my input.