Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

question from:https://stackoverflow.com/questions/66059156/c-sharp-socket-programming-allow-2-application-to-use-same-socket-tcp-connectio

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
297 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 :-)...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...