C# server-side socket sender? - c#

I'm trying to muddle my way through setting up a server with a socket port, but I can't find any good references/examples for my situation.
Pretty much all server-side examples go:
Listen for socket to connect
Receive msg from client
Reply to client
End communications
What I want to do is:
Listen for socket to connect
Begin sending data intermittently (as data updates arrive)
Continue until the connection is broken
I'm considering taking the standard example, simply skipping the receive, then setting up to repeatedly send (based on receiving events inside the server), but I don't have high confidence in that and I'd like to know what the standard approach to this problem is.
Also, if I want to have multiple ports open do I have to manually assign IP ports, or can than be done automagically?

You can find one of the libraries here. There are some samples in demo project and I think CustomWireProtocol is a sample project that can solve your problem. You can change the client code to send continious messages to server.

I'd recommend looking into Nanomsg by the author of ZeroMQ. Here's an example from a .NET binding project on github:
https://github.com/mhowlett/NNanomsg/blob/master/Example/Program.cs

Related

How can I ping a Networked AV Receiver with UDP?

I've been working with a Denon AVR-X1100W in an effort to find out whether or not I can ping it over a local network. For this, I am using C# in order to make a simple program.
How can I ping this type of device? A traditional ping command doesn't work, however, as it lacks an actual web server internally. I understand that a socket needs to be opened and, from there, a specific message sent with UDP.
Beyond creating a socket, I am unsure on how to continue.
I managed to find a great JavaScript-based library, denon-avr, that helped me connect to the receiver.
While this resulted in a change from C# to JavaScript, I found using JS to be much more agreeable in terms of development.

Copy TCP stream and retransmitt from two sockets

I need to create a server application that will take the TCP input from an Ethernet webcam and retransmit the packets to two different ports. The reason for this is that I want to connect to a webcam feed with two different programs (using one program locks the feed, making the connection unavailable for anyone else).
Can anyone please say if this is possible and perhaps provide some pseudo code. I am new to network programming and I am not sure on the best way of proceeding.
Have a look at a basic TCP sockets tutorial such as this one.
In short, you can use TcpListener to detect incoming connections, TcpClient to store and interact with those methods server side, and make them client-side. You'd need a listener for the webcam connection, a client to hold that connection, and two more to route it's input into.
I've also create this gist this one (Thanks #jgauffin), that contains an working example program that does this, and two programs you can run to test it and see how it works. It's a bit too detailed for a SO answer, so I'll leave it there and let you examine it yourself.

How to read whenever there is an incoming message in Tcp socket programming in C#

I have a server-client program written in .NET C#. I have used socket programming and so far both the server and client are working. There is only one server instance, while I am able connect multiple clients all together.
Right now my code works like the following scenario.
When I start the Server code, it starts Listening. When I start a client, it waits for a command to be written on the console. After I write something, the client sends that to the server, then the server does something with that, writes something back. This is how the cycle goes. Like a ping-pong game.
My client code is availabe in this link.
Problem and what I want:
With this functionality, my client cannot listen to the server unless it writes something to the server. I want to make it listen whenever there is an incoming message. My server code is fine and the sequence for server(listen then write) is okay. The client sequence(write the listen) is okay as well, but my client cannot read unless it writes. Suppose two clients are communicating with the server. client1 tells the server- "hey I need something that belongs to client2", then server will send a message to client2. With the current code, client2 cannot listen to the message because its actually waiting to write. Hope I made myself clear.
How can I achieve that? Any code samples will be helpful. Thank you.
You will need more than on thread on the client. One for accepting console input & sending and one for reading the TCP stream (blockingly) and displaying. Getting it to look like IRC on the console is a bit tricky because user written characters and received characters will interleave.

Is it possible to 'relay' a socket?

I was wondering it it was possible to relay a socket object in either C# or Java? (Preferably C#)
I have lots of little programs i make and host them on my home pc, but my pc is behind a router, so i have to forward a port on my router every time i want to make a new application. So is there a way to send a tcp connection to another application on the same computer? for instance i get a connection in with the first line of text being RELAY::21005 which would then forward that port to localhost:21005 ?
Any help, tutorials, code snippets would be much appreciated. Thank you! :)
One problem you might face with your suggested solution (first line identifies target port) is that you'll have to change all of your client programs to send that first line. That's easy for programs you've written yourself but not so convenient if you want to connect to your PC's web server or ssh daemon etc. It's not impossible of course, but does make it hard.
I'd suggest your routing server listens on two ports - a control port and a "normal" port (I can't think of a better name at the moment). You would send control messages to the control port to indicate "until further notice redirect all incoming connections on the normal port to port nnnn". That avoids having to manipulate client protocols.
I don't know enough C# to provide advice about a C# solution, but in Java I'd simply do something like:
while (true) {
acceptConnectionOnNormalPort()
connectToTargetPort()
startThreadCopyingDataFromAcceptedPortToTargetPort()
startThreadCopyingDataFromTargetPortToAcceptedPort()
}
You'd not be able to scale that easily into thousands of connections...
K I take back my comment, check this out:
http://msdn.microsoft.com/en-us/library/aa395195.aspx
Using this requires the port sharing service to be up (it is disabled by default):
The Net.TCP Port Sharing Service is available on all operating systems
that support .NET Framework 3.0, but the service is not enabled by
default.
All of this is only useful to you if you are using WCF services tho.
The easiest approach IMO is to use ssh tunneling. As I wrote in my comment, there are lots of questions on SU that will show you how to do this.
But assuming that you want to program something ...
You'll need to create a client and a server. The client will have threads that call accept on whatever local ports you want to open. When a connection comes to a port, you create another thread that opens a connection to the server and continually sends data over the wire.
The server program listens on a single port, which you open in your firewall. It waits for connections on that port, and when it receives one it opens a connection to the specified local port. Then it shuffles bytes from one to the other.
The only trick is that you have to define a protocol for specifying the destination port in the client-server stream. Simplest approach is to write a two-byte integer at the start of the stream.
Yes, it is possible to relay a socket.
You can use TURN http://en.wikipedia.org/wiki/Traversal_Using_Relay_NAT
Some of TURN library/application:
pjnath
turnserver.sourceforge.net

Sending & Receiving "Nudges" via tcp?

I'm tyring to add a feature to my app that sends a nudge to all users of my program (family members inside the house), and when they receive the nudge the window of my app on their computer shakes for a second...
Can somebody please suggest how i'd have to go about this? I've never worked with tcp/ip before.. Is this what I should use, or is there something better?
I have tried to come up with my own solution however none of the samples ever work. So I thought maybe the people on SO might know of other ways?
Thank you :)
If this is just an "in-house" (pardon the pun) application, and you're all on the same network, you might consider sending a UDP broadcast packet. Each instance of your application could listen for a packet on a particular port, and when the correct one is received do the window shake thing.
You might consider UDP for this. Since you can broadcast/multicast via UDP it may be more suitable for this sort of application. There are downsides - UDP transmission is not reliable or guaranteed in the same way as TCP.
I'd go with a good XMPP Library. Maybe Jabber-Net?
You'll also have the added bonus of being able to connect to Google Chat and now Facebook chat later.
You haven't specified if presence in your case requires a server or not. If it is client/server oriented, using XMPP gives you the server side for "free" as a bonus.

Categories

Resources