File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes Doubt in Sockets!! 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 "Doubt in Sockets!!" Watch "Doubt in Sockets!!" New topic
Author

Doubt in Sockets!!

Gunasekhar Dev
Greenhorn

Joined: Mar 03, 2005
Posts: 7
Hi

I have to write a Client/Server application where initially client connects to a multi threaded server and exchanges requests and after server receives data it processes and send a reply. Server also performs other activity like connecting to client back and exchange some data. Here i expect client to behave like a server so that it will run on another port or same port(im not sure about this) and accepts the server connection & request and reply back with some data.
In order to proceed I have just written a simple client/server(not multithreaded server) program that performs above activity.
But i get exception on server while it tried connected to client but gave connection refused.
I am running my client/server both programs in same pc.

Here is the code:
Server Program:



Client Program:



And following is the output in client & server respectivly:
##Client output:

Connecting to server...
Connected
Now sending data to server....
data sent to server
Listening on 4000 port

##Server output:
Server listening...3000 port
Client 192.168.200.227
Reading client data...
Data from Client########: Hello from Client
Now connecting to Client on 4000 port
Exception in thread "main" java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at ServSocket.main(ServSocket.java:29)

Please help me
I tried googling for info but could not find any docs!!

regards
Gunasekhar Dev
Greenhorn

Joined: Mar 03, 2005
Posts: 7
Can anybody help me please where I am doing wrong in my code?
Is it a correct idea of having server connecting back to client on a particular port and exchange data with client



Pls help
regards
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
bit impatient are you?
Starting to scream after waiting 20 minutes for volunteers?

And not taking those 20 minutes to read the site rules which tell you to use your real full name.


42
Gunasekhar Dev
Greenhorn

Joined: Mar 03, 2005
Posts: 7
Sorry

I didnt read the rules, so I didnt know that its mandatory to have full name in this forum.
Now i have updated my profile and added my full name.
Actually first after registering I have used my full name but donno why I modified it back to short name..
I apologise
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Thanks for adjusting your name - and welcome at the Ranch!

I'm not an expert with sockets, but as far as I know, they are bidirectional: once you have a socket connection, you can both send and receive, by using the appropriate input- and output-streams. No need for a second connection to reply.

Moving this thread to "Sockets and Internet Protocols"...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18821
    
  40

Ilja is correct. This is an awkward way to use sockets. Both programs are acting as both client and servers.

But to answer you question, I would say that the server is trying to connect to the client, before the client is ready to accept the connection.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Gunasekhar Dev
Greenhorn

Joined: Mar 03, 2005
Posts: 7
Thanks for your replies!!

But my requirement is, there are various clients who keep updating a common file which resides in server. Only one client at a time will be able to access that common file. Now everytime any updation done to that file, then file content has to be sent it back to all clients who are currently logged in.
In this scenario, server should be able to connect to client and send the file.
Any suggestions, on how to go about this in java?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Well, simply, the client needs to open the ServerSocket before connecting to the server, so that when the server tries to connect, the client is already waiting. You'll can simply use a dedicated thread to call accept(); this thread could stick around and do the processing of commands received on that thread, too.


[Jess in Action][AskingGoodQuestions]
 
jQuery in Action, 2nd edition
 
subject: Doubt in Sockets!!