Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to send the message from server to client with out client ipaddress using TCP listener in C#?

My client application connect to server using TCP protocol with host ip address and port address.
at that time the server receive the client message (Message can getstream and write the stream) using tcp listener.

The client also listen to server request and then show the server message to client.

My problem is some time any message send to any client machine without ip address? Please give any idea about how to send to client??
Posted

1 solution

The direction of data transfer has nothing to do with listening and knowing IP address. (Besides, you can create two channels: in one channel you can pass client IP and later create another channel, where a client listens. However, let me tell about it in proper order…)

To establish communication you should listen on one side and connect on another side. Let's call listening side "server" and connecting side "client". You should understand though, that the same application can play a role of a server and a client at the same time, or represent several "clients" or several "servers", or any combination, at the same time. (Only a traditional "client-server" model does not use those combinations, but this is a very bad simple model, one of the reasons why Web application development is so difficult; but we cannot discuss it here in detail.) All you need is a separate thread for each activity, normally, two threads per listener (it's a common mistake to use one) and one per client.

To listen, you need the class System.Net.Sockets.TcpListener, to connect to listening application, you need the class System.Net.Sockets.TcpClient:
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.aspx[^],
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx[^].

Even in the primitive "client-server" model, on the server side, you listen for new connection (in a separate thread), and use another thread to read/write from/to a network stream. More exactly, you can accept a socket and obtain the socket instance representing a remote (client) socket, or accept an instance of TcpClient, representing the connected remote TCP client. You can accept in a loop in your accepting thread, to allow other clients to connect.

You can share the instances of the TcpClient or System.Net.Sockets.Socket (in general case, some collection of instances) with another thread which does read/write operations. (Don't forget to use mutual exclusion, for example, with lock statements.) This thread, as well as the communication thread on the "client" side, should implement some protocol of interlacing read/write operation. Even if you don't call it a protocol, such thing always exists. This is some application layer protocol:
http://en.wikipedia.org/wiki/Application_layer[^].

As you can see, even if you have one channel, both "client" and "server" reads and writes in sequence.

And finally, let me get back to the idea of "inverted" channel. You can start listening on what was initially a "client". You create two more threads and start listening, playing the role of the "server" as well. You already have one channel. Its application protocol can include the mechanism of passing of the IP address (port or whatever else) to the "service". The "server" part can play the role of the client and connect to the "client".

See also my past answer: Multple clients from same port Number[^].

—SA
 
Share this answer
 
Comments
durai.net 18-Feb-13 3:29am    
Hi, thank u for your reply, again i explain my problem... i have one server with ipaddress and port. The server running this location. But lot of client connect with this server.
for example when i connect to server that time i send message so, server listener getting that message and the write as stream(networkstream). Some case the server send message to client. (The server have one thread running and getting value from database)
more than one client connected with server..
"How to write the correct message with correct client... ) exmple
Database design
client 1 welcome c1
client 2 welcome c2
Sergey Alexandrovich Kryukov 18-Feb-13 9:59am    
I fully described the possible scenarios to do such things. If you have a specific question, please ask. On server side, have one thread accepting connections, plus and one thread to read/write; one for all clients. You will have to store sockets or TcpClient instances representing remote clients on server part; one thread for all would be enough. As soon as you have these instances, you can read or write data from/to. Two threads. One thread for all is bad.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900