Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Running a client/server apllication

 
Ranch Hand
Posts: 82
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hell,
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
Client.java

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...................
 
Sheriff
Posts: 22508
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Roger Fed
Ranch Hand
Posts: 82
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:365)
at java.net.ServerSocket.bind(ServerSocket.java:319)
at java.net.ServerSocket.<init>(ServerSocket.java:185)
at java.net.ServerSocket.<init>(ServerSocket.java:97)
at Server.main(Server.java:8)
what is this?
how can i get out of this problem?
 
Rob Spoor
Sheriff
Posts: 22508
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is something else using that port? You can see by opening a command prompt and then executing "netstat -a". An alternative is to use CurrPorts.

It's definitely not something in your code, because I was able to run your server just fine yesterday.
 
Roger Fed
Ranch Hand
Posts: 82
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
hello
FROM SERVER: HELLO
how are you

here the server supposed to respond but it doesn't
Where is the problem?

 
Rob Spoor
Sheriff
Posts: 22508
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic