473,323 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

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

CreateConnection( server_ip, server_port) // connects to the server
CloseConnection( connection_handler ) // disconnects
Send( connection_handler, message ) // sends a command
Listen( timeout_in_secs ) // waits for the reply
GetQueuedMessageCount( connection_handler )
ReadNextMessage( connection_handler )

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****@compactive.com> wrote in message
news:u%****************@TK2MSFTNGP12.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 ):
|
| CreateConnection( server_ip, server_port) // connects to the server
| CloseConnection( connection_handler ) // disconnects
| Send( connection_handler, message ) // sends a command
| Listen( timeout_in_secs ) // waits for the reply
| GetQueuedMessageCount( connection_handler )
| ReadNextMessage( connection_handler )
|
| 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****@compactive.com> wrote in message
news:Or**************@TK2MSFTNGP09.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
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
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
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#...
5
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...
1
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...
2
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...
8
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...
13
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...
9
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.