Skip to content

Instantly share code, notes, and snippets.

@diloabininyeri
Created December 28, 2023 09:45
Show Gist options
  • Save diloabininyeri/2f98cdd32ed5c9a5f5ec757a3210ea29 to your computer and use it in GitHub Desktop.
Save diloabininyeri/2f98cdd32ed5c9a5f5ec757a3210ea29 to your computer and use it in GitHub Desktop.
simple php socket server and client
<?php
$date = date('Y-m-d H:i:s');
$socket_client = stream_socket_client('tcp://localhost:8080');
while (true) {
fwrite($socket_client, "son gelen $date");
echo fread($socket_client, 1024).PHP_EOL;
sleep(1);
}
fclose($socket_client);
<?php
class SocketServer
{
private $ip;
private $port;
private $masterSocket;
private $clients = [];
private string $message;
public function __construct($ip, $port)
{
$this->ip = $ip;
$this->port = $port;
$this->createSocket();
$this->bindAndListen();
$this->acceptConnections();
}
private function createSocket()
{
$this->masterSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($this->masterSocket === false) {
die('Hata: Soket oluşturulamadı: ' . socket_strerror(socket_last_error()));
}
}
private function bindAndListen()
{
if (!socket_bind($this->masterSocket, $this->ip, $this->port)) {
die('Hata: Soket bağlanamadı: ' . socket_strerror(socket_last_error()));
}
if (!socket_listen($this->masterSocket)) {
die('Hata: Soket dinlenemedi: ' . socket_strerror(socket_last_error()));
}
echo "Sunucu $this->ip:$this->port adresinde dinleniyor.\n";
}
private function acceptConnections()
{
while (true) {
$readSockets = $this->getReadSockets();
if (socket_select($readSockets, $write, $except, 0) > 0) {
foreach ($readSockets as $socket) {
if ($socket === $this->masterSocket) {
$clientSocket = socket_accept($this->masterSocket);
$this->handleNewConnection($clientSocket);
} else {
$this->handleData($socket);
}
}
}
}
}
private function getReadSockets()
{
$readSockets = [$this->masterSocket];
foreach ($this->clients as $client) {
$readSockets[] = $client;
}
return $readSockets;
}
private function handleNewConnection($clientSocket)
{
$clientId = uniqid();
$this->clients[$clientId] = $clientSocket;
echo "Yeni bağlantı kabul edildi. İstemci ID: $clientId\n";
}
private function handleData($clientSocket)
{
$data = socket_read($clientSocket, 1024);
$this->message = $data;
if ($data === false || $data === '') {
$this->disconnectClient($clientSocket);
} else {
$message = "Server Response: $this->message";
$this->sendResponse($clientSocket, $message);
}
}
private function disconnectClient($clientSocket)
{
$clientId = array_search($clientSocket, $this->clients, true);
unset($this->clients[$clientId]);
socket_close($clientSocket);
echo "İstemci bağlantısı kapatıldı. İstemci ID: $clientId\n";
}
private function sendResponse($clientSocket, $message)
{
socket_write($clientSocket, $message, strlen($message));
}
private function sendToAllClients(string $message):void
{
foreach ($this->clients as $client) {
socket_write($client, $message);
}
}
}
// Kullanım
$ip = '127.0.0.1';
$port = 8080;
$server = new SocketServer($ip, $port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment