4

I have already gone through some tutorials for socket but i couldn't get what it does. I want to know what sockets do and why is it used. This is the code I have referred.

client.php
<?php
$host    = "localhost";
$port    = 1024;
$message = "Hello Server";
echo "Message To server :".$message;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create              socket\n");
// connect to server
$result = socket_connect($socket, $host, $port) or die("Could not connect to  server\n");  
// send string to server
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
// get server response
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
echo "Reply From Server  :".$result;
// close socket
socket_close($socket);
?>

server.php
<?php
// set some variables
$host = "localhost";
$port = 1024;
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");

// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
echo "Client Message : ".$input;
// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write  output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
?>

So I couldn't get the idea of where to enter the server code and client code. Usually we write server code on what it should do while getting user input.So i am extremely confused about this. Can anyone help me? Thanks in advance

Sam
  • 57
  • 1
  • 1
  • 9
  • 1
    I'm voting to close this question as off-topic because it is there. – Martin James May 18 '16 at 12:48
  • If it is there can you please suggest where it is? It will be a great help. – Sam May 18 '16 at 13:25
  • Thank you @RyanVincent it's a very useful article to know about how two computers communicate. But they didn't said anything about sockets. So can u help me with that? Or suggest me any article to know about sockets and how to implement it. I already developed a chat application there i will save the message send from one user to the database and show it to the other user .The page will be refreshed using ajax. So what i have to do inorder to make the chat run through socket programming. Can you explain the flow? – Sam May 19 '16 at 11:21

2 Answers2

7

In order to understand sockets I think it's important to understand networking principles. Especially the Internet Protocol and Transmission Control Protocol.

The Transmission Control Protocol is a way of breaking up a message into smaller chunks, and addressing them in such a way that the chunks can be reliably re-assembled at the receiving end. The Internet Protocol is a way of routing these chunks through the Internet.

A Socket is just a programming object that manages the details of these protocols for you. You configure the socket to connect to a given port on a given IP address. The socket manages the rest: chunking, packaging, and labeling the data. The socket encapsulates all the protocol details so that you can abstract them away and act as if you are creating a "connection" from one computer to another. As a developer, you use sockets when you need to exchange information with another computer over the Internet.

For me, the idea of a socket and what it might be used for didn't make sense until I studied computer networking. (Especially the protocols themselves, not necessarily the practical, technician side of things.) You can start with the Wikipedia articles on TCP and IP. And you can try to read individual, piecemeal articles on the web. But frankly, networking is such a huge topic that I don't think anything short of a cohesive, semester-long course or a quality textbook would be enough to truly answer this question (and to correct the gaps, oversimplifications, and exceptions that I used to keep this answer simple.)

Syntax Junkie
  • 589
  • 6
  • 13
2

You need to understand the concept of socket programming. To get a better idea.

Sockets are used for interprocess communication. Interprocess communication is generally based on client-server model. In this case, client-server are the applications that interact with each other. Interaction between client and server requires a connection. Socket programming is responsible for establishing that connection between applications to interact.

Client application sends message($message) to server($host) and the server application receives it from the client through a port($port).

The client.php runs and sends the message from a client machine. The server.php runs on the server machine which receives the message.

Try these links for examples and how to run the server and client files.

http://www.binarytides.com/php-socket-programming-tutorial/
http://www.devshed.com/c/a/php/socket-programming-with-php/

zapping
  • 4,118
  • 6
  • 38
  • 56
  • 2
    "The client.php runs and sends the message from a client machine. The server.php runs on the server machine which receives the message." As php is a server-side scripting language we write server side scripting and where the client code should be written?. And what is the need to use sockets? What is the benefit of using socket programming and at what circumstances it can be used? Can You please explain these things? – Sam May 18 '16 at 12:08
  • 1
    php is a server side scripting language when it comes to web programming. php can be used as general purpose programming language in cases like for socket programming. A common example is a chat application where socket programming is used. The client.php in this case is used by users that log on to chat and the server.php will be receiving and routing the chat messages send by one user to another. Socket is virtually an end-point through with messages are exchanged after establishing a connection. http://stackoverflow.com/questions/152457/what-is-the-difference-between-a-port-and-a-socket – zapping May 18 '16 at 12:40
  • 1
    Exactly i have to implement socket programming in my chat application. There i will save the message send from one user to the database and show it to the other user .the page will be refreshed using ajax. So what i have to do inorder to make the chat run through socket programming. Can you explain the flow? At what point socket should be implemented either at the time of the saving data to the database or refreshing the page? – Sam May 18 '16 at 13:23
  • 1
    And one more thing what is the difference between socket and websocket? Just now i have seen this. I browsed as much as i could but couldn't get any appropriate answer. What is the difference between these things and which one is better to use? – Sam May 18 '16 at 13:45
  • 1
    The sockets are to be used when communicating from the client browser to server. That is when messages are to be passed between client and server. Once you have the messages on the server saving them to the database can be done as you do normally. Sockets are to be used just for passing the chat messages between server and the client/browser. – zapping May 24 '16 at 07:48
  • Websockets are in a way similar to sockets but the difference is websockets support http-compatible protocols for establishing the connection between client and server. This will enable you to pass data over the same web port instead of having to use a different port for communication. – zapping May 24 '16 at 07:49