UDP Socket Programming
UDP Server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#include <iostream> #include <cstring> #include <arpa/inet.h> #include <unistd.h> using namespace std; int main() { int sockfd; struct sockaddr_in serverAddr, clientAddr; char buffer[1024]; socklen_t addrLen = sizeof(clientAddr); // Create UDP socket if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { cerr << "Socket creation failed" << endl; return -1; } // Setup server address memset(&serverAddr, 0, sizeof(serverAddr)); serverAddr.sin_family = AF_INET; serverAddr.sin_addr.s_addr = INADDR_ANY; serverAddr.sin_port = htons(8080); // Bind socket to address if (bind(sockfd, (const struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) { cerr << "Bind failed" << endl; close(sockfd); return -1; } cout << "UDP Server is listening on port 8080" << endl; while (true) { int len = recvfrom(sockfd, (char *)buffer, 1024, 0, (struct sockaddr *) &clientAddr, &addrLen); buffer[len] = '\0'; cout << "Client: " << buffer << endl; string response = "Message received"; sendto(sockfd, response.c_str(), response.length(), 0, (struct sockaddr *) &clientAddr, addrLen); } close(sockfd); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include <iostream> #include <cstring> #include <arpa/inet.h> #include <unistd.h> using namespace std; int main() { int sockfd; struct sockaddr_in serverAddr; char buffer[1024]; string message = "Hello from UDP client"; // Create UDP socket if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { cerr << "Socket creation failed" << endl; return -1; } // Setup server address memset(&serverAddr, 0, sizeof(serverAddr)); serverAddr.sin_family = AF_INET; serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); serverAddr.sin_port = htons(8080); // Send message to server sendto(sockfd, message.c_str(), message.length(), 0, (const struct sockaddr *) &serverAddr, sizeof(serverAddr)); // Receive response from server socklen_t len = sizeof(serverAddr); int n = recvfrom(sockfd, (char *)buffer, 1024, 0, (struct sockaddr *) &serverAddr, &len); buffer[n] = '\0'; cout << "Server: " << buffer << endl; close(sockfd); return 0; } |
TCP Socket Programming
TCP Server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#include <iostream> #include <cstring> #include <arpa/inet.h> #include <unistd.h> using namespace std; int main() { int serverFd, newSocket; struct sockaddr_in serverAddr, clientAddr; char buffer[1024]; socklen_t addrLen = sizeof(clientAddr); // Create TCP socket if ((serverFd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { cerr << "Socket creation error" << endl; return -1; } // Setup server address memset(&serverAddr, 0, sizeof(serverAddr)); serverAddr.sin_family = AF_INET; serverAddr.sin_addr.s_addr = INADDR_ANY; serverAddr.sin_port = htons(8081); // Bind socket to address if (bind(serverFd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) { cerr << "Bind failed" << endl; close(serverFd); return -1; } // Listen for incoming connections if (listen(serverFd, 3) < 0) { cerr << "Listen failed" << endl; close(serverFd); return -1; } cout << "TCP Server is listening on port 8081" << endl; // Accept a connection if ((newSocket = accept(serverFd, (struct sockaddr *)&clientAddr, &addrLen)) < 0) { cerr << "Accept failed" << endl; close(serverFd); return -1; } while (true) { int valread = read(newSocket, buffer, 1024); buffer[valread] = '\0'; cout << "Client: " << buffer << endl; string response = "Message received"; send(newSocket, response.c_str(), response.length(), 0); } close(newSocket); close(serverFd); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#include <iostream> #include <cstring> #include <arpa/inet.h> #include <unistd.h> using namespace std; int main() { int sock = 0; struct sockaddr_in servAddr; char buffer[1024]; string message = "Hello from TCP client"; // Create TCP socket if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { cerr << "Socket creation error" << endl; return -1; } // Setup server address memset(&servAddr, 0, sizeof(servAddr)); servAddr.sin_family = AF_INET; servAddr.sin_port = htons(8081); // Convert IP address from text to binary if (inet_pton(AF_INET, "127.0.0.1", &servAddr.sin_addr) <= 0) { cerr << "Invalid address or address not supported" << endl; close(sock); return -1; } // Connect to server if (connect(sock, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0) { cerr << "Connection failed" << endl; close(sock); return -1; } // Send message to server send(sock, message.c_str(), message.length(), 0); // Receive response from server int valread = read(sock, buffer, 1024); buffer[valread] = '\0'; cout << "Server: " << buffer << endl; close(sock); return 0; } |
Explanation
- UDP Programming:
- UDP Server:
- Creates a UDP socket, binds it to port 8080, and listens for incoming messages.
- Receives messages from clients and sends a response.
- UDP Client:
- Creates a UDP socket, sends a message to the server on port 8080, and waits for a response.
- UDP Server:
- TCP Programming:
- TCP Server:
- Creates a TCP socket, binds it to port 8081, listens for incoming connections, and accepts a connection from a client.
- Reads messages from the client and sends a response.
- TCP Client:
- Creates a TCP socket, connects to the server on port 8081, sends a message, and waits for a response.
- TCP Server: