1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #define PORT 12345
- #define BUFFER_SIZE 1024
- int main()
- {
- int server_socket, client_socket;
- struct sockaddr_in server_addr, client_addr;
- socklen_t client_addr_len;
- char buffer[BUFFER_SIZE];
- if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
- {
- perror("Socket creation failed");
- exit(EXIT_FAILURE);
- }
- // Define server address
- server_addr.sin_family = AF_INET;
- server_addr.sin_addr.s_addr = INADDR_ANY;
- server_addr.sin_port = htons(PORT);
- // Bind the socket to the address and port
- if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
- {
- perror("Bind failed");
- close(server_socket);
- exit(EXIT_FAILURE);
- }
- // Listen for incoming connections
- if (listen(server_socket, 5) < 0)
- {
- perror("Listen failed");
- close(server_socket);
- exit(EXIT_FAILURE);
- }
- printf("Server listening on port %d...\n", PORT);
- // Accept incoming client connection
- client_addr_len = sizeof(client_addr);
- if ((client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len)) < 0)
- {
- perror("Client connection failed");
- close(server_socket);
- exit(EXIT_FAILURE);
- }
- printf("Client connected. Waiting for message...\n");
- // Receive message from the client
- //int n = recv(client_socket, buffer, BUFFER_SIZE, 0);
- //if (n < 0)
- // {
- // perror("Receive failed");
- // close(client_socket);
- // close(server_socket);
- // exit(EXIT_FAILURE);
- //}
- //buffer[n] = '\0'; // Null-terminate the received string
- //printf("Received from client: %s\n", buffer);
- // Send response to the client
- //const char *response = "Hello, Client! Your message was received.";
- //send(client_socket, response, strlen(response), 0);
- // printf("Response sent to client.\n");
- // Close the sockets
- close(client_socket);
- close(server_socket);
- return 0;
- }
|