wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes switching the connection between two servers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "switching the connection between two servers" Watch "switching the connection between two servers" New topic
Author

switching the connection between two servers

feras almasri
Greenhorn

Joined: Nov 28, 2013
Posts: 24
Hello,

I'm coding a program to exchange an information between a client and a server when the server give the client the requested information the client need to contact another server I don't know to contact another server when the the first step finished while I'm using this in the client side



Do you have any Idea about that

Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41151
    
  45
What is the exact problem? You know how to contact one server - what's the difficulty in contacting another in the same way if you know there server name (or IP address) and the port number?


Ping & DNS - my free Android networking tools app
feras almasri
Greenhorn

Joined: Nov 28, 2013
Posts: 24
I meant by my question how can I change the connection at the same session to another server is it only by changing the port of the other server or I need to use another Ip in this case.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41151
    
  45
I'm not sure what you mean by "change the connection". You can't change the server or port of an existing socket, but you can open as many sockets as you like to whichever servers and ports you wish. If the connection is to another server, then obviously you'd need to specify a different server name or IP address when you open the socket.
feras almasri
Greenhorn

Joined: Nov 28, 2013
Posts: 24
Ok, let me say it again

Now I'm connected by the client to the server and I asked him for the key to send it to another server. the server send me the key now I need to send this key to the other server. how can I implement this. I tried just to use another port in the second server and change the socket again in the client to the new server port is it correct?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41151
    
  45
Maybe I would understand if you posted a short, relevant code excerpt. As it is, I don't know what to add to what I wrote in my last post. Let me ask a specific question: What currently prevents you opening a second socket to a second server?

Maybe it's because the phrase "change the connection" or "change the socket" leaves me with the impression that you're doing something wrong, but without seeing the code I'm not sure what that might be. There is no "changing" of anything - there are two separate socket connections to different servers (or to different ports on the same server).
feras almasri
Greenhorn

Joined: Nov 28, 2013
Posts: 24
here is my configuration



and here where I call the configuration with the ports number


first Call to connect with the main server and second call too change the port for the second server port
It's working with me now. but is it the good way to change the connection between two servers for the same session
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

feras almasri wrote:
first Call to connect with the main server and second call too change the port for the second server port
It's working with me now. but is it the good way to change the connection between two servers for the same session


As already mentioned, TCP is point to point -- and there isn't a mechanism to change the plumbing once it is up an running. You can kinda of simulate it by having the first server acting as a proxy for the second server, but that isn't changing, as the client is still connected to the first server.

You will need some sort of protocol (and code) where the first server tells the client about the second server, and the client does a brand new connection to it.

Henry


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

Joined: Nov 28, 2013
Posts: 24
Could you show me an example please?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

feras almasri wrote:Could you show me an example please?


I'm missing the point. You already know how to talk to one server. You want an example to talk to another server? See the work you did with the first server -- you need to do it again with the second server.

Henry
feras almasri
Greenhorn

Joined: Nov 28, 2013
Posts: 24
my problem is not with the other server my problem in the client to change the connection to the next server
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

feras almasri wrote:my problem is not with the other server my problem in the client to change the connection to the next server


As already mentioned, with TCP, you can't do that. You will have to code your client to start a new connection with the second server (once you gotten the information from the first server).

Henry
feras almasri
Greenhorn

Joined: Nov 28, 2013
Posts: 24
This is what I already did I'm call the configuration function to change the port for the next server.
My question is it correct to do that?

one more question if two clients connected to the server can I give an information from the first client to the second client?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41151
    
  45
feras almasri wrote:This is what I already did I'm call the configuration function to change the port for the next server. My question is it correct to do that?

Does it work?

if two clients connected to the server can I give an information from the first client to the second client?

The client would have to send it to the server, and the server would send it to the second client. You'd need to think up a protocol for that (meaning, how does one client identify to which other client to send this information, how does the server know which client is which, etc.)
feras almasri
Greenhorn

Joined: Nov 28, 2013
Posts: 24
now I'm getting this error message when I'm trying to connect the server by another thread client

java java.net.socketexception connection reset


at the same time when I connect it with the first one it's working

First client connection working


Second client giving exception in the server side


Server Side... the error in the reading line

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41151
    
  45
Is your server written to support multiple clients? In other words, is it multi-threaded?
feras almasri
Greenhorn

Joined: Nov 28, 2013
Posts: 24
It supposed to be that.
because when I run to services of the first client it respond correctly except when I run this another client
feras almasri
Greenhorn

Joined: Nov 28, 2013
Posts: 24
Ok this was the problem when I added to the client it worked

I don't know why ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41151
    
  45
We haven't seen enough of the code to say why. But just in case you're reusing the "socket" object reference - you'd need to close the first one before you can open another one.
feras almasri
Greenhorn

Joined: Nov 28, 2013
Posts: 24


I'm receiving two different objects from two user based on the connection I need to send the KCWS from the first onject to the second object while the KCWS generated after the the second object starts before the first object.
feras almasri
Greenhorn

Joined: Nov 28, 2013
Posts: 24
while I'm in a two different thread how can I send an information between two clients
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

feras almasri wrote:while I'm in a two different thread how can I send an information between two clients


Well, your server is talking to both clients -- so your server can relay data between those two clients.

Henry
feras almasri
Greenhorn

Joined: Nov 28, 2013
Posts: 24
I can't I think I'm in a two different threads.

Or maybe not I can't decide.

Anyway if I'm in a one thread how can send form the second condition the variable of the first object of the first condition.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

feras almasri wrote:I can't I think I'm in a two different threads.

Or maybe not I can't decide.

Anyway if I'm in a one thread how can send form the second condition the variable of the first object of the first condition.



Perhaps the Oracle tutorial on threads is a good place to start... http://docs.oracle.com/javase/tutorial/essential/concurrency/

Fully understanding the use of threads isn't something that can be done by example, or explained in a paragraph.... but yes, there are tons of mechanisms in the concurrency library to enable threads to pass data safely.

Henry
feras almasri
Greenhorn

Joined: Nov 28, 2013
Posts: 24
thanks, I'm doing that and I used a static variable between the two threads.

I have another question now. how can I ask the other client to wait until the server send to it a message I use this but it seems not working

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

feras almasri wrote:thanks, I'm doing that and I used a static variable between the two threads.


Yea, just be careful. It isn't as simple as just using a static variable. You have to take care of all race conditions, by properly synchronizing and/or using the concurrent tools correctly.

feras almasri wrote:
I have another question now. how can I ask the other client to wait until the server send to it a message I use this but it seems not working



You have to design it. This is not a networking issue -- but an implementation/design issue. One possible to option is to have the server hold onto messages until the clients polls for them. A client can contact the server to send information to the server (or to other clients to be held by the server), or a client can make a request for information being held at the server. By only having the clients control when messages are sent or received. your client don't have to poll for requests, and become easier to implement. Of course, this is one design. I am sure you can figure out other designs.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: switching the connection between two servers
 
Similar Threads
Chat Application
socket programming on 2 ports
Connect to a socket server using applet
Opening a socket connection on client m/c
Opening a socket connection on client machine