473,573 Members | 5,079 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Socket communication layer as a DLL - how?

Hello,

I have a quite specific problem, and after half a day spent with
Google, still no solid answer.

The task:

I have to write a C# class Library ( DLL ) to handle TCP socket
communication. It will be called by a multithreaded server application
to send some data to an another server ( which is a linux server ).

There was already a very old ( unmanaged ) C++ dll, which used win32
sockets to do the communication, and I am required to replace it with a
managed C# DLL. The only information I have from this C++ DLL is the
data structures and response / error codes, and the function names and
parameters.

The question is - how?

My first idea was a static class with a single TcpClient to send the
data. Is this a viable method? Or I have to use asynchronous socket
functions? Or a spearate working thread with a Queue implented?

All the examples I found were:
- multithreaded SERVERS
- simple applications, not thread-safe class libraries

The problem with the current C++ comm layer DLL is that it gets struck
with more than 50 concurrent users.

Any help appreciated, thanks!

--

Mar 25 '06 #1
8 5669
From each server thread (i.e. user connection), will it be a single
request/response pattern then back to the client thread? Could you expand
the server requirements a bit? TIA

--
William Stacey [MVP]
Mar 25 '06 #3
If the class is going to be used by multiple threads making calls on it
simultaneously, you should consider using asynchronous methods. This means
that every time a send request comes in, it gets spun off on a threadpool
thread which gets freed up as soon as the work in the callback method is
complete.
if you still have issues, then you may need a custom threadpool because of
the default number of threads avaialable in the .NET threadpool (this may
have changed in .NET 2.0) One that I've used with great success is "Smart
Threadpool" by Ami Bar, you can find it on codeproject.com .
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Roodie" wrote:
Hello,

I have a quite specific problem, and after half a day spent with
Google, still no solid answer.

The task:

I have to write a C# class Library ( DLL ) to handle TCP socket
communication. It will be called by a multithreaded server application
to send some data to an another server ( which is a linux server ).

There was already a very old ( unmanaged ) C++ dll, which used win32
sockets to do the communication, and I am required to replace it with a
managed C# DLL. The only information I have from this C++ DLL is the
data structures and response / error codes, and the function names and
parameters.

The question is - how?

My first idea was a static class with a single TcpClient to send the
data. Is this a viable method? Or I have to use asynchronous socket
functions? Or a spearate working thread with a Queue implented?

All the examples I found were:
- multithreaded SERVERS
- simple applications, not thread-safe class libraries

The problem with the current C++ comm layer DLL is that it gets struck
with more than 50 concurrent users.

Any help appreciated, thanks!

--

Mar 26 '06 #4
Hello,

William Stacey [MVP] wrote:
From each server thread (i.e. user connection), will it be a single
request/response pattern then back to the client thread? Could you
expand the server requirements a bit? TIA


Finally I was able to get some more information about the project.

The first - fix - part is a server written in visual basic.net. What it
does: using async methods, it receives requests from a mobile
communication device, parses the messages, validates them, and send
them to an another server ( using an unmanaged DLL currently), waits
for the response, and then sends back the info to the mobile comm
device.

My part is replacing the unmanaged c++ communcation layer with a
managed C# one.

My current plan is to implement a queue in this DLL: when the VB
application calls the sendMessage method, it will put that message into
a queue, and a separate working thread will regularly check that queue,
and if there are any messages there, it will send them ( using a socket
connection ), and it will put the results into an another queue.

The interface is fixed, I HAVE TO work with the following functions (
the VB application uses these ):

CreateConnectio n( server_ip, server_port) // connects to the server
CloseConnection ( connection_hand ler ) // disconnects
Send( connection_hand ler, message ) // sends a command
Listen( timeout_in_secs ) // waits for the reply
GetQueuedMessag eCount( connection_hand ler )
ReadNextMessage ( connection_hand ler )

Is my plan feasible? OR did I miss something? Unfortunately I have not
too much experience creating these kind of communication layers.

Thanks for any suggestions or comments!

--

Mar 31 '06 #5
So you want to multiplex 1 socket connection (using the dll) to the other
server? So there will be 1 worker thread in the dll. Just confirming.
Also, are those methods static? This communication object will be a
singleton?

--
William Stacey [MVP]

"Roodie" <ro****@compact ive.com> wrote in message
news:u%******** ********@TK2MSF TNGP12.phx.gbl. ..
| Hello,
|
| William Stacey [MVP] wrote:
| > From each server thread (i.e. user connection), will it be a single
| > request/response pattern then back to the client thread? Could you
| > expand the server requirements a bit? TIA
|
| Finally I was able to get some more information about the project.
|
| The first - fix - part is a server written in visual basic.net. What it
| does: using async methods, it receives requests from a mobile
| communication device, parses the messages, validates them, and send
| them to an another server ( using an unmanaged DLL currently), waits
| for the response, and then sends back the info to the mobile comm
| device.
|
| My part is replacing the unmanaged c++ communcation layer with a
| managed C# one.
|
| My current plan is to implement a queue in this DLL: when the VB
| application calls the sendMessage method, it will put that message into
| a queue, and a separate working thread will regularly check that queue,
| and if there are any messages there, it will send them ( using a socket
| connection ), and it will put the results into an another queue.
|
| The interface is fixed, I HAVE TO work with the following functions (
| the VB application uses these ):
|
| CreateConnectio n( server_ip, server_port) // connects to the server
| CloseConnection ( connection_hand ler ) // disconnects
| Send( connection_hand ler, message ) // sends a command
| Listen( timeout_in_secs ) // waits for the reply
| GetQueuedMessag eCount( connection_hand ler )
| ReadNextMessage ( connection_hand ler )
|
| Is my plan feasible? OR did I miss something? Unfortunately I have not
| too much experience creating these kind of communication layers.
|
| Thanks for any suggestions or comments!
|
| --
|
Mar 31 '06 #6
Hello,
So you want to multiplex 1 socket connection (using the dll) to the
other server? So there will be 1 worker thread in the dll. Yes, that's the current plan.
confirming. Also, are those methods static? This communication
object will be a singleton?

Currently - in the old C++ version of the DLL - the methods are noot
static. The comm object will be a singleton, yes - or it is how I
planned so far.

--
Roodie
Apr 3 '06 #7
William Stacey [MVP] wrote:
So you want to multiplex 1 socket connection (using the dll) to the
other server? So there will be 1 worker thread in the dll. Just
confirming. Also, are those methods static? This communication
object will be a singleton?

One more thing - nothing is carved into stone :-) I am totally
inexperienced with network programming, thats why I am asking for
opinions and help!
So if you think that I should use a different approach, just tell me (
and explain of course :-) )

--
Roodie
Apr 3 '06 #8
I might keep it straight forward and simple as possible especially as this
may be your first network program. I am thinking:
1) Use a lock(). That "queues" up other caller threads.
2) Inside the lock, do your write and read to/from the server using the
single open socket.
3) release lock and the next thread goes.

public class Client
{
private object syncLock = new object();
//...
public string Send(string msg)
{
lock(syncLock)
{
byte[] buf = ...
s.Send(buf, 0, buf.length);
buf = Receive();
// return the string.
}
}
}

When you get this working, you can then decide if you need to get into
queues and/or async methods.

--
William Stacey [MVP]

"Roodie" <ro****@compact ive.com> wrote in message
news:Or******** ******@TK2MSFTN GP09.phx.gbl...
| William Stacey [MVP] wrote:
|
| > So you want to multiplex 1 socket connection (using the dll) to the
| > other server? So there will be 1 worker thread in the dll. Just
| > confirming. Also, are those methods static? This communication
| > object will be a singleton?
| One more thing - nothing is carved into stone :-) I am totally
| inexperienced with network programming, thats why I am asking for
| opinions and help!
| So if you think that I should use a different approach, just tell me (
| and explain of course :-) )
|
| --
| Roodie
Apr 3 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1167
by: Jacoste | last post by:
Using Socket communication under Terminal Services. Are there some kind of "virtual sockets", so that my app. can use the same port for different users on the same Terminal Server? /Jacoste
4
2339
by: Sa¹o Zagoranski | last post by:
Hi! I'm writing a simple 3D First person shooter game. It is a multiplayer game, where all the players connect to one server.
2
1511
by: WTH | last post by:
with a C# client (and/or server, but server not important)? I've got a scalable high speed (uses completion ports) C++ TCP/IP communication server but I'd like to write a C# client that other C# devs at our company can just drop in and use (it's an event and message passing system.) I'm sure, as most things C#/.Net, it's relatively...
5
15092
by: Rotzooi | last post by:
Hi, In the past I created a VB6 application that was capable of accepting multiple client connections for status logging over the internet using simple non-Windows clients (GSM/GPRS). There is one management console/monitor that receives messages and keeps track of keep-alive polling. The monitor created a new WinSock connection for each...
1
1366
by: batista | last post by:
Hi, My problem is that i have a client which was, uptil now, running inside a lan and using sokcet communication to connect to server which is also insdie the same lan.....(it was all working fine) Now, i want this client app to be able to connect to my server from outside the lan as well... Like i would like to give my user the...
2
2459
by: ne.seri | last post by:
In short, I'm building a kind of server which is supposed to handle open connections with clients. E.g. client connects to the server, the connection stays open, client sends a request to the server, server reasoned, and so on... The thing is that connection MUST always stay open. I'm using C++, and as for sockets, I'm using native syscalls,...
8
4565
by: panko | last post by:
Hello, I can't manage with asynchronous socket communication. :( I wrote a class CSocket.cs. This class is taking care of sending strings to LED display. This display is actually communicating via serial port and serial/ethernet converter (MOXA NE-4100T) with TCP server. So communication is in that way: MyApplication(TCP...
13
2713
by: Wade Yin | last post by:
Hi, If I can write a ActiveX component that can support socket communication in webpage, that will make browser have strongger capability to communicate with different clients, but not only can exchange info with the web server. I don't familarize with C# programming and here I just want to know is that possible to implemented?
9
10721
by: cnixuser | last post by:
Hi, I was wondering if someone could give me some general pointers about creating client server applications that would be able to communicate with each other over not just the LAN which I am able to do, but would be able to communicate with each other and exchange filestreams over the internet, meaning in most cases communication with PC's...
0
7792
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7709
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8039
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8091
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6435
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5296
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3744
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1049
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.