473,378 Members | 1,495 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,378 software developers and data experts.

Tcp Server for several clients data conflict ?

I'm writing a TCP server that receive data from several clients.

The code:

Socket clientSocket = server.AcceptSocket(); // server is a TcpListener
object.

And now I start a thread that handle the clientSocket received data from the
client, and I going back to wait on the server.AcceptSocket() above for the
new client, and so on.

The trouble is that thread that handle one client receive data sent from
another client. And this is bad for me !
For each accepted socket the RemoteEndPoint is different but the
LocalEndPoint is the same, I think this is the trouble, but I do not know I
how to fix it. Here is the server must listen on a single port but should
handle several sockets connections at the same time. I guess every new socket
should be moved to a different port, but I found no way to do this.
Can anyone help we with that?

---------
Thanks
Sharon
Nov 17 '05 #1
9 1563
Nope, that's right. You must be doing something wrong in your code, servers
are meant to be able to accept multiple connections on a single socket
I haven't done any dotnet socket stuff yet so can't help.
Can you post some of your code.
Nov 17 '05 #2
Ok, here is the summarized code I'm using:

///////////////////////////////////////////////////////////////////////////////
IPAddress DEFAULT_SERVER = IPAddress.Parse("127.0.0.1");
IPEndPoint ipNport = new IPEndPoint(DEFAULT_SERVER, 31001);
TcpListener m_server = new TcpListener(ipNport);

while( true )// For ever for this example
{
Socket clientSocket = m_server.AcceptSocket();
Thread m_clientListenerThread = new Thread(new
ThreadStart(SocketListenerThreadStart));
m_clientListenerThread.Start();
}

private void SocketListenerThreadStart()
{
int size = 0;
byte [] byteBuffer = new byte[131072];// 128 Kilobyte.

while( true )// For ever for this example
{
size = m_clientSocket.Receive(byteBuffer);
// ERROR: In here I get mixed data, I mean that I get data received
from more then one client !!!
}
}
///////////////////////////////////////////////////////////////////////////////

Any idea???

------------
Thanks
Sharon
Nov 17 '05 #3
Where is "m_clientSocket" set? I can see "clientSocket" in the first bit but
I can't see where the m_clientSocket comes from in the thread method.
If there are several, check you're selecting the correct one.
Nov 17 '05 #4
Hi,

What is happening is that m_clientSocket variable is defined as an instance
member, hence it's available to all the thread, when a new socket is
received you assign to m_clientSocket the new one, losing the old one and
hence the mix

do this, use a Sync queue
Queue q = Queue.Syncronized( new Queue );

while(true)
{
Socket clientSocket = m_server.AcceptSocket();
q.Push( clientSocket )
.....
}

in the listening method:
private void SocketListenerThreadStart()
{
Socket clientSocket = (Socket) Queue.Pop();
..
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"Sharon" <Sh****@discussions.microsoft.com> wrote in message
news:8A**********************************@microsof t.com...
Ok, here is the summarized code I'm using:

///////////////////////////////////////////////////////////////////////////////
IPAddress DEFAULT_SERVER = IPAddress.Parse("127.0.0.1");
IPEndPoint ipNport = new IPEndPoint(DEFAULT_SERVER, 31001);
TcpListener m_server = new TcpListener(ipNport);

while( true )// For ever for this example
{
Socket clientSocket = m_server.AcceptSocket();
Thread m_clientListenerThread = new Thread(new
ThreadStart(SocketListenerThreadStart));
m_clientListenerThread.Start();
}

private void SocketListenerThreadStart()
{
int size = 0;
byte [] byteBuffer = new byte[131072];// 128 Kilobyte.

while( true )// For ever for this example
{
size = m_clientSocket.Receive(byteBuffer);
// ERROR: In here I get mixed data, I mean that I get data received
from more then one client !!!
}
}
///////////////////////////////////////////////////////////////////////////////

Any idea???

------------
Thanks
Sharon

Nov 17 '05 #5
Sorry, I posted typing error, here is the fix one,
The clientSocket and the m_clientSocket are the same socket which is data
member of the class:

///////////////////////////////////////////////////////////////////////////////
IPAddress DEFAULT_SERVER = IPAddress.Parse("127.0.0.1");
IPEndPoint ipNport = new IPEndPoint(DEFAULT_SERVER, 31001);
TcpListener m_server = new TcpListener(ipNport);

while( true )// For ever for this example
{
Socket m_clientSocket = m_server.AcceptSocket();
Thread m_clientListenerThread = new Thread(new
ThreadStart(SocketListenerThreadStart));
m_clientListenerThread.Start();
}

private void SocketListenerThreadStart()
{
int size = 0;
byte [] byteBuffer = new byte[131072];// 128 Kilobyte.

while( true )// For ever for this example
{
size = m_clientSocket.Receive(byteBuffer);
// ERROR: In here I get mixed data, I mean that I get data received
from more then one client !!!
}

///////////////////////////////////////////////////////////////////////////////

Hope it's clear now.

-----------
Thanks
Sharon
Nov 17 '05 #6
So every time a new client attaches, the value of m_ClientSocket changes, as
Ignacio and I suspected.
You need to store an array of client sockets, check which one you need to
use in the thread method before storing data from it.
Nov 17 '05 #7
Hi,

He may use a Sync queue as I shown in my previous post, it does work fine
that way

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Claire" <as*******@ntlworld.com> wrote in message
news:%2******************@TK2MSFTNGP14.phx.gbl...
So every time a new client attaches, the value of m_ClientSocket changes,
as Ignacio and I suspected.
You need to store an array of client sockets, check which one you need to
use in the thread method before storing data from it.

Nov 17 '05 #8
Thanks
Nov 17 '05 #9
Thanks
Nov 17 '05 #10

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

Similar topics

0
by: Kaan ?cg?n | last post by:
Hello NG, we have created a database for our schedules on the SQL server 2000. As we have many satellite stations, we created a merge publication. The aim of this merge publication is to provide...
15
by: Michael Rybak | last post by:
hi, everyone. I'm writing a 2-players game that should support network mode. I'm now testing it on 1 PC since I don't have 2. I directly use sockets, and both client and server do...
1
by: B Moor | last post by:
Hello, I am quite bogged down with this problem and would like some tips/help if any one has any. Thanks in advance. The Problem ----------- This system initially seemed quite stable for...
7
by: Trevor Best | last post by:
I have an import routine that takes a generic file (Excel, CSV, Fixed length, PDMS BOM, XML, etc) and maps fields to a temp import table on the server, in the field mapping there may be functions...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
0
by: perdoname | last post by:
Hello, I urgently need help, im trying to complete a class which will be able of multiple - clients but is not doing the required puprose. Can anyone be assistance of what is needed to add for...
1
by: kurrachaitanya | last post by:
Lecture Saying that: I am generally expecting most of you to work n java.If you prefer to use c/c++ version of the programs,you may,but the assistance in the lab sessions will be very much geared...
1
by: kurrachaitanya | last post by:
YOUR TASK IS AS FOLLOWS : The server is written in such a way that only one client can connect to it at any time.modify the server sothat several clients can be connected to it(and later disconnect...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.