C# TCP/IP server and client - c#

i have a simple TCP client/server application. How do set up the two program so on the server side it requires a username and a password, and when the client tries to connect, they have to enter the correct user name and password and send it to the server side, the server side will check to see if thats the correct username and password, if its correct then it allows connection, otherwise it rejects the connction

Use WCF since you obviously have not used pure sockets before. It's will most probably be a more stable solution then if you try to learn sockets AND invent your own protocol.
You can use WCF together with the netTcpBinding to achieve what you want. You got a example here: http://www.dotnetspider.com/resources/19314-Client-server-sample-using-WCF-net-tcp-binding.aspx
I'm sorry if this wasn't the answer you where looking for, but there is a lot that need to be implemented correctly to get a hassle free tcp/ip client/server solution.

This can not be done using simple TCP/IP. You'd either have to invent some kind of protocol or use WCF or the like.
As the "connect" happens before anything can be sent, there's no way of sending credentials "along with the connect". You'd have to have the client connect, receive a command for "Login", check credentials and pass back some unique identifier that then needs to be sent along with every command so the server can check whether the client was authenticated. That's how I'd do it if WCF was not an option.

White Thorsten is technically correct, I still think there is a solution:
Let the server accept all connections, but then expect as the first data in the connection the username and password. If they are incorrect, the server will then close the connection, and not process any requests that the client may have sent on the connection.

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.

Prevent other applications from connecting to your TcpListener

I made a FTP server application which uses Net's sockets. The application will immediately disconnect a client, that was accepted by the TcpListener, if the client didn't send a login message containing a username and password. But how do I prevent someone from decompiling my client application and seeing the server's password and replicating the message that's required by the client, to login in to the server, in another app. In general I want to avoid someone from making an application, that implements my objects from the .dll provided with the client application, which can connect to my FTP server.
There isn't really a way to completely protect your service if you are going to distribute a client library. As Kinetic says, hard coding passwords is definitely a bad idea because someone could easily decompile your assembly or use something like process explorer to discover it.
There is usually little harm in gracefully handling any client that conforms to your protocol. If you think that it's likely your service will be the subject of abuse then perhaps look into denial of service protection. 😉

Sending messages from server to client 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/

Multi-Client Server communication when server is offline

So I want to create a Multi-Client/Server application. I know multiple clients can connect through one port, and I want to allow for the messages to be stored in a list when the server is "offline".
I'm having trouble finding information online to help me understand how the server can tell the difference between the multiple clients on the port. For example, if a 2 clients send messages to the server while it is offline and I store them in a list, how would I be able to tell the server that this message came from this client first so answer that, and then this message came from this client next?
Is there any information online on an easy way to achieve this? Any help would be greatly appreciated.
Sorry, I didn't make it very clear. I'll try again! Ok basically I want like a middleware between them so that the clients contact the middleware with the message, the middleware checks if the server is online and if so it sends it on, otherwise it will store the message(s) until the server is connected again. Does this make more sense? I'm not great at explaining myself, sorry again!
There seems to be some confusion of concepts here. When client are not connected they obviously can't send anything to the server and also don't have a port. Conversely, when the server is running and clients connect to it, the server has an object for each connection, representing each client.

Trouble understanding persistent http connections in c#

I do not quite understand how exactly persistent connections work.
So the keepalive property is set by default and this should keep my connection open, as far as I understand it.
So right now I am sending my data via a POST on an HTTPWebRequest.
But I do this everytime I send something to the recipient.
So it is like this:
POST data from client to server, response to the post is returned.
But next i just send another POST, instead of using the connection I already opened. So I feel like I am sending more than I actually have to.
Can't I just open the connection once and then continue communication via this connection?
I am sorry for my poor understanding of this subject, but this is the first time I really work with network protocols.
Thanks in advance
Daniel
KeepAlive was added to HTTP protocol to improve server-side performance.
HTTP is generally a stateless protocol. All state is preserved as cookies or server's session. If KeepAlive is set to true, client and the server could potentially keep their underlying TCP Connection alive.
Usually a time-out set for KeepAlive so that if client did not make any other request, this connection is closed.
This feature is usually implemented differently across different platforms, for example I have seen issues with Java implementation where they do not respect the timeout and server closes the connection so client's attempt to connect again fails since it assumes connection is still open.
See RFC document here.
You can reuse the connection IF the server supports it.... including any proxies in between you and the server. Which is where it sometimes falls down. It was tacked on to HTTP 1.0 and officially added in 1.1.
Basically, your client asks "may I keep this connection alive" with a special header, then if the server supports it, it replies saying "yes" and the client can then send more requests on the same socket.
Your client code also has to support this ability. .Net should be fine.

Categories

Resources