Sending messages from server to client c# - c#

My client sends messages to the my server using tcp protocol (my server has a listener).
I'm trying to send messages back from the server to the client, but I'm not sure what do I need to add to the client in order to get those messages.
I tried looking it up, but I didn't understand how to implement it.
Could you please assist?

It's the exact same process, but in reverse.

If you intend to receive messages independently (i.e. not in response to a request by the client), you need to make the client a TCP server, too. The client needs to implement exactly the same thing as the server you have now and the server needs to connect to the client.
Can't you use WCF, which supports callback contracts?

If you want a console example of a client server application using a network library see here http://www.networkcomms.net/how-to-create-a-client-server-application-in-minutes/.
If you want a WPF application example then see here http://www.networkcomms.net/creating-a-wpf-chat-client-server-application/

Related

Grpc - send message from one client to another client that is connected to the same server

Is it possible to send message from one client to another client that is connected to the same server?
I want send data from one client to server Then send to specific client. I think I need to get client ID but I dont know how to get this id and how to send this message to that client from server.
I have a sample here. This is a chat server-client application. Multiple clients can connect to the server. When a client writes a message, the server simply broadcasts it for all clients who are receiving server stream RPC.
https://github.com/cactuaroid/GrpcWpfSample
See these server side implementation. When a client calls Subscribe(), it awaits m_chatService.Added event. When a client calls Write(), it raises the event and event args ChatLog is written on responseStream.
https://github.com/cactuaroid/GrpcWpfSample/blob/f6e8c4b2493c23cdcbaffeca29b5bb6705fbe95c/GrpcWpfSample.Server/Grpc/ChatServiceGrpcServer.cs
https://github.com/cactuaroid/GrpcWpfSample/blob/f6e8c4b2493c23cdcbaffeca29b5bb6705fbe95c/GrpcWpfSample.Server/Model/ChatService.cs
You can add your logic such as specifying channel name to subscribe/write, or define OpenChannel(string password) to be called by client at the first time so that server can bind the client IP address to the channel, whatever as you like.
There's no special gRPC feature that would allow you to do this (all RPC are between a server and a client, there's no "broadcast" or "selective broadcast" feature to reach out to other clients connected to the same server.
The logic you want is something that can definitely be implemented and but details of such solution depend on your need. A naive approach that might work is e.g. this:
each client opens a bidi-streaming call to the server
server keeps a directory of connected clients
once server receives a message from a client, it selects which client it should forward to based on the directory.
server forwards the message to a client.
Needless to say this setup feels a bit complicated (you're basically implementing your own network protocol on top of gRPC), so even though it might be just the right thing for you to do, it would make sense to think about how to simplify the protocol so that you can use features directly supported by gRPC.

Redirect all program packets through alerternate port

I have an application I need to debug.
The application connects to a server using a specific UDP port, though the application can be configured to join any server with any UDP port.
My goal is to be able to view all incoming data/packets from the server, and be able to accurately send my own packets from my application to the application I want to debug to see and learn from the effects.
My idea was to use some sort of web service to redirect all traffic, sent and received through. With this in mind, I could process the data being received from the server and choose when I want to send my own packets between data being received from the official server.
I am fluent in VB.NET and C#, if anyone has any tips or ideas to help kick-start this project! Thank you to all who helpout! :)
Search for packet injection if you're interested in this. You can forward all your data through your own proxy/vpn and study the data there.

Client-Server architecture

This topic has been discussed million times before, but let me clarify my needs:
I need a single server which controls a system and includes the necessary functions. Furthermore, there will be "n" Clients which represents only the HI/GUI and call server side functions. The server itself should be able to send data back to the clients and call client-side functions too (like shutdown, exit and so on...)
I have heard about duplex services/contracts (http://msdn.microsoft.com/en-us/library/ms731064.aspx), but I'm not sure how far I'll come with that.
How would you handle this?
I recently made a proof of concept app that made both the server and the client host a WCF service each. The client connects to the server and then in a handshake call, gives the server the connection information to allow the server create a separate connection back to the client. It worked a treat with multiple clients on network links from local lan to 64k line on remote sites at the same time.
You could use WCF, and host the service on the server in IIS, in the application on the client and let the client register it's endpoint on the server.

WCF duplex communication

I need to re-write an existing client-server application.
The existing application communicate using socket, I have to convert it into WCF.
At server side I need to
Monitor connected clients
Validate client request
Broadcast live data (comming from diff source).
Listen to client and respond to it
At client end I need to
Receive server broadcast and display it on UI.
Display UI and take input from user
Submit user data to server and display response
I have chosen tcp protocol on self hosting environment.
I need some immediate level references (link, tutorial, book)...
Have a look at this MSDN article, covers pretty much everything you need to know to build a duplex service in WCF:
http://msdn.microsoft.com/en-us/magazine/cc163537.aspx
You can try this : WCF on MSDN
I don't know if you can get it, but Microsoft book on WCF (course 6461A) is not bad.

Remoting in .net

I am doing applications on .net remoting ,i write the code for
Client -----------> server
but
client <------------ server
i don't know how to do it(just like a chat),Please give suggestions and some samples to look up.
without any request from server i send messages from my client application
Try using MarshalByRefObject and events. This would allow the Server to respond back to the client.
Here is a sample on code project
Another Sample with Event CallBack

Categories

Resources