aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Running a client/server apllication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Running a client/server apllication " Watch "Running a client/server apllication " New topic
Author

Running a client/server apllication

Roger Fed
Ranch Hand

Joined: Oct 17, 2010
Posts: 82

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...................


I Hear I Forget, I See I Remember, I Do I Understand.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Roger Fed
Ranch Hand

Joined: Oct 17, 2010
Posts: 82

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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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

Joined: Oct 17, 2010
Posts: 82

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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running a client/server apllication
 
Similar Threads
cleint-server interaction
display http header
bugs in client\server program - can't connect to remote server
port number
Simple problem in running a Server class