Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
Password Reminder
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #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:

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

// don't timeout!

// 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
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.
* We don't want any time-limit for how the long can hang
* around, waiting for connections:

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

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

* 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";

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

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

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

/* 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";

/* 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.
Reply With Quote
  #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

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
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,

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 10:02 AM.

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