Read HTTP Request From TCP Socket Stream In Java (TCP Socket Programming)

Опубликовано: 29 Сентябрь 2024
на канале: Cyecize
1,967
49

Here you will find out how Web servers process incoming requests from the browser.
It will be a plus if you have basic understanding of the HTTP protocol.
In this video we are creating a ServerSocket that is listening on a specific port.
When a new connection is received, we pass it to a new thread which then reads the content of the stream and parses it into our beloved HTTP format.
Initially the server is written in a way that is as straightforward as possible until we reach an issue while sending large HTTP requests.
The tricky part is the async nature of TCP sockets. InputStream.available() DOES NOT WORK on TCP Socket streams, so we come up with a more cleaver way to read the request.

Link to MDN about HTTP Structure:
https://developer.mozilla.org/en-US/d...

Link to Github Repo:
https://github.com/Cyecize/HTTP-TCP-S...

Timecodes:
0:00 - Intro
2:07 - What is HTTP
4:51 - Project Skeleton
5:56 - Create Server On Port 8080
9:46 - Spawn a Thread for each connection
11:38 - Incorrectly read TCP Stream
16:49 - Parse HTTP Request
23:17 - Serving Files
33:10 - Uploading Files
39:00 - The Issue with TCP Streams
43:06 - Properly read TCP Stream
52:48 - Test
54:51 - Outro
56:03 - Custom Web Server