cancel
Showing results for 
Search instead for 
Did you mean: 
Reply

How can a VI be both server and client?

Solved!
Go to solution
Highlighted

How can a VI be both server and client?

Hi, 

I'm new in LabView and I'm trying to build a server and client VI using TCP/IP that runs in two computers. in my program I need both server and client VIs to communicate with each other which means I need both VIs to be server and client. I've tried using a case structure but it doesnt work. The only thing I achieved is a normal server/client system where the server sends a request and the client responses.But i need the client to send requests too.i have attached my VIs to this post.I would appriciate it if someone could help with this problem. 

Thanks in advanced. 

Rambaldi.

0 Kudos
Message 1 of 14
(621 Views)

Re: How can a VI be both server and client?

I suggest looking at network shared variables. Your TCP vis can not use the same port if both are running on the same machine.

=====================
LabVIEW 2012


0 Kudos
Message 2 of 14
(605 Views)

Re: How can a VI be both server and client?

Do you really need a client and server on each PC? If you simply need that two to talk to each other they can once the client connects to the server. In most cases you only need one server.

 

What Steve said about the not using the same port only applies to two servers on the same machine. A client must use the port the server is listening to and if the client and the server are on the same machine then they will both use the same port. However, only ONE of them is accepting waiting for connections on that port.

 

In the code you posted you actually swapped the names. What you call the client is actually the server code and vise versa. In networking a server is an application that listens to an assigned port, accepts connections on that port and provides whatever services it has implemented. A client is an application that establishs a connection to a server. Once a connection is established the two applications can communicate in both directions. The applications themselves will define how the conversation should progress and whether it is a one way conversation or a two way conversation. You don't specify what you are trying to accomplish but I suspect you only need a single server.

 

In TCP, every connection is defined by the source and destination IP addresses and the source and destination ports. The server uses a known port (FTP is port 21, HTTP is 80, telnet is 23, or some custom port in the user space beyonds the reserved ports) to listen for connections. The client will use the well known port of teh server and generally picks a random port for its port number. The LabVIEW VIs do this automatically. This is how a server and a client on the same computer can use the same port number. Two servers however cannot. The server application can spawn a child task allowing it to service multiple connections at one time though. Each connection will be unique though since the client's port number, address or both will change for each connection.

 

OK, end of networking 101.

 

Can you describe in a bit more detail what exactly you want to accomplish. Given that I could probably provide you with more information for your application.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 3 of 14
(588 Views)

Re: How can a VI be both server and client?

thank you Mark for the networking lesson,it was very helpfull.

what im actually trying to do is something close to a instant messenger application where one pc sends a text to another pc and vice versa and i need to encrypt the text.however encrypting the text is not a problem because i have already found the algorithm but my main problem is to connect two PCs together. i already know how to develop a server and client where the client sends some requests and the server responds. but i need this communication to be in both direction so i can achieve the instant messaging. i guess the right question is how to make a tcp connection use both write and read.i hope my explanations are enough.if you need to know anything else let me know.

thanks for your help!

Rambaldi.

0 Kudos
Message 4 of 14
(580 Views)

Re: How can a VI be both server and client?

To solve your problem you would write the server and client as you would any server/client. You would run the server on each PC. Either application could start a conversation by connecting to the other's server. Once connected they would use that connection to hold their conversation. When they were finished they would disconnect. Depending on the nature of your appication and the conversations taking place you could could have your application use a connection that has already been established to send more data or establish a different connection if the conversations need to be unique and separate. You can pick a single port number for your server and start a server process on each computer. Each computer will need to know the name or address of any and all computers it will communicate with. Based on your exact need you could use a single connection for a pair of computers (before connecting it would check to see if a connection already exists) or establish a different connection for every conversation. If you use multiple connections your server application must listen for the connections and when one is established it must spawn another task to process that connection.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 5 of 14
(575 Views)

Re: How can a VI be both server and client?

so correct me if im wrong,what u mean is that i actually need 3 VIs?2 servers and one client?

0 Kudos
Message 6 of 14
(567 Views)

Re: How can a VI be both server and client?

No, you only need two. One server (which will be executed on each PC) and one client. For obvious reasons you should allow the settings such as port number and IP address to be passed. that way you can reuse them easily.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 7 of 14
(557 Views)

Re: How can a VI be both server and client?

ok,im kind of confused.i came up with these VIs attached which are not working and its embarrassing.would it be possible for you to draw a sketch or a sample?

0 Kudos
Message 8 of 14
(550 Views)
Solution
Accepted by topic author Rambaldi
08-27-2015 04:09 PM

Re: How can a VI be both server and client?

 


Rambaldi wrote:

ok,im kind of confused.i came up with these VIs attached which are not working and its embarrassing.would it be possible for you to draw a sketch or a sample?


 

You have the cases in the case structure reversed. The second read should occur in the default case. Currently you only read if the size is 1, which never happens.

 

Also, the client should probably change the order of operations. It should write first, then read.

 

Your loop termination is continue if true, whcih seems backwards, especially if you pair it with a button labeled "stop". Smiley Wink

0 Kudos
Message 9 of 14
(541 Views)

Re: How can a VI be both server and client?

i fixed the problems you mentioned and its working perfectly.thanks so much for the great help.

Rambaldi.Smiley Wink

0 Kudos
Message 10 of 14
(510 Views)