473,659 Members | 2,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TCP/IP Client/Server question

I have a handheld running CE .NET 4.2 and I am using c# with framework
1.1 to develop a solution for syncing data that is on the handheld with
the local pc.

Our handheld cradles only support network connections, no usb or
serial, so i have to use networking to get the data transfered.

Here's the problem. I have the server accepting connections fine, but
i'm a little confused how to actually send the server data, and then
how the server should receive the data.

Here's what i want it to do, please let me know if this is the wrong
way to use tcp.
1) Client establishes a connection to the server

2) Client sends data.
2.1) Client creates a byte array the size of one class object, fills
it with an instance of that object, sends it to the server. It does
this multiple times for every object, so in a for loop it will cycle
through an array of objects and send them one at a time.
OR
2.2) it creates a byte array and fills it with all of the objects it
needs to send, and then does one send statement pushing all the data at
once.

3) Server receives data
3.1) Server recieves that data into a byte array the size of one
object, decodes the object and puts it in an array for later use.
Server then receives the next number of bytes for one object, decodes
it, stores it, and moves on, forever until all objects have been
receieved.
OR
3.2) Server recieves all of the data and puts it in a big string or
something, knowing that the client added a delimeter to figure out when
an object begins and ends, and then after all data has been received it
parses all that.

Here's what i am doing now. Client builds big byte array with all
objects. Each object is encoded in a pre-determined format and the
server knows how to decode it. Client has one send statement that
sends the entire array. Server pulls in 35 bytes, decodes those bytes
into a class object, then pulls in the next 35 bytes, decodes, etc
etc....

The problem: If i don't put some sort of Thread.Sleep(10 ) the server
decodes the bytes faster than the client sends them so i don't recieve
all the data. Here's some example code:

Client:
ItemQuote.ItemQ uote[] quotes = new ItemQuote.ItemQ uote[10000];
int itemNumber = 0;
TcpClient client = new TcpClient('127. 0.0.1', 1234);
NetworkStream netStream = client.GetStrea m();

for (int i=0; i<20; i++)
{
quotes[i] = new ItemQuote.ItemQ uote(itemNumber , "5mm Super Widgets",
1000, 12999,
true, false);
itemNumber += 1;

}

ItemQuoteEncode rBin coder = new ItemQuoteEncode rBin();
byte[] codedQuote = coder.encode(qu otes);

netStream.Write (codedQuote, 0, codedQuote.Leng th);

netStream.Close ();
client.Close();

Notes: Fill quotes array with ItemQuote objects, each with a different
itemNumber value. Encode the array into a byte array where each object
is just layed out in a row. Write the data to the network stream and
close the stream and connection.

Server:
(Note: clntSock is assigned elsewhere in the class. It is a TcpClient
object)
NetworkStream stream = clntSock.GetStr eam();
byte[] packet = new byte[35]; // 35 is the size of one class object in
bytes
ItemQuote.ItemQ uote quote;
ItemQuoteDecode r decoder = new ItemQuoteDecode rBin();

do
{
stream.Read(pac ket, 0, packet.Length);
quote = decoder.decode( packet);
*do something with quote here*
Thread.Sleep(10 );

} while (stream.DataAva ilable);

Notes: Pretty straightforward . Get the read/write stream, read 35
bytes at a time, do something with those bytes, then continue doing
this until stream.DataAvai lable is false.

Thread.Sleep(10 ) is required in order to keep DataAvailable to be true
before all the data is sent, but it feels like a hack rather than a
solution.

Maybe a solution would be to have the client first send the number of
objects it is sending, and do a for loop having it read that many
objects in and then quitting?

What is an exceptable solution for this? Everything i think up feels
like a hack and not a clean standard way to do it. The scenario is:
Client has a large amount of data to send, multiple class objects, and
wants to send them all at once. Server needs to accept it all and then
do something with it.

I can't use remoting because it's not supported on the compact
framework.

Thanks!

Apr 19 '06 #1
5 3436
One way could be to put a header of some sort telling the server how large
the data actually is i.e the first byte of the data you are sending to the
server from the client can be used to tell the server the size of the data
to read. i.e

