A
A
Andrew2015-08-05 03:41:42
PHP
Andrew, 2015-08-05 03:41:42

Chat on socket. Logic for sending messages?

Good day, gentlemen.
Recently I came across such an interesting thing as sockets and decided to write a chat to better understand this issue.
What happened: I
wrote a server that creates a socket, gets ip, listens / binds connections.
Wrote a client that connects to the server.
Server:

set_time_limit(0);
$address = "127.0.0.1";
$port = 1000;

try {
    echo 'Create socket ... ';
    $socket_desc = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);//Создаем сокет.Получаем дескриптор для соединения.
    if (!$socket_desc) {
        throw new Exception('Ошибка создания сокета'.socket_last_error());
    }
    echo "Bind socket....";
    if(!socket_bind($socket_desc,$address,$port)){//Привязываем полученный дескриптор к ip ресурс.
        throw new Exception('Ошибка привязки сокета'.socket_last_error());
    }
    echo "Listen socket....";
    if(!socket_listen($socket_desc)){//Прослушиваем соединение на сокете
        throw new Exception('Ошибка послушки сокета'.socket_last_error());
    }
    echo "Accept socket....";
    do{
        $accept_socket = socket_accept($socket_desc);//Принимаем соединение.
        if(!$accept_socket){
            throw new Exception('Невозможно принять соединение'.socket_last_error());
        }
        $id = uniqid('user_');
        $msg = "Hello $id\n";
        socket_write($accept_socket, $msg);
    }
    while(true);
    socket_close($socket_desc);//Закрываем сокет
}
catch(Exception $e){
    echo $e->getMessage();
}

Customer:
$address = "127.0.0.1";
$port = 1000;
try{
    $client_socket_desc = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);//Создаем сокет.Получаем дескриптор для соединения.
    if (!$client_socket_desc) {
        throw new Exception('Ошибка создания сокета'.socket_strerror(socket_last_error()));
    }
    echo 'Соединение с сервером ... ';
    if (!socket_connect($client_socket_desc,$address,$port)) {
        throw new Exception('Ошибка соединения с сервером'.socket_last_error());
    }
    $txt = socket_read($client_socket_desc,2096,PHP_NORMAL_READ);//Зачитываем буфер сокета (Привет user_*********)
    echo $txt;

    socket_close($client_socket_desc);
}
catch(Exception $e){
    echo $e->getMessage();
}

What did not work:
The logic of sending messages from clients is not entirely clear; while the option is this:
We have an input field (ordinary input), from where we pull the data into the script with Ajax. We write the received data to the socket (on the server) and read / output through the client socket.
How true is this? Is it possible to do without Ajax, implement it through sockets?
Oftop
What is the difference between TCP sockets (which I currently use) and WebSockets, which is better? Could not find a clear explanation (

Thank you for your time.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DevMan, 2015-08-05
@Ashlst

Is it possible to do without Ajax, implement it through sockets?
in order to send something to the socket you need to somehow get it from the browser.
What is the difference between TCPsockets (which I currently use) and WebSockets, which is better?
which is better depends on the task.
you can read about the differences:
stackoverflow.com/questions/2681267/what-is-the-fu...
stackoverflow.com/questions/16945345/differences-b...
eng.kifi.com/websockets-vs-regular-sockets

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question