Questions tagged [tcp]

Transmission Control Protocol (TCP) is a transport layer protocol that provides a connection-oriented data stream service with delivery that is guaranteed to be in-order.

Transmission Control Protocol (TCP) is a transport layer protocol that provides a connection-oriented data stream service with delivery that is guaranteed to be in-order on top of the underlying packet-oriented, unreliable IP layer. TCP is referred to as a connection-oriented protocol. This is opposed to UDP, which offers a relatively bare-bones unreliable all-or-nothing delivery of discrete packets and is referred to as a connection-less protocol.

How TCP fits into Internet protocol suite layers (going top to bottom):

  • Application: Encrypts data to be sent or sends data in a specific format (e.g. TLS or HTTPS)
  • Transport: Splits the data into segments and adds a TCP header to each (creating a TCP segment)
  • Internet: Encapsulates each segment (and splits if necessary) into IP packets (with source and destination IP address)
  • Link: Encapsulates each packet (and splits if necessary) and adds physical address (MAC)

The basic definition of TCP is given in RFC 793. There is more information at the Wikipedia article on TCP.

23633 questions
151
votes
18 answers

How to write a scalable TCP/IP based server

I am in the design phase of writing a new Windows service application that accepts TCP/IP connections for long running connections (i.e., this is not like HTTP where there are many short connections, but rather a client connects and stays connected…
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
143
votes
4 answers

How does the socket API accept() function work?

The socket API is the de-facto standard for TCP/IP and UDP/IP communications (that is, networking code as we know it). However, one of its core functions, accept() is a bit magical. To borrow a semi-formal definition: accept() is used on the server…
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
142
votes
20 answers

In C#, how to check if a TCP port is available?

In C# to use a TcpClient or generally to connect to a socket how can I first check if a certain port is free on my machine? more info: This is the code I use: TcpClient c; //I want to check here if port is free. c = new TcpClient(ip, port);
Ali
  • 5,286
  • 9
  • 33
  • 36
136
votes
2 answers

Can TCP and UDP sockets use the same port?

First of all, is there any problem with using both UDP and TCP on the same server? Secondly, can I use the same port number?
user800799
  • 2,883
  • 7
  • 31
  • 36
134
votes
12 answers

How should one go about choosing a default TCP/IP port for a new service?

When developing an app that will listen on a TCP/IP port, how should one go about selecting a default port? Assume that this app will be installed on many computers, and that avoiding port conflicts is desired.
Kevin Wong
  • 14,656
  • 11
  • 42
  • 52
132
votes
5 answers

Linux: is there a read or recv from socket with timeout?

How can I try to read data from socket with timeout? I know, select, pselect, poll, has a timeout field, but using of them disables "tcp fast-path" in tcp reno stack. The only idea I have is to use recv(fd, ..., MSG_DONTWAIT) in a loop
osgx
  • 90,338
  • 53
  • 357
  • 513
127
votes
7 answers

HTTP vs TCP/IP, send data to a web server

I'm currently working on a project where I need to use an Arduino Nano (http://arduino.cc/en/Main/arduinoBoardNano) to send data from a temperature sensor to a web server. At first I thought it would be easy, since there are so many great libraries…
Adam
  • 8,752
  • 12
  • 54
  • 96
118
votes
8 answers

When is TCP option SO_LINGER (0) required?

I think I understand the formal meaning of the option. In some legacy code I'm handling now, the option is used. The customer complains about RST as response to FIN from its side on connection close from its side. I am not sure I can remove it…
dimba
  • 26,717
  • 34
  • 141
  • 196
115
votes
8 answers

Does a TCP socket connection have a "keep alive"?

I have heard of HTTP keep-alive but for now I want to open a socket connection with a remote server. Now will this socket connection remain open forever or is there a timeout limit associated with it similar to HTTP keep-alive?
Kevin Boyd
  • 12,121
  • 28
  • 86
  • 128
115
votes
9 answers

How to convert from []byte to int in Go Programming

I need to create a client-server example over TCP. In the client side I read 2 numbers and I send them to the server. The problem I faced is that I can't convert from []byte to int, because the communication accept only data of type []byte. Is there…
Emanuel
  • 6,622
  • 20
  • 58
  • 78
111
votes
13 answers

TCP vs UDP on video stream

I just came home from my exam in network-programming, and one of the question they asked us was "If you are going to stream video, would you use TCP or UDP? Give an explanation for both stored video and live video-streams". To this question they…
Alxandr
  • 12,345
  • 10
  • 59
  • 95
110
votes
16 answers

Do I need to heartbeat to keep a TCP connection open?

I have two components that that communicate via TCP/IP. Component A acts as a server/listener and Component B is the client. The two should communicate as quickly as possible. There can only ever be one connection at any time (though that is…
Rob Gray
  • 3,186
  • 4
  • 33
  • 34
109
votes
5 answers

WebSockets ping/pong, why not TCP keepalive?

WebSockets have the option of sending pings to the other end, where the other end is supposed to respond with a pong. Upon receipt of a Ping frame, an endpoint MUST send a Pong frame in response, unless it already received a Close frame. It…
Thomas
  • 174,939
  • 50
  • 355
  • 478
105
votes
15 answers

What do you use when you need reliable UDP?

If you have a situation where a TCP connection is potentially too slow and a UDP 'connection' is potentially too unreliable what do you use? There are various standard reliable UDP protocols out there, what experiences do you have with them? Please…
Len Holgate
  • 21,282
  • 4
  • 45
  • 92
102
votes
1 answer

Does WebRTC use TCP or UDP?

I have some questions about WebRTC: Does WebRTC use TCP or UDP as its peer-to-peer transport? How can I know? I read that there are reliability mode and DTLS agreement, how does these affect? Is this transport the same for both Media and…
onmyway133
  • 45,645
  • 31
  • 257
  • 263