tags:

views:

111

answers:

0

Alright so i have the tcp listener, and when i recieve a tcp client i pass it to a class which uses the Streadread/writers to communicate:

EDIT:::

I forgot to mention that, they connect to each other fine, but just wont read/write to eachother..

C# side:

    private StreamReader srReceiver;
    private StreamWriter swSender;
    private NetworkStream stream;
    stream = tcpClient.GetStream();
    srReceiver = new System.IO.StreamReader(stream);
    swSender = new System.IO.StreamWriter(stream);

from there, i use a while loop to listen, and just flush data to the swSender like so:

Write:

    public void WriteLine(string str)
    {
        try
        {
            swSender.WriteLine(str);
            swSender.Flush();
        }
        catch (Exception e) { }
    }

Read:

    private void AcceptClient()
    {
        while (isConnected == true)
        {
            try
            {
                ProcessMessage(srReceiver.ReadLine());
            }
            catch (Exception i) { Dispose(); Console.WriteLine("aborted: "+i.ToString()); }
        }
    }

This method works fine communicating from C# -> C#, so im not sure why its not working C# -> PHP. here is a summary of my php code.

PHP side:

Write:

function Write($data=null)
{
    //** no connection is available, zero bytes can be sent. 
    if(!$this->isOpen())
    {
        $this->LastError = "No connection available for writing"; 
        return 0;
    }
    $data = strval($data); //** ensure that data is available. 
    if(strlen($data) == 0) //** no data to be sent. 
    return 0; //** zero bytes were sent. 
    else //** connection and data, set timeout and send. 
    { 
        $this->_SetTimeout(); //** set timeout. 
        return fwrite($this->Socket, $data, strlen($data)); //** write data. 
        } 
    } 

function WriteLine($data=null)
{
    return $this->Write($data . $this->NewLine);
}

and finnally, Read:

function Read($length=1)
{
    if(intval($length) <= 0)
    {
        $this->LastError = "Cannot read zero or less bytes"; 
        return null;
    }
    //** no connection is available to read from, no data can be read. 
    else if(!$this->isOpen())
    {
        $this->LastError = "No connection available for reading"; 
        return null;
    }
    else //** a valid connection identifier is available. 
    { 
        $this->_SetTimeout(); //** ensure timeout is set. 
        return fread($this->Socket, $length); //** attempt to read n-bytes. 
        } 
    } 
//** Returns: String 
//** Attempt to read one full line from the underlying stream. If no 
//** connection is available NULL is returned. Any newline characters 
//** are included in the string returned. 

function ReadLine()
{
    //** no connection is available to read from, no data can be read. 
    if(!$this->isOpen())
    {
        $this->LastError = "No connection available for reading"; 
        return null;
    }
    //** continue to read in data until a line ends with the newline character(s). 
    //** This is safe as the 'fgets()' function will not read past a newline 
    //** character. Ensure that the read buffer is at least the minumum size. If 
    //** one iteration is complete and no data was read in the socket blocking 
    //** expired. Stop reading at that point. 
    $streamdata = ""; //** no data to start with. 
    $sockethasexpired = false; //** initially no timeout experienced. 
    while(!$this->_EndsWithNewLine($streamdata) && !$sockethasexpired)
    {
        $this->_SetTimeout(); //** ensure socket timeout is set. 
        $streamdata .= fgets($this->Socket, max(TcpClientMinBufferSize, 
        $this->ReceiveBufferSize)); 
        //** if ever at the point where reading has occurred and no data is available 
        //** a socket timeout has occurred. Exit the loop and set the error. 
        if(strlen($streamdata) == 0)
        {
            $sockethasexpired = true; 
            $this->LastError = "The read took longer than $this->Timeout ms";
        }
        } 
    return $streamdata; //** return the data received, including newline. 
    } 

Now i don't have a CLUE whats gone wrong, so if anyone knows whats up with this it would be great.

PHP CONNECTION METHOD:

function Connect()
{
    $this->ClearError(); //** clear any existing error messages. 
    //** no host name/IP has been set for this client, no connection can be made. 
    //** Store the appropriate error. 
    if(strlen(trim($this->Host)) == 0)
    {
        $this->LastError = "No remote host was provided"; 
        return false;
    }
    //** attempt to connect to the host on the port given. Record any error number 
    //** and error message generated. 
    $errorNum = 0; 
    $this->Socket = fsockopen($this->Host, $this->Port, &$errorNum, &$this->LastError, ($this->Timeout / 1000)); 
    //** if there is no error given and the socket is valid connection is okay. 
    return ($this->isOpen() && strlen(trim($this->LastError)) == 0);
}