I'm trying to figure out how to do a very small chat program where I have a server and several clients, lets say 3, the server should be the middleman in all the communication and all message should be passed to it before passing it forward to the right client.
Now I'm quite new at this so all I got is a client that can connect to the server using TcpListener and Socket. The client can then send a message to the server and the server can reply.
Essentially what I'm asking is how can I connect 3 clients at once, and how I can i destinguish the difference between them, so I know which client to forward my message to.
This is what I got so far:
https://gist.github.com/4555536
Also, how do I handle when I want to send several messages in a row without having to send back an acknwoledgement ? I mostly do games where I got an update method that can handle this for me.
Edit: How do I do it localy ? I know I can use IP adresses otherwise.
Best Regards, Fredrik
You could identify the connecting client by its IP
Related
I want to make a client and server app. The client would be able to send a request and receive the relevant data that the server would make. The client would run on a single computer with a static IP address and the clients would run on multiple machines.
So far I've been trying to use Remoting but couldn't get it to work properly as all the examples I found seem to be old, is this the best way to enable this service or is there a better way to do this?
I used the answer that was posted by sipwiz on this question: How can I send data over the internet using a socket? , I modified the code to send and recive a serialized tuple and it works great for my needs.
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.
I'm sure this has a simple work around, but right now I seem to have the inability to find an elegant solution. I have built an ASync C# server application which accepts multiple clients and handles them well, but to proceed I need to be able to determine which clients are connected to my server (there will be only two clients both of which do different things).
When my program accepts these clients I want the server to know the clients address and an indication which client is which potentially stored in a small list.
So I guess what I'm asking, is there a simple means of when the client has connected, sending a predefined message to the server letting it know what client application goes to which address?
Thanks
There are numerous ways.
One of the easiest is to have a different port for each of the client applications. The other way is in order to "connect" (after TCP Handshake) is to send a message identifying the application before any data is exchanged. The other option would be to add headers to each requests / reply stating the application and storing that.
You are free to send all kind of data over sockets.
To solve this, invent some kind of protocol between client and server.
For instance sending a first message when connecting to identify the client to the server.
From there on continue with the normal conversation between those two.
Server can accept the message and store of show it, or even reject the client when the first message does not contain the agreed information.
I need some feedback with some programming logic.
I'm developing a TCP Server using C#, which will also act as a TCP client. Basically, a client device (A) contacts my TCP Server and sends information. Based on that the TCP server may (or may not) talk to another server (Z) over TCP, send some data to it and sometimes get a response which it passes back to device A. So far so good, this works quite well. However, sometimes Z creates a new connection to my C# TCP server, and wants to send some data to device A.
This is what I am struggling with. I can get the data from Z, read it and see the destination IP address in the text that is read. But how do I send that to device A, which is still connected via a TCPListener on it's own thread?
I could use static variables and the "lock" mechanism to have the thread look for something to send to the device, but what happens if I add device B & C as well?
I basically need some way of, based on the data sent from Z on a new connection, determining which TCPListener on which thread the data should be sent to the devices.
It's doing my head in so any links you may have or rough logic (doesn't have to be code) would greatly help.
And yes, I've been searching Google for a while, but all the examples I have found are simpler than what I need to do.
Thanks in advance!
you can keep a Dictionary<IPAddress,TCPClient> for this purpose and send the message to the destination IP. Make sure you handle the multiple clients from the single IP gracefully either by droping the previous connection while accepting new one or instead having Dictionary<IPAddress,List<TCPClient>>
I'm looking to develop an asynchronous C# TCP server which can act as a proxy between two client VNC connections, passing data between the two transparently.
I've already got some asynchronous client-server code set up where I can effectively communicate messages between the server & any connected clients, now I need a way to host a kind of proxy for VNC traffic.
Client A--------------Server--------------Client B
VNC traffic sent -> relayed through server -> VNC traffic received
And then any response from client B to simply flow back to the server, then transitively to client A.
If any more information is required for a proper answer, please do let me know.
Thanks!
I managed to get this answered on Server Fault...the key is to launch "socat" from your application and use it to "link" the connections together.
See: https://serverfault.com/questions/254855/socat-connect-connect-proxy-two-inbound-tcp-connections-to-expose-a-firewalled
For more information.
You say that you already can receive data on the server that is sent from the client - you just write exactly what you read from one client back out to the other connected client.
It seems like you've already done the hard work, and I'm confused as to what part you're not understanding on how to finish it up.