NetTcpBinding: send and receive simultaneously - c#

Consider a service that uses NetTcpBinding in duplex mode: the instance of the service receives messages, while the callback is used to send messages in the opposite direction. Can you receive and send data simultaneously? In other words: while the service instance is receiving some data, at the same time can you send data via the callback for the client (the client of the service instance)?

Raw TCP can send and receive at the same time. I wouldn't expect the NetTCPBinding to be any different. TCP is a bi-directional connection and the source and destination addresses distinguish between the direction as well as sequence numbers in the TCP packets.
Try it by writing two threads, a sender and a receiver and perform the respective operations in both at the same time.

Related

How to send and receive data simultaneously via TCP/IP C#

Trying to build an application that will act as client connecting to a remote server on a predefined IP and Port. The server will periodically send messages and also forward messages from other clients. I want to be able to receive all oncoming messages without interrupting the connection or state to send data to the server.
My current implementation uses sockets on which I switch between receiving and sending messages every 30 seconds.
This can mean I might miss messages during the time I am sending.

Socket listening without binding C#

I am trying to make a c# socket application which involves listening to a certain port and take any message sent to this port.
The problem is, I want to make it in a non-binding way. I mean, when I use TCPListener and accept socket,once a connection came, a socket for this connection returns and it deal ONLY with this connection.
What I want is different, I want any connection ( from any remote socket ) to send and I get their messages without being tied with any of them
What I am doing is 2 background workers, one for sending and other for receiving (i.e they are on different ports ). the app will send through the sending some commands depends on the pre-defined message format got on the receiving.
I hope it was clear enough

C# Log Socket Connections

I've no idea how I would go about this but I'm assuming that it is possible in some way, shape or form.
If I have a server that allow multiple connections to it through one port, is there a way I can make some sort of log of the connections, so that I could choose a certain connection to send a message to? Also if this is possible.
Is it also possible to do the same with connections through different ports?
How would I go about this? I'm fairly new to C# so not very experienced - any help is greatly appreciated!
Basically I want 3 clients to connect to a server. The clients will all send a message to the server, and the server will wait for a message from each client before replying to them, in the order in which the messages were sent.
I hope this makes more sense now.
If you are using TCP/IP, this is very much possible - the Socket that listens for incoming connections only does that - it does not handle the communication with each individual socket. Instead, the Accept() and BeginAccept() methods return a new Socket instance for each client that connects.
So the Socket instance you get when a client connects only receives messages from that client, and sending a message on that socket sends it to only that client.
Keeping track of which connection sent what - and which came first - will be more of a challenge, but definately possible.
If you are using UDP though things are a bit different, you would need to use a custom means of identifying each client.

How to Receive UDP Concurrent Request?

I'm building a UDP server that handles each incoming request in a separate
thread. The problem is, a UDP client may send out multiple requests
concurrently by using multiple threads. Each thread on the client will wait
for responses from the server. Since UDP is connectionless, a client thread
may receive a mismatching datagram. In this case, does have any
built-in mechanism or pattern that helps a client thread to get the matching response?
(for example send each request from server to specific udp port !!?)
I don't want use queue because it lost concurrently property.
If not, I guess we can build a queue that dispatches responses to
appropriate client threads. However, what if I need to run multiple clients
in different JVMs on the same client machine, and each client will make
requests to the same server?
Use a separate socket for each udp client. That way you have a different ip and port for sending and would receive response on the same i.e. the client which sent the request would only receive it's response (i presume that is what you meant by matching) This should be done automatically unless you share the same socket between threads and use it to send messages to server which seems like a bad idea.
You can set any port in your source (sender port) in client before sending message to server. The server can extract your source port and respond to the same port
e.g.
Client 1 source port:10401 -> server:listening port:2000. Server responds to port 10401.
Client 2 source port:10402 -> server: listening port: 2000. Server responds to port 10402.
If you are wanting to do concurrent connections, make multiple connections. Spawn a new UDP connection on the client, the server will just open a new connection on it's end. Anything sent on connection1 on the client comes in on comes on on connection1 on the server, anything sent on connection2 gets received on connection 2.

TCP Sockets connecting to a server on two different ports

I have a custom TCP Server listening on port 5888(dummy port). The proxy server listens for incoming connections. When the proxy receives HTTP request for certain pages, it should relay it to the main server on port 80.For other page requests the proxy is required to send data to the main Server on port 8081.
The port 80 is used to service the HTML Pages where as the port 8081 is used for streaming data to the clients.
I am able to receive the incoming connections on the proxy and then read the data from the clients. After reading the data, I can determine which port to connect to on the main server for sending the data.
I am stuck at deciding how to connect on 2 ports for sending the data from the clients to the Main Server?
In that case you either need 2 socket connection objects to the same IP on the different ports (this is legal), or you have one connection object which reconnects according to the port you have to deal with.
Depending on how often you have to switch connections the latter version might have a huge overhead, plus the first one allows you to send data to both ports virtually simultaneously.
You need to stop thinking of your program as a server. After you have received the connection, read the data, and decided what port to send it to, shift gears and start operating as a client would.
Just open a new connection to "localhost" on either port 80 or 8081 and re-send the data you received as if you were the original client.
Your client is connected to the proxy server on port 5888 so no matter from what real server (Web or streaming) you take the data, you are going to provide the data to the client using port 5888 only.
It seems to be a not so practical solution. I am assuming here that you are trying to achieve a kind of control port and data port structure where one port is controlling the streaming from another port.
Just creating two sockets is sufficient for obtaining data from two servers. Here you will have to manually create a protocol which your client understands as you are going to provide both html and streaming data to the client using single port.

Categories