//from the client
byte[] codedQuote = coder.encode(qu otes);
byte[] dataToSend = new byte[codedQuote.Leng th + 1]
dataToSend[0] = (byte)codedQuot e.Length;
codedQuote.Copy To(dataToSend,1 );

//i also prefer using Raw sockets instead of the TcpClient class
socket.Send(dat aToSend);

//on the server also using raw sockets
byte[] header = new byte[1];
byte[] message = null;
int read = socket.Recieve( header)
if(read == 1){
//it read something
message = new byte[(int)header[0]];
socket.Recieve( message);
}
//play around with message

hope this helps

"Yossarian" <Ar********@gma il.com> wrote in message
news:11******** **************@ j33g2000cwa.goo glegroups.com.. .
I have a handheld running CE .NET 4.2 and I am using c# with framework
1.1 to develop a solution for syncing data that is on the handheld with
the local pc.

Our handheld cradles only support network connections, no usb or
serial, so i have to use networking to get the data transfered.

Here's the problem. I have the server accepting connections fine, but
i'm a little confused how to actually send the server data, and then
how the server should receive the data.

Here's what i want it to do, please let me know if this is the wrong
way to use tcp.
1) Client establishes a connection to the server

2) Client sends data.
2.1) Client creates a byte array the size of one class object, fills
it with an instance of that object, sends it to the server. It does
this multiple times for every object, so in a for loop it will cycle
through an array of objects and send them one at a time.
OR
2.2) it creates a byte array and fills it with all of the objects it
needs to send, and then does one send statement pushing all the data at
once.

3) Server receives data
3.1) Server recieves that data into a byte array the size of one
object, decodes the object and puts it in an array for later use.
Server then receives the next number of bytes for one object, decodes
it, stores it, and moves on, forever until all objects have been
receieved.
OR
3.2) Server recieves all of the data and puts it in a big string or
something, knowing that the client added a delimeter to figure out when
an object begins and ends, and then after all data has been received it
parses all that.

Here's what i am doing now. Client builds big byte array with all
objects. Each object is encoded in a pre-determined format and the
server knows how to decode it. Client has one send statement that
sends the entire array. Server pulls in 35 bytes, decodes those bytes
into a class object, then pulls in the next 35 bytes, decodes, etc
etc....

The problem: If i don't put some sort of Thread.Sleep(10 ) the server
decodes the bytes faster than the client sends them so i don't recieve
all the data. Here's some example code:

Client:
ItemQuote.ItemQ uote[] quotes = new ItemQuote.ItemQ uote[10000];
int itemNumber = 0;
TcpClient client = new TcpClient('127. 0.0.1', 1234);
NetworkStream netStream = client.GetStrea m();

for (int i=0; i<20; i++)
{
quotes[i] = new ItemQuote.ItemQ uote(itemNumber , "5mm Super Widgets",
1000, 12999,
true, false);
itemNumber += 1;

}

ItemQuoteEncode rBin coder = new ItemQuoteEncode rBin();
byte[] codedQuote = coder.encode(qu otes);

netStream.Write (codedQuote, 0, codedQuote.Leng th);

netStream.Close ();
client.Close();

Notes: Fill quotes array with ItemQuote objects, each with a different
itemNumber value. Encode the array into a byte array where each object
is just layed out in a row. Write the data to the network stream and
close the stream and connection.

Server:
(Note: clntSock is assigned elsewhere in the class. It is a TcpClient
object)
NetworkStream stream = clntSock.GetStr eam();
byte[] packet = new byte[35]; // 35 is the size of one class object in
bytes
ItemQuote.ItemQ uote quote;
ItemQuoteDecode r decoder = new ItemQuoteDecode rBin();

do
{
stream.Read(pac ket, 0, packet.Length);
quote = decoder.decode( packet);
*do something with quote here*
Thread.Sleep(10 );

} while (stream.DataAva ilable);

Notes: Pretty straightforward . Get the read/write stream, read 35
bytes at a time, do something with those bytes, then continue doing
this until stream.DataAvai lable is false.

Thread.Sleep(10 ) is required in order to keep DataAvailable to be true
before all the data is sent, but it feels like a hack rather than a
solution.

Maybe a solution would be to have the client first send the number of
objects it is sending, and do a for loop having it read that many
objects in and then quitting?

What is an exceptable solution for this? Everything i think up feels
like a hack and not a clean standard way to do it. The scenario is:
Client has a large amount of data to send, multiple class objects, and
wants to send them all at once. Server needs to accept it all and then
do something with it.

I can't use remoting because it's not supported on the compact
framework.

Thanks!

Apr 19 '06 #2
On 19 Apr 2006 12:41:25 -0700, "Yossarian" <Ar********@gma il.com>
wrote:
Here's the problem. I have the server accepting connections fine, but
i'm a little confused how to actually send the server data, and then
how the server should receive the data. The problem: If i don't put some sort of Thread.Sleep(10 ) the server
decodes the bytes faster than the client sends them so i don't recieve
all the data.


There are pretty much exactly four solutions to this problem:

1) Have the client close the connection after all data is sent, so the
server can simply read until the stream is closed.

2) Have the client send the length of content before the content
itself. This is the approach used by HTTP, for example, in the
Content-Length header.

3) Have the client send some kind of "end of data chunk" marker, and
have the server recognize it. This is the approach used by HTTP for
recognizing the end of headers and the start of content data: a blank
line after the headers indicates the end of headers.

4) Use a packet-based approach where data lumps are of a fixed size,
so both client and server know what to do, and pad out the data if it
is the wrong size.
-- Barry
Apr 20 '06 #3
On 19 Apr 2006 12:41:25 -0700, "Yossarian" <Ar********@gma il.com>
wrote:
do
{
stream.Read(pac ket, 0, packet.Length);
quote = decoder.decode( packet);
*do something with quote here*
Thread.Sleep(10 );

} while (stream.DataAva ilable);


By the way, you don't want to read data like this. You need to read
somewhat like:

MyPacketBuilder packetBuilder = new MyPacketBuilder ();
while (packetBuilder. ReadFromStream( stream))
{
foreach (Packet packet in packetBuilder.P ackets)
ProcessPacket(p acket);
packetBuilder.P ackets.Clear();
}

Where:
MyPacketBuilder - something which can read partial packets from a
stream and assemble them into packets.
MyPacketBuilder .ReadFromStream () - reads as many bytes are available
from the stream and tries to assemble as many packets from the bytes
read as possible, and returns false if the stream is closed (a call to
the Stream returned 0).
MyPacketBuilder .Packets - a collection of packets assembled from the
data read from the stream.

In other words, you need to perform buffer management, and
incrementally build your packets from the little byte array slices you
read from the TCP stream.

The loop above assumes a thread is dedicated to the TCP connection.
You can rework it to work with asynchronous methods like BeginRead()
etc., but I'd leave that until (1) you get it working and (2) you are
unhappy with the scalability, because working with asynchronous
methods can be quite tricky.
-- Barry
Apr 20 '06 #4

"Barry Kelly" <ba***********@ gmail.com> wrote in message
news:kl******** *************** *********@4ax.c om...
On 19 Apr 2006 12:41:25 -0700, "Yossarian" <Ar********@gma il.com>
wrote:
Here's the problem. I have the server accepting connections fine, but
i'm a little confused how to actually send the server data, and then
how the server should receive the data.

The problem: If i don't put some sort of Thread.Sleep(10 ) the server
decodes the bytes faster than the client sends them so i don't recieve
all the data.


There are pretty much exactly four solutions to this problem:

1) Have the client close the connection after all data is sent, so the
server can simply read until the stream is closed.

2) Have the client send the length of content before the content
itself. This is the approach used by HTTP, for example, in the
Content-Length header.

3) Have the client send some kind of "end of data chunk" marker, and
have the server recognize it. This is the approach used by HTTP for
recognizing the end of headers and the start of content data: a blank
line after the headers indicates the end of headers.

4) Use a packet-based approach where data lumps are of a fixed size,
so both client and server know what to do, and pad out the data if it
is the wrong size.
-- Barry


All of these still require you to accumulate data as you describe in your
other post.

Even with 4 using small "packets" it is entirely legal and possible
(although hugely unlikely) for an intermediate network node to chop it up.

There is no way around this because TCP is just a stream.
Apr 20 '06 #5
On Thu, 20 Apr 2006 08:08:57 GMT, "Nick Hounsome"
<Ne**@NickHouns ome.Me.Uk> wrote:
Even with 4 using small "packets" it is entirely legal and possible
(although hugely unlikely) for an intermediate network node to chop it up.

There is no way around this because TCP is just a stream.


I'm aware of that. I noticed the OP's read loop late, and that's why I
posted the second reply.

-- Barry
Apr 20 '06 #6

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

Similar topics

5
2561
by: Matt | last post by:
I think this is the basic concept in ASP server-side development. My boss told me web application is NOT client-server application. I argued with him because browser is the client, and the server code put in server. Then web application should be a client-server application. My understanding is that a web application is an application that runs on a browser. But client-server application is not necessary a web application. Please...
18
7365
by: cjl | last post by:
Hey all: I know that it is silly in the age of Google to 'lose' something on the internet, but I recently checked out a project that had implemented a database with a subset of SQL in pure client-side javascript. I forgot to bookmark it, and now I can't find it. Anyone?
7
3361
by: CT | last post by:
Hi, This might seem like a basic question but I have some doubts, please humour me. I have a client-server application using java where each client on each machine needs to directly communicate directly with the database. Do I need a separate db2 connect on each such machine. Please advice.
12
2985
by: ShepardBerry | last post by:
This may be a dumb question, but I'm not finding anything specifically what I'm looking for. Still kind of new to .NET as well. What I'm trying to do that I know I could do in VB6.0/ASP is to create a client side object set some properties and have it run some code. I know that in VB6/asp it was fairly straight forward by creating an ActiveX control, register it and then VBScripting the CreateObject("xxx.xxxx") command and it worked...
3
2655
by: Marc Gravell | last post by:
Kind of an open question on best-practice for smart-client design. I'd really appreciate anyones views (preferably with reasoning, but I'll take what I get...). Or if anybody has any useful links on the subject? (and yes, I have already googled it at length, but still no strong decision) ============= After a long stint of pure-desktop / pure-server applications, I'm currently working on a number of smart-client projects in C# using...
9
2409
by: CGW | last post by:
I asked the question yesterday, but know better how to ask it, today: I'm trying to use the File.Copy method to copy a file from a client to server (.Net web app under IIS ). It looks to me that when I give a path like @"C:\holdfiles\myfile.txt" it looks on the server C drive. How do I pull from the client? Do I need a different class and/or method? Filestream? -- Thanks,
1
1335
by: Frank Millman | last post by:
Hi all I am developing a multi-user business/accounting application. It is coming along nicely :-), though rather slowly :-( I have hit an issue which will require a lot of changes to the code I have written so far, together with an increase in complexity and all the bad things that follow from that. Before I go ahead and make the changes, I thought I would bounce it off the group and see if there is a simpler approach.
2
4710
by: Wimpie van Lingen | last post by:
Hey I have some more questions with regards to Remoting in .NET 2. I'm using TCP with the Binary formatter. My solution consists of 4 projects: - Class Library containing the server classes which Inherits MarshalByRefObject (ok, at this stage it only contains one class... but its gonna grow) - Class Library containing common classes and interfaces that will be shared between all projects. This include interfaces for the server...
11
4875
by: Jeff | last post by:
Hello everyone. I've searched through the archives here, and it seems that questions similar to this one have come up in the past, but I was hoping that I could pick your Pythonic brains a bit. Here's a broad overview of what I need to do: cross-platform, client- side GUI apps that interact with a server backed by a database. I'd also like the possibility of having a web interface for small portions of the app. It will be a fairly...
6
4909
by: 7stud | last post by:
My question pertains to this example: #!/usr/bin/env python import socket, sys, time host = sys.argv textport = sys.argv s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
0
8428
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8339
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8751
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8535
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8629
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4338
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.