从服务器发送数据

时间:2013-04-29 08:15:44

标签: c# sockets network-programming

我尝试使用此功能向客户端发送数据

protected void SendData(Socket so, string sendData)
{
    byte[] data = Encoding.ASCII.GetBytes(sendData);
    so.Send(data);

    Console.WriteLine("Sending data back to Client\n");
}

我在这个函数上调用这个函数

protected void ProcessData(int x, string data, Socket so)
{
    if (x < mybuffersize)
    {
        data = data.Trim();
        SendData(so, data);
    }
}

工作正常..并发送服务器从客户端收到的数据。 但当我改变它时,

protected void ProcessData(int x, string data, Socket so)
{
    if (x < mybuffersize)
    {
        data = data.Trim();
        SendData(so, data);
        string sendData = "Testing send string";
        SendData(so, sendData);
    }
}

客户端只会收到他发送给我的数据,但不会收到字符串Testing send string。 我做错了什么或有任何限制吗?

3 个答案:

答案 0 :(得分:1)

我们可以使用TCP消息使用异步通信将数据发送到客户端。

初始化服务器套接字并尝试连接:

// Specify the size according to your need.
private byte[] bytData = new byte[1024 * 50000];
private Socket oServerSocket;

private void WindowLoaded(object sender, RoutedEventArgs e)
{
    try
    {
        // We are using TCP sockets
        this.oServerSocket = new Socket(
        addressFamily: AddressFamily.InterNetwork,
        socketType: SocketType.Stream,
        protocolType: ProtocolType.Tcp);

        // Assign the any IP of the hardware and listen on port number which the hardware is sending(here it's 5656)
        var oIPEndPoint = new IPEndPoint(address: IPAddress.Any, port: 5656);

        // Bind and listen on the given address
        this.oServerSocket.Bind(localEP: oIPEndPoint);
        this.oServerSocket.Listen(backlog: 4);

        // Accept the incoming clients
        this.oServerSocket.BeginAccept(this.OnAccept, null);
    }
    catch (Exception ex)
    {
        // Handle Exception
    }
}

成功连接:

private void OnAccept(IAsyncResult ar)
{
    try
    {
        var oClientSocket = this.oServerSocket.EndAccept(asyncResult: ar);

        // Start listening for more clients
        this.oServerSocket.BeginAccept(callback: this.OnAccept, state: null);

        // Once the client connects then start receiving the commands from her
        oClientSocket.BeginReceive(
        buffer: this.bytData,
        offset: 0,
        size: this.bytData.Length,
        socketFlags: SocketFlags.None,
        callback: this.OnReceive,
        state: oClientSocket);
    }
    catch (Exception ex)
    {
        // Handle Exception
    }
}

处理收到的消息:

private void OnReceive(IAsyncResult ar)
{
    try
    {
        var oClientSocket = (Socket)ar.AsyncState;
        oClientSocket.EndReceive(asyncResult: ar);

        /* Process the data here

        BitConverter.ToInt32(value: this.bytData, startIndex: 0);
        string SomeString= Encoding.ASCII.GetString(bytes: this.bytData, index: 8, count: 5);

        */

        // Specify the size according to your need.
        this.bytData = null;
        this.bytData = new byte[1024 * 50000];
        oClientSocket.BeginReceive(
            buffer: this.bytData,
            offset: 0,
            size: this.bytData.Length,
            socketFlags: SocketFlags.None,
            callback: this.OnReceive,
            state: oClientSocket);
    }

    catch (Exception ex)
    {
        // Handle Exception
    }
}

答案 1 :(得分:0)

我发现问题在于发送数据,

似乎我的客户端(flash)将收到字符串,如果我添加这样的东西:

protected void ProcessData(int x, string data, Socket so)
{
    if (x < mybuffersize)
    {
        data = data.Trim();
        SendData(so, data);
        string sendData = "Testing send string\0";
        SendData(so, sendData);
    }
}

如果有人能解释原因,请在此答案的评论中添加,thx

答案 2 :(得分:0)

因为我们不知道服务器做了什么以及它是如何获取数据的,所以请你试试看看服务器的内容:

protected void ProcessData(int x, string data, Socket so)   
{
    if (x < mybuffersize)
    {
        string sendData = "Testing send string\0";
        SendData(so, sendData);
        data = data.Trim();
        SendData(so, data);
    } 
}