TCP Client in C
Shared by: devcanvas
c
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
How It Works
-
Socket Creation:
- The
socket
function initializes a socket using IPv4 (AF_INET
) and TCP (SOCK_STREAM
).
- The
-
Server Address Setup:
- The server’s IP address and port are defined in a
sockaddr_in
structure.
- The server’s IP address and port are defined in a
-
Connection:
- The
connect
function establishes a connection to the specified server address.
- The
-
Communication:
- The
send
function transmits data to the server. - The
recv
function waits for a response from the server.
- The
-
Cleanup:
- The
close
function releases the socket resources.
- The
Example Usage
- Server: Set up a simple TCP server on port
8080
(e.g., using Python or another tool). - Client: Compile and run this program to connect to the server, send a message, and print the server’s response.