简体   繁体   English

从C#客户端向C服务器发送消息

[英]Sending message from C# client to C server

I'm fairly new to socket programming, but, using an online tutorial, I've successfully sent a short string from one machine to another using C. 我是套接字编程的新手,但是,通过使用在线教程,我已经成功地使用C将短字符串从一台计算机发送到另一台计算机。

The problem I'm having is with trying to send a string from a client written in C#. 我遇到的问题是尝试从用C#编写的客户端发送字符串。 The server (written in C) prints out a blank/empty string. 服务器(用C编写)打印出一个空白/空字符串。

Here is the C code that runs on the "server" machine (in this case a router running OpenWRT): 这是在“服务器”计算机上运行的C代码(在本例中为运行OpenWRT的路由器):

int main(int argc, char *argv[])
{
    int listenfd = 0, connfd = 0;
    struct sockaddr_in serv_addr;

    char recvBuff[1025];

    int bytesRead;

    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    memset(&serv_addr, '0', sizeof(serv_addr));

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(1234);

    bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));

    printf("Listening for string on port 1234...\n");

    listen(listenfd, 10);

    while(1)
    {
        connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);

        bytesRead = recv(connfd, recvBuff, 1024, 0);    // Receive string

        if (bytesRead < 0)
        {
            printf("Error reading from stream\n");
        }

        recvBuff[bytesRead] = 0;    // null-terminate the string

        printf("%d:%s\n", bytesRead, recvBuff);

        close(connfd);
        sleep(1);
     }
}

When sending this little server a string from another C program it works exactly as expected (prints the string out then waits for another one). 当从另一个C程序向这个小服务器发送字符串时,它的工作原理与预期的完全一样(将字符串打印出来然后等待另一个)。 [ Note: I don't think the C client's code is relevant, but I can post it if need be ] [ 注意:我认为C客户端的代码不相关,但是如果需要可以将其发布 ]

However, when trying to send it a string from a C# program (copied below), the server prints out 0: (ie 0 bytes read, followed by an empty string) and I can't for the life of me figure out what the issue is. 但是,当尝试从C#程序(下面复制)向它发送一个字符串时,服务器打印出0:即读取0字节,后跟一个空字符串),我一生都无法弄清楚问题是。 Both apps are pretty straightforward, so I'm assuming I should be using something other than WriteLine (I've also tried Write but to no avail). 这两个应用程序都非常简单明了,所以我假设我应该使用WriteLine其他东西(我也尝试过Write但无济于事)。

C# Client: C#客户端:

namespace SocketTest
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient client = new TcpClient("10.45.13.220", 1234);

            Stream stream = client.GetStream();

            StreamWriter writer = new StreamWriter(stream);

            writer.WriteLine("Testing...");

            client.Close();
        }
    }
}

To null terminate a string use 要使字符串终止为空,请使用

recvBuff[bytesRead] = '\0'; 

And call close on writer (which causes the writer to flush any pending bytes) 并在writer上调用close (这会导致writer刷新所有未决字节)

writer.WriteLine("Testing...");
writer.Close();
client.Close();

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

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