NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/select.h>

#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024

int main() {
int server_socket, client_sockets[MAX_CLIENTS];
struct sockaddr_in server_addr, client_addr;
fd_set readfds;
char buffer[BUFFER_SIZE];
int max_clients = MAX_CLIENTS;
int max_sd, activity, i, valread, sd, addrlen;

// Sunucu soketini oluştur
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("Sunucu soketi oluşturma hatası");
exit(EXIT_FAILURE);
}

// Sunucu adresi ve bağlantı bilgilerini ayarla
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(12345);

// Sunucu soketini adres ve bağlantı bilgileriyle bağla
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Sunucu soketi bağlama hatası");
exit(EXIT_FAILURE);
}

// Sunucuyu dinle
if (listen(server_socket, 5) < 0) {
perror("Sunucu dinleme hatası");
exit(EXIT_FAILURE);
}

printf("Sunucu başlatıldı ve istemcilere bağlantı bekleniyor...n");

// İstemci soketlerini ve bunların veri okuma yazma durumlarını takip etmek için bir liste oluştur
for (i = 0; i < max_clients; i++) {
client_sockets[i] = 0;
}

while (1) {
// Soket setini temizle
FD_ZERO(&readfds);

// Sunucu soketini sete ekle
FD_SET(server_socket, &readfds);
max_sd = server_socket;

// Mevcut istemci soketlerini sete ekle
for (i = 0; i < max_clients; i++) {
sd = client_sockets[i];

// Geçerli bir soketse sete ekle
if (sd > 0) {
FD_SET(sd, &readfds);
}

// En büyük soket numarasını güncelle
if (sd > max_sd) {
max_sd = sd;
}
}

// Aktiviteyi bekle, süre olmadan
activity = select(max_sd + 1, &readfds, NULL, NULL, NULL);
if ((activity < 0) && (errno != EINTR)) {
printf("select çağrısı başarısızn");
}

// Yeni bir bağlantı talebi var mı?
if (FD_ISSET(server_socket, &readfds)) {
int new_socket;
addrlen = sizeof(client_addr);

// Yeni bir istemci bağlantısını kabul et
new_socket = accept(server_socket, (struct sockaddr *)&client_addr, (socklen_t*)&addrlen);
if (new_socket < 0) {
perror("Bağlantı kabul hatası");
exit(EXIT_FAILURE);
}

printf("Yeni bir istemci bağlandı, soket numarası: %d, IP: %s, Port: %dn", new_socket, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));

// İstemci soketini listeye ekle
for (i = 0; i < max_clients; i++) {
if (client_sockets[i] == 0) {
client_sockets[i] = new_socket;
break;
}
}
}

// İstemciden gelen veriyi oku ve diğer istemcilere gönder
for (i = 0; i < max_clients; i++) {
sd = client_sockets[i];

if (FD_ISSET(sd, &readfds)) {
valread = read(sd, buffer, BUFFER_SIZE);
if (valread == 0) {
// İstemci bağlantısı koptuysa, soketi kapat
getpeername(sd, (struct sockaddr*)&client_addr, (socklen_t*)&addrlen);
printf("İstemci ayrıldı, IP: %s, Port: %dn", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
close(sd);
client_sockets[i] = 0;
} else {
// Gelen veriyi diğer istemcilere gönder
for (int j = 0; j < max_clients; j++) {
if (client_sockets[j] != server_socket && client_sockets[j] != sd) {
send(client_sockets[j], buffer, valread, 0);
}
}
}
}
}
}

return 0;
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.