如何通过套接字PHP将文件传输到C#

时间:2016-01-17 00:46:51

标签: c# php sockets

嗨,经过大量的研究,我没有找到解决方案

我有一个C#套接字服务器在我的服务器上运行什么,我需要通过我的网站上的Socket将文件传输给它。

我现在拥有什么?

Php part:

    $file = array('file' => Input::file('file'));
    $fp = stream_socket_client("tcp://localhost:9192", $error_number, $error_string);
    if ( !$fp ) {
        echo "$error_number ($error_string)\n";
    } else {
        fwrite($fp, $file['file']);    
    }

    fclose($fp);

C#套接字服务器:

        String content = String.Empty;

        // Retrieve the state object and the handler socket
        // from the asynchronous state object.
        StateObject state = (StateObject)ar.AsyncState;
        Socket handler = state.workSocket;

        // Read data from the client socket. 
        int bytesRead = handler.EndReceive(ar);

        if (bytesRead > 0)
        {
            // There  might be more data, so store the data received so far.
            state.sb.Append(Encoding.ASCII.GetString(
                state.buffer, 0, bytesRead));

            // Check for end-of-file tag. If it is not there, read 
            // more data.
            content = state.sb.ToString();
            if (content.IndexOf("<EOF>") > -1)
            {
                // All the data has been read from the 
                // client. Display it on the console.
                Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
                    content.Length, content);
                // Echo the data back to the client.
                Send(handler, content);
            }
            else
            {
                // Not all data received. Get more.
                handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                new AsyncCallback(ReadCallback), state);
            }
        }

当我尝试建立连接时,C#立即拒绝它我做错了什么?

我在PHP部分使用Laravel 5。

1 个答案:

答案 0 :(得分:0)

我在这个问题中找到了解决方案:

How to receive a file over TCP which was sent using socket.FileSend Method

抱歉浪费你的时间:))