-3

I am working on creating c# socket application and server side. I need to create one C# windows service which will should create a TCP connection and listen/establish a connection with a client on a certain address/port . Lets say example : 172.00.000.0/1000 and i have done that using TCPListener

And another c# windows service will use same socket/TCP connection to transmit the message with client.

This design is to make sure, the 1st windows service will hold the socket/TCP connection all the time and second windows service can be stopped/paused when there is deployment and as soon as the application is started, the socket connection starts transmitting message because, the connection is there because of 1st windows service.

Jayendran
  • 1
  • 1
  • There are many better solutions than sockets. I suggest using signalr, GRPC, wcf, any service bus, and basically any other modern technically other than sockets – TheGeneral Feb 05 '21 at 07:41
  • \*Technology\*... – TheGeneral Feb 05 '21 at 07:48
  • 1
    Frankly, your design is questionable. But the bigger problem is that you didn't _ask a question_. It is possible using the native Win32 API to copy a socket handle and pass it to a child process. But you can't have two processes successfully using the socket at the same time. See e.g. https://stackoverflow.com/questions/4604273/pass-socket-handle-from-net-to-unmanaged-child-process. Maybe you could get that to work in your situation. Maybe not. Your question doesn't have enough focus for anyone to provide a good answer. – Peter Duniho Feb 05 '21 at 08:36
  • Why not just establish a new connection in case of restarts? - it's not like the app can send anything during the time it is shut down anyway, so why go to this effort to save perhaps a fraction of a second in that scenario? – 500 - Internal Server Error Feb 05 '21 at 09:43

1 Answers1

0

you can try use this TcpTransferLib: https://www.nuget.org/packages/TcpTransferLib/2.0.0.6

It is not documented, but it is simple to create server and client with this lib. The good thing is, that this lib is care about chunks, so you can just send "messages" to the server or back to client. I use this in some projects and it works stable.

Code for server:

TcpTransfer tcpServer = null;
tcpServer = new TcpTransfer();
tcpServer.bufferChunk = 4096;
tcpServer.connectionTimeout = 15000;
tcpServer.receiveTimeout = 15000;
tcpServer.sendTimeout = 15000;
tcpServer.pingOnReceiveTimeout = true;
tcpServer.OnReceive += TcpServer_OnReceive;
tcpServer.StartServer(5557);

private void TcpServer_OnReceive(object sender, PacketReceiveEventArgs e)
{
      string data = Encoding.UTF8.GetString(e.data, 0, e.data.Length);
}

Code for client:

TcpTransfer tcpClient = null;
tcpClient = new TcpTransfer();
tcpClient.bufferChunk = 4096;
tcpClient.connectionTimeout = 15000;
tcpClient.receiveTimeout = 15000;
tcpClient.sendTimeout = 15000;
tcpClient.pingOnReceiveTimeout = true;

bool connected = tcpClient.Connect(ip, port);
if(connected)
  tcpClient.Send("hello", tcpClient.masterClient);

It is easy, but has not much options :-)...

nxexo007
  • 37
  • 2