Jump to content

Recommended Posts

Hey there. :)

 

I'm new to sockets in PHP programming. I've searched the web and found a tutorial, in which there is a coding example. This script is supposed to open a socket between the given IP and port that belongs to a Counter Strike server. I want to send a socket containing some basic input and retrieve it from the server again. However, i get this error..

 

Warning: socket_bind() [function.socket-bind]: unable to bind address [0]: The requested address is not valid in its context. in C:\wamp\www\cs_script\index.php on line 13
Could not bind to address

 

My code is:

 

<?php

// Set time limit to indefinite execution
set_time_limit (0);

// Set the ip and port we will listen on
$address = '178.236.69.70';
$port = 27015;

// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address');
// Start listening for connections
socket_listen($sock);

/* Accept incoming requests and handle them as child processes */
$client = socket_accept($sock);

// Read the input from the client &#8211; 1024 bytes
$input = socket_read($client, 1024);

// Strip all white spaces from input
$output = ereg_replace("[ \t\n\r]","",$input).chr(0);

// Display output back to client
socket_write($client, $output);

// Close the client (child) socket
socket_close($client);

// Close the master sockets
socket_close($sock); 

?>

 

Thanks in advantage.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.