New Update Launched Contact Us Download Now!
Posts

2 way TCP

Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated

 

Server Code

c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> // For close() function
  • #include <stdio.h>: Includes the standard input/output library necessary for functions like printf and fgets.
  • #include <stdlib.h>: Includes the standard library header for functions like exit.
  • #include <string.h>: Includes the string library header for functions like memset and strlen.
  • #include <sys/socket.h>: Includes definitions for socket functions and structures.
  • #include <netinet/in.h>: Includes constants and structures needed for internet domain addresses.
  • #include <unistd.h>: Includes definitions for miscellaneous functions, including close.
c
#define PORT 8080
  • Defines a constant PORT with the value 8080, which will be used as the port number for the server.
c
int main() { struct sockaddr_in server, client; char buff[100]; int sid = socket(AF_INET, SOCK_STREAM, 0);
  • int main() {: Entry point of the program.
  • struct sockaddr_in server, client;: Declares structures to hold the server's and client's internet addresses.
  • char buff[100];: Declares a buffer to store messages sent and received.
  • int sid = socket(AF_INET, SOCK_STREAM, 0);: Creates a socket with:
    • AF_INET: Address family for IPv4.
    • SOCK_STREAM: Socket type for TCP.
    • 0: Default protocol for TCP.
c
if (sid == -1) { printf("Error in socket creation\n"); exit(1); }
  • Checks if sid is -1, indicating an error in socket creation. If so, prints an error message and exits the program with status 1.
c
server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons(PORT);
  • server.sin_family = AF_INET;: Sets the address family to IPv4.
  • server.sin_addr.s_addr = INADDR_ANY;: Binds the socket to all available network interfaces.
  • server.sin_port = htons(PORT);: Sets the port number to 8080, converting it to network byte order using htons.
c
if (bind(sid, (struct sockaddr*)&server, sizeof(server)) == -1) { printf("Error in bind\n"); close(sid); exit(1); }
  • Binds the socket to the address and port specified in the server structure. Checks if bind returns -1, indicating an error. If so, prints an error message, closes the socket, and exits the program.
c
listen(sid, 5);
  • Puts the socket into listening mode, ready to accept incoming connections. 5 specifies the maximum length of the queue of pending connections.
c
socklen_t len = sizeof(client); int temp = accept(sid, (struct sockaddr*)&client, &len);
  • socklen_t len = sizeof(client);: Initializes len to the size of the client structure.
  • int temp = accept(sid, (struct sockaddr*)&client, &len);: Accepts an incoming connection. Returns a new socket descriptor (temp) for the connection. If accept returns -1, it indicates an error.
c
if (temp == -1) { printf("Error in accept\n"); close(sid); exit(1); }
  • Checks if temp is -1, indicating an error in accepting the connection. If so, prints an error message, closes the listening socket, and exits the program.
c
while (1) { memset(buff, 0, sizeof(buff)); if (recv(temp, buff, sizeof(buff), 0) <= 0) { printf("Client disconnected\n"); break; } printf("Client: %s\n", buff);
  • while (1) {: Infinite loop for continuous communication.
  • memset(buff, 0, sizeof(buff));: Clears the buffer.
  • if (recv(temp, buff, sizeof(buff), 0) <= 0) {: Receives data from the client. If recv returns 0 or a negative value, it indicates the client has disconnected.
  • printf("Client: %s\n", buff);: Prints the received message from the client.
c
printf("Enter message to send: "); fgets(buff, sizeof(buff), stdin); if (send(temp, buff, strlen(buff), 0) == -1) { printf("Error in send\n"); break; } }
  • Prompts the user to enter a message to send to the client.
  • fgets(buff, sizeof(buff), stdin);: Reads a line of input from the user into buff.
  • if (send(temp, buff, strlen(buff), 0) == -1) {: Sends the message to the client. If send returns -1, it indicates an error.
c
close(temp); close(sid); return 0; }
  • close(temp);: Closes the connection socket.
  • close(sid);: Closes the listening socket.
  • return 0;: Exits the program with status 0.

Client Code

c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> // For close() function
  • Similar header inclusions as the server code for necessary functions and structures.
c
#define PORT 8080
  • Defines the port number 8080 to connect to the server.
c
int main() { struct sockaddr_in client; char buff[100]; int sid = socket(AF_INET, SOCK_STREAM, 0);
  • int main() {: Entry point of the program.
  • struct sockaddr_in client;: Declares a structure to hold the client's internet address.
  • char buff[100];: Declares a buffer to store messages sent and received.
  • int sid = socket(AF_INET, SOCK_STREAM, 0);: Creates a socket with similar parameters as the server.
c
if (sid == -1) { printf("Error in socket creation\n"); exit(1); }
  • Checks if sid is -1, indicating an error in socket creation. If so, prints an error message and exits the program.
c
client.sin_family = AF_INET; client.sin_addr.s_addr = INADDR_ANY; client.sin_port = htons(PORT);
  • client.sin_family = AF_INET;: Sets the address family to IPv4.
  • client.sin_addr.s_addr = INADDR_ANY;: Specifies that the client can connect to any available network interface.
  • client.sin_port = htons(PORT);: Sets the port number to 8080, converting it to network byte order using htons.
c
if (connect(sid, (struct sockaddr*)&client, sizeof(client)) == -1) { printf("Error in connect\n"); close(sid); exit(1); }
  • Attempts to connect to the server. Checks if connect returns -1, indicating an error. If so, prints an error message, closes the socket, and exits the program.
c
while (1) { printf("Enter message to send: "); fgets(buff, sizeof(buff), stdin); if (send(sid, buff, strlen(buff), 0) == -1) { printf("Error in send\n"); break; }
  • while (1) {: Infinite loop for continuous communication.
  • Prompts the user to enter a message to send to the server.
  • fgets(buff, sizeof(buff), stdin);: Reads a line of input from the user into buff.
  • if (send(sid, buff, strlen(buff), 0) == -1) {: Sends the message to the server. If send returns -1, it indicates an error.
c
memset(buff, 0, sizeof(buff)); if (recv(sid, buff, sizeof(buff), 0) <= 0) { printf("Server disconnected\n"); break; } printf("Server: %s\n", buff); }
  • memset(buff, 0, sizeof(buff));: Clears the buffer.
  • if (recv(sid, buff, sizeof(buff), 0) <= 0) {: Receives data from the server. If recv returns 0 or a negative value, it indicates the server has disconnected.
  • printf("Server: %s\n", buff);: Prints the received message from the server.
c
close(sid); return 0; }
  • close(sid);: Closes the socket.
  • return 0;: Exits the program with status 0.

Summary

  • The server sets up a listening socket and waits for client connections.
  • Upon accepting a connection, it enters a loop to receive and send messages.
  • The client creates a socket, connects to the server, and enters a loop to send and receive messages.
  • Both the client and server handle errors and clean up resources by closing sockets before exiting.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.