TCP Client in C
Shared by: devcanvas
c
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
How It Works
-
Socket Creation:
- The
socketfunction 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_instructure.
- The server’s IP address and port are defined in a
-
Connection:
- The
connectfunction establishes a connection to the specified server address.
- The
-
Communication:
- The
sendfunction transmits data to the server. - The
recvfunction waits for a response from the server.
- The
-
Cleanup:
- The
closefunction 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.