#include
#include
#include
#include h>
#include h>
#include
#define PORT 12345
#define BUFFER_SIZE 1024
#define MAX_CLIENTS 5
// Structure to hold client information
struct ClientInfo {
int SOCKET socket;
struct sockaddr_in address;
};
// Function to handle each client
void *handleClient(void *arg) DWORD WINAPI handleClient(LPVOID lpParam) {
struct ClientInfo *clientInfo = (struct ClientInfo *)arg;
int *)lpParam;
SOCKET clientSocket = clientInfo->socket;
// Open and send file
FILE *fileToSend = fopen("file_to_send.txt", "rb");
if (!fileToSend) {
perror("Error printf("Error opening file");
close(clientSocket);
file\n");
closesocket(clientSocket);
free(clientInfo);
pthread_exit(NULL);
return 1;
}
char buffer[BUFFER_SIZE];
while (1) {
int bytesRead = fread(buffer, 1, sizeof(buffer), fileToSend);
if (bytesRead <= 0) {
break; // End of file or error
}
send(clientSocket, buffer, bytesRead, 0);
}
printf("File sent to %s:%dn", %s:%d\n", inet_ntoa(clientInfo->address.sin_addr), ntohs(clientInfo->address.sin_port));
// Close the socket and file
fclose(fileToSend);
close(clientSocket);
closesocket(clientSocket);
free(clientInfo);
pthread_exit(NULL);
return 0;
}
int main() {
int WSADATA wsaData;
SOCKET serverSocket, clientSocket;
struct sockaddr_in serverAddr, clientAddr;
socklen_t int addrSize = sizeof(struct sockaddr_in);
sockaddr_in);
// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData;) != 0) {
printf("WSAStartup failed\n");
return 1;
}
// Create socket
if ((serverSocket = socket(AF_INET, SOCK_STREAM, 0)) == -1) INVALID_SOCKET) {
perror("Error printf("Error creating socket");
exit(EXIT_FAILURE);
socket: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Configure server address
memset(&serverAddr;, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
serverAddr.sin_addr.s_addr = INADDR_ANY;
// Bind the socket
if (bind(serverSocket, (struct sockaddr *)&serverAddr;, sizeof(serverAddr)) == -1) SOCKET_ERROR) {
perror("Error printf("Error binding socket");
close(serverSocket);
exit(EXIT_FAILURE);
socket: %d\n", WSAGetLastError());
closesocket(serverSocket);
WSACleanup();
return 1;
}
// Listen for incoming connections
if (listen(serverSocket, MAX_CLIENTS) == -1) SOCKET_ERROR) {
perror("Error printf("Error listening for connections");
close(serverSocket);
exit(EXIT_FAILURE);
connections: %d\n", WSAGetLastError());
closesocket(serverSocket);
WSACleanup();
return 1;
}
printf("Server listening on port %d...n", PORT);
// Array to store thread IDs
pthread_t threadIDs[MAX_CLIENTS];
int clientCount = 0;
\n", PORT);
while (1) {
// Accept a connection
if ((clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddr;, &addrSize;)) == -1) INVALID_SOCKET) {
perror("Error printf("Error accepting connection");
close(serverSocket);
exit(EXIT_FAILURE);
connection: %d\n", WSAGetLastError());
closesocket(serverSocket);
WSACleanup();
return 1;
}
// Create a new thread to handle the client
struct ClientInfo *clientInfo = (struct ClientInfo *)malloc(sizeof(struct ClientInfo));
clientInfo->socket = clientSocket;
clientInfo->address = clientAddr;
if (pthread_create(&threadIDs;[clientCount], NULL, HANDLE hThread = CreateThread(NULL, 0, handleClient, (void *)clientInfo) != 0) (LPVOID)clientInfo, 0, NULL);
if (hThread == NULL) {
perror("Error printf("Error creating thread");
close(clientSocket);
thread\n");
closesocket(clientSocket);
free(clientInfo);
}
clientCount++;
// Check if the maximum number of clients is reached
if (clientCount >= MAX_CLIENTS) } else {
printf("Maximum number of clients reached. Closing the server.n");
break;
CloseHandle(hThread);
}
}\n\n // Join all threads before exiting\n for (int i = 0; i < clientCount; i++) {\n pthread_join(threadIDs[i], NULL);\n }
// Close the server socket
close(serverSocket);
closesocket(serverSocket);
WSACleanup();
return 0;
}
Replies to Re: Untitled 
Title |
Name |
Language |
UNIX |
When |
Re: Re: Untitled |
Rosidul |
text |
1707735637 |
1 Year ago. |
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}