简体   繁体   English

从服务器发送数据

[英]Sending data from server

I tried to send data to client using this function 我尝试使用此功能将数据发送到客户端

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

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

I call this function on this function 我在这个功能上称这个功能

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

its work well.. and send the data that server received from client. 其工作正常..并发送服务器从客户端接收的数据。 but when I changed it to this, 但是当我将其更改为

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);
    }
}

Client will only receive the data he send to me, but not the string Testing send string . 客户只会收到他发送给我的数据,而不会收到字符串Testing send string Am I doing something wrongly or are there any restriction? 我做错了什么或有什么限制吗?

We can use TCP messages to send data to client using asynchronous communication. 我们可以使用TCP消息通过异步通信将数据发送到客户端。

Initialize Server Socket and try to connect: 初始化服务器套接字,然后尝试连接:

// 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
    }
}

On Successful connection: 成功连接时:

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

Process the message received: 处理收到的消息:

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

I found out that the problem is with sending the data, 我发现问题在于发送数据,

it seems my client(flash) will receive the string if i add something like this: 如果我添加如下内容,我的client(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);
    }
}

if anyone can explain why, please add at the comment of this answer, thx 如果有人可以解释原因,请在此答案的评论中加上thx

Because we don't know what the server does nor how it gets the data, can you please try this and see what the server gets: 因为我们不知道服务器做什么,也不知道它如何获取数据,所以请您尝试一下看看服务器得到了什么:

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);
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM