View Single Post
Old 07.11.2006, 20:23   #7
mcrabben7
Архивный участник
 
Join Date: Apr 2006
Posts: 461
Default

From Wikipedia under Transmission Control Protocol:
Quote:
The Transmission Control Protocol (TCP) is a virtual circuit protocol that is one of the core protocols of the Internet protocol suite, often simply referred to as TCP/IP. Using TCP, applications on networked hosts can create connections to one another, over which they can exchange streams of data. The protocol guarantees reliable and in-order delivery of data from sender to receiver. TCP also distinguishes data for multiple connections by concurrent applications (e.g. Web server and e-mail server) running on the same host.

TCP supports many of the Internet's most popular application protocols and resulting applications, including the World Wide Web, e-mail and Secure Shell.

In the Internet protocol suite, TCP is the intermediate layer between the Internet Protocol (IP) below it, and an application above it. Applications often need reliable pipe-like connections to each other, whereas the Internet Protocol does not provide such streams, but rather only unreliable packets. TCP does the task of the transport layer in the simplified OSI model of computer networks. The other main transport-level Internet protocol is UDP.


Applications send streams of octets (8-bit bytes) to TCP for delivery through the network, and TCP divides the byte stream into appropriately sized segments (usually delineated by the maximum transmission unit (MTU) size of the data link layer of the network the computer is attached to). TCP then passes the resulting packets to the Internet Protocol, for delivery through a network to the TCP module of the entity at the other end. TCP checks to make sure that no packets are lost by giving each packet a sequence number, which is also used to make sure that the data are delivered to the entity at the other end in the correct order. The TCP module at the far end sends back an acknowledgement for packets which have been successfully received; a timer at the sending TCP will cause a timeout if an acknowledgement is not received within a reasonable round-trip time (or RTT), and the (presumably lost) data will then be re-transmitted. The TCP checks that no bytes are damaged by using a checksum; one is computed at the sender for each block of data before it is sent, and checked at the receiver.
Also, versus UDP:
Quote:
The User Datagram Protocol (UDP) is one of the core protocols of the Internet protocol suite. Using UDP, programs on networked computers can send short messages sometimes known as datagrams to one another.

UDP does not provide the reliability and ordering guarantees that TCP does. Datagrams may arrive out of order or go missing without notice. Without the overhead of checking if every packet actually arrived, UDP is faster and more efficient for many lightweight or time-sensitive purposes. Also, its stateless nature is useful for servers that answer small queries from huge numbers of clients. Compared to TCP, UDP is required for broadcast (send to all on local network) and multicast (send to all subscribers).

Common network applications that use UDP include the Domain Name System (DNS), streaming media applications, Voice over IP, Trivial File Transfer Protocol (TFTP) and online games.
UDP is a packet-based protocol using datagrams and non-socket-based connections. UDP is a send and hope to God it gets there protocol.

TCP is a stream protocol because it makes sure you get all the packet data in order, on the same socket, with reliability. It does not break the stream up in any way. If you send 5600 bytes in 2 seconds and go to do a socket read, you will get 5600 bytes of data. This could be 1, 2, 500 packets. TCP does not care, because it only handles the stream. That's why it is up to you to read properly.

I'll show you some example C++ Winsock Receive code:
Quote:
short NetworkingMS::recvData(SOCKET RecvSocket, std::string * strToRecv)
{
short len = MAX_DATA_SIZE;
char buffer[1024];

if ((len = recv(RecvSocket, buffer, len, 0)) == SOCKET_ERROR)
{
std::cerr << "recv: " << WSAGetLastError() << "\n";
}

if( len > 0 )
{
std::string temp(buffer);
temp.resize(len);
*strToRecv = temp;
return len;
}
else if(len == 0)
{
std::cerr << "recv: remote host closed the connection.\n";
return len;
}
else
return -1;
}
Just read len from the packet and return it to a packet handler switch where you read the header from the next 2 bytes. My example code reads data up to max length, which isn't what you want, but this was part of a Library I made for one of my classes. I handled 2 packets in 1 stream inside my server code seperately.

Sorry if I came across as trying to be rude or something, but you were wrong and I had to try to explain to you the difference between a stream and packet protocol.
mcrabben7 is offline