Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
| Search | Today's Posts | Mark Forums Read
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 9th, 2003, 05:10 PM
Registered User
 
Join Date: Aug 2003
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Socket Programming

Hello everybody,

I've read the manual regarding sockets, followed the user contribution and read couple of tutorials inorder to get the very simple example working.I would be greatful to you if somebody can make this code working in the way I really want it to.

First attempt:
With this code, I'm able to create the connection and then i telnet to the ip-address and port no.Now, when i input a character,the connection is lost immediately:
Code:
<?php

// set some variables
$host = "127.0.0.1";
$port = 10000;

// 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");
echo "waiting for connections....\n";
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
echo "Received connection request\n";

// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");

// clean up input string
//$input = trim($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);
?>
Second attempt:
NOw with this code, its much better.when i try to input, it accepts and it says what i said.Actually,both the codes were supposed to accept a string and when i press enter, it is supposed to close the socket connection.
It does'nt happen in that way.
Code:
<?php
/*
* We don't want any time-limit for how the long can hang
* around, waiting for connections:
*/
      set_time_limit(0);

/* Create a new socket: */
    if(($sock = socket_create(AF_INET, SOCK_STREAM, 0)) < 0 )
    {
        print strerror( $sock ) . "\n";
        exit(1);
    }

/* Bind the socket to an address and a port: */
    if(($ret = socket_bind( $sock, "127.0.0.1", 10000 )) < 0 )
    {
        print strerror( $ret ) . "\n";
        exit(1);
    }

/*
* Listen for incoming connections on $sock.
* The '5' means that we allow 5 queued connections.
*/
    if(($ret = socket_listen( $sock, 5)) < 0 )
    {
        print strerror( $ret ) . "\n";
    }

/* Accept incoming connections: */
    if(($msgsock = socket_accept( $sock )) < 0)
    {
        print strerror( $msgsock ) . "\n";
        exit(1);    
    }

/* Send the welcome-message: */
    $message = "Welcome to my TCP-server!\n";
    if(($ret = socket_write( $msgsock, $message, strlen($message)) ) < 0 
)
    {
        print strerror( $msgsock ) . "\n";
        exit(1);
    }

/* Read/Receive some data from the client: */ 
    $buf = '';

    if(($buf = socket_read( $msgsock, 128 )) < 0 )
    {
        print strerror( $ret ) . "\n";
        exit(1);
    }

/* Echo the received data back to the client: */
    if(($ret = socket_write( $msgsock, "You said: $buf\0\n", strlen("You 
said: $buf\0\n")) ) < 0 )
    {
        print strerror( $ret ) . "\n";
        exit(1);
    }

/* Close the communication-socket: */            
    socket_close($msgsock );

/* Close the global socket: */
    socket_close($sock );
?>
I tested the above code(s) under windows-NT,XP and from the command prompt.
Thanks in advance for all your suggestions.
  #2 (permalink)  
Old August 18th, 2003, 03:59 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't know what problems you're experiencing... it seems to work for me. This is what I get when testing your example 2 on my Win2k command line.

telnet localhost 10000
Trying 127.0.0.1...
Connected to bigaction.
Escape character is '^]'.
Welcome to my TCP-server!
Hello, world!
You said: Hello, world!

Connection closed by foreign host.


I ran the code on the commandline using:
  php -q sock_test.php


Take care,

Nik
http://www.bigaction.org/


Similar Threads
Thread Thread Starter Forum Replies Last Post
Socket Programming lisamargaret C# 1 December 18th, 2007 10:20 AM
GUI Socket Programming Ewura BOOK: Beginning Java 2 0 November 11th, 2006 12:41 PM
Socket Programming ashu_from_india General .NET 0 June 28th, 2005 12:02 PM
Socket programming vinodkalpaka J2EE 0 June 16th, 2005 02:43 AM
Socket Programming in Window Service rbharatha VS.NET 2002/2003 0 July 15th, 2003 01:09 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.