Newbie: Could use tutorial guidance of experienced TCP/IP sockets programmer
by Ramon F Herrera » Sun, 13 Sep 2009 00:32:02 GMT
Hello all,
I have been programming under U*ix for ages, but this is the first
time I have gathered the courage to attempt dealing with such scary
black art as sockets. :^)
As an introductory, tutorial-type material, these programs are great
(see simplest one below):
http://www.**--****.com/
I am trying to take the 'daytime' code for client and server and adapt
them to my needs.
My main problem is that such code was designed to transfer a short
string from the server to the client. Using my own data, the client
and server handle properly a few lines being transferred, but as soon
as the data is more than a handful lines, some data is lost.
Note: The transfer (request) from the client is short -just a few
parameters- but the server may return about 1K bytes.
It seems that the client is being overwhelmed by the server's
response. Reading at the docs I am guessing that the limitation is
caused by something called "short read".
Perhaps I need a larger buffer or something.
Conceptual info is most appreciated...
Thanks for your kind assistance,
-Ramon
-----------------------------
#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
std::string make_daytime_string()
{
using namespace std; // For time_t, time and ctime;
time_t now = time(0);
return ctime(&now);
}
int main()
{
try
{
boost::asio::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13));
for (;;)
{
tcp::socket socket(io_service);
acceptor.accept(socket);
std::string message = make_daytime_string();
boost::system::error_code ignored_error;
boost::asio::write(socket, boost::asio::buffer(message),
boost::asio::transfer_all(), ignored_error);
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
Re: Newbie: Could use tutorial guidance of experienced TCP/IP sockets programmer
by Ramon F Herrera » Sun, 13 Sep 2009 00:42:39 GMT
> using namespace std; // For time_t, time and ctime;
>> time_t now = time(0);>
> return ctime(&now)>
> >
> int main(>
> >
> t>y
> > {
> boost::asio::io_service io_ser>ic>;
>
> tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4()> 1>));
>
> gt;for (;>)
> {
> tcp::socket socke>(io_service);
> accepto>.a>cept(socket);
>
> std::string message = ma>e_>aytime_string();
>
> boost::system::er>or_code ignored_error;
> boost::asio::write(socket, boo>t::asio::buffer(message),
> boost::asio::>ransfe>_all(>, ignored_error);
> }
>>}
> {
><gt;std>:c>rr << e.what(> <> std>:e>dl;
> }
>
> return 0;
>
> }
>
>
Please don't tell me go and read the Stevens books. I have them, but
they are in a different continent right now. :-)
All I need is a little nudge in the right direction...
TIA,
-Ramon
http://www.**--****.com/
Re: Newbie: Could use tutorial guidance of experienced TCP/IP sockets programmer
by Ramon F Herrera » Sun, 13 Sep 2009 00:58:04 GMT
> > using namespace std; // For time_t, time and ctime;
>>>> time_t now = time(0);>
> > return ctime(&now)>
>
>> > int main(>
>> > >
>> > t>y>
> > > >
> > boost::asio::io_service io_ser>ic>;>
>
> > tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4()> 1>)>;
>
> > gt;f>r (;;)>
> > {
> > tcp::socket socke>(>o_service);
> > accepto>.a>c>pt(socket);
>
> > std::string message = ma>e_>a>time_string();
>
> > boost::system::er>o>_code ignored_error;
> > boost::asio::write(socket, boo>t>:asio::buffer(message),
> > boost::asio::>r>nsfer_>l>(), i>n>red_error);
> > }
> > gt;}>
> > > >atch (std::exception& e)
> > lt;{
> > g<<gt;std::>er> >< e.what() <<>st>:>endl>
>
> > return 0;
>
> > }
>
> Please don't tell me go and >ead the Stevens books. I have them, but
> they are>in>a different continent right now. :-)
>
> All I need is > l>ttle nu>ge>in the ri>ht>direction...
>
> TIA,
>
> -Ramon
>
> http://www.**--****.com/
More questions:
In my preliminary client & server versions, I will send the data in
clear text, but a future version will be based on SSL.
Given that future direction (SSL on Version 2), should I base my
Version 1 on:
(1) UDP
(2) TCP
(3) Irrelevant
-Ramon
Re: Newbie: Could use tutorial guidance of experienced TCP/IP sockets programmer
by Tom Einertson » Sun, 13 Sep 2009 02:25:08 GMT
If you plan to convert to SSL eventually, then I would use TCP for Version
1. Although there are
differences between TCP and SSL (SSL is more complicated) both of these
protocols provide the
same type of service (stream). UDP provides a datagram service, so you
have to deal with a
different set of issues.
--
Tom Einertson E-mail: XXXX@XXXXX.COM
SIEMENS Power Transmission & Distribution Phone: (952) 607-2244
Energy Management & Automation Division Fax: (952) 607-2018
10900 Wayzata Boulevard, Suite 400
Minnetonka, MN, 55305
Re: Newbie: Could use tutorial guidance of experienced TCP/IP sockets programmer
by Ramon F Herrera » Sun, 13 Sep 2009 06:30:14 GMT
> SIEMENS Power Transmission & Distribution hone: gt;952) 607-2244
> Energy Management & Automation Division ax: > 952) 607-2018
> 10900 Wayzata Bou>evard, Suite 400
> Minnetonka, MN, 55305
Thanks, Tom. You confirmed my suspicions. I have always wondered why:
- IP has an encrypted equivalent: IPsec
- TCP has an encrypted equivalent: SSL
but there is no such thing as an encrypted UDP.
Regards,
-Ramon
Re: Newbie: Could use tutorial guidance of experienced TCP/IP sockets programmer
by Ramon F Herrera » Sun, 13 Sep 2009 06:36:19 GMT
> Try a 'using namespace boost::asio' here.
> I don't mind a bit of C++ namespace qualification,
> but three or four levels of colons in your
> code are just too much for me.
Your request is being referred to Christopher M. Kohlhoff, the author
of Boosto.Asio. :-)
http://www.**--****.com/
I copied and pasted that program. Chose the simplest of the 7
provided. I assume he used that style for didactic reasons.
-Ramon
Re: Newbie: Could use tutorial guidance of experienced TCP/IP sockets programmer
by David Schwartz » Tue, 15 Sep 2009 14:08:58 GMT
> IP as an encrypted equivalent: IPsec
>> TCP has an encrypted equivalent: SSL>
>>
> but there is no such thing as an encrypted UDP.
DTLS can thought of as an encrypted UDP.
http://www.**--****.com/
There are, to some extent, good reasons why encrypted datagram
protocols aren't popular. Part of it is that most of the advantages of
datagram protocols are hard to replicate in a secure protocol. For
example, designing a secure broadcast protocol is non-trivial, and
would require some other channel to negotiate the keys. Also, unless
you implement your own retransmissions and acknowledgements, keeping
the encryption context the same on both ends becomes challenging.
DS
Re: Newbie: Could use tutorial guidance of experienced TCP/IP sockets programmer
by Nick Keighley » Tue, 15 Sep 2009 17:42:26 GMT
<snip>
> The read_some operation may not read all of the req>ested
> number>of>bytes.
>
> This being a property your [] library share> with the
> underlying system call. TCP does not preserve message bounda>ies which
> implies that, if you need to read 'messages', you mus> design a
> protocol such that the end of a message can be rec>gnized by
> inspecting the data recevied so far and continue to call >ead until
> such an end of message indication was seen.
Note "end of message indication" could be a count at the beginning of
the message. Otherwise you have to do "byte-stuffing" or such like to
avoid false positives on EOM detection.
Similar Threads:
1.Newbie: Could use tutorial guidance of experienced TCP/IP sockets programmer
2.Not So Newbie Anymo "Could use tutorial guidance of experienced TCP/IP sockets programmer" problem solved!
3.Not So Newbie Anymo "Could use tutorial guidance of experienced TCP/IP sockets programmer" problem solved!
4.Newbie TCP/IP Socket Question
I know that in TCP sockets, when the server accepts a connection from
the client, the server creates a new socket descriptor. This new
socket descriptor is then used in subsequent communication with the
client. My question is this:
What is the port number that the server uses for subsequent
communication with the client?
According to Unix Network Programming by Stevens (old edition ----
i.e. the single volume book), accept() creates a new socket with the
"same properties" as the original socket. To me the phrase "same
properties" implies that the port number is the same in both the
cases. In other words, if the server is waiting to accept new
connections on port number 12345, after the new socket has been
established, does the server still use port number 12345?
Thanks,
NS
5.Newbie TCP/IP Socket Question # 2
Since my last posting, I have run some experiments with TCP sockets
and I am now slightly better informed [(or perhaps more confused;-)].
However, some doubts persist. Here's my new question:
Suppose a client on host 43.44.45.46 wants to connect to the server on
host 146.145.144.143, which is listening on port number 20000. The
client opens a socket and issues a connect request for
146.145.144.143:20000. When the server accepts the connection
request, it returns a new socket descriptor. Let us assume that the
TCP/IP software running on host 146.145.144.143 (the server) assigns a
port number of 30000 to this new socket. Now what is happening is
that while the client is sending the request to 146.145.144.143:20000,
the server is receiving this request on 146.145.144.143:30000. I
wonder which entity in the server domain (146.145.144.143) is
performing the translation of port number 20000 to port number 30000
My apologies if this question sounds stupid, but I believe that there
is no such thing as a stupid question.
Thanks,
NS
~~~~~~~~~~~~~~ original posting snippet begin
XXXX@XXXXX.COM (Nimmi Srivastav) wrote in message news:< XXXX@XXXXX.COM >...
> I know that in TCP sockets, when the server accepts a connection from
> the client, the server creates a new socket descriptor. This new
> socket descriptor is then used in subsequent communication with the
> client. My question is this:
>
> What is the port number that the server uses for subsequent
> communication with the client?
>
~~~~~~~~~~~~~~ original posting snippet end
6. Need C# programmer ASAP, DICOM experience a plus