从 ip 和端口接收数据的线程

发布于 2024-08-12 18:28:42 字数 1762 浏览 3 评论 0原文

我想编写一个程序来使用 tcpClient 从指定的 ip 和端口号接收一些数据。我第一次使用 while(true) 来做到这一点。我的朋友告诉我使用线程而不是 while 循环。于是我就按照他说的做了。

public static void receiveThread()
{
    TcpClient tcpClient = new TcpClient();
    try
    {
        tcpClient.Connect(ipAddress, incPort);
        Console.WriteLine("Connection accepted ...");
    }
    catch (Exception e)
    {
        Console.WriteLine(e + "\nPress enter to exit...");
        Console.ReadKey();
        return;
    }
    NetworkStream stream = tcpClient.GetStream();
    StreamReader incStreamReader = new StreamReader(stream);

    try
    {
        data = incStreamReader.ReadLine();
        Console.WriteLine("Received data: {0}", data);
    }
    catch (Exception e)
    {
        Console.WriteLine(e + "\nPress enter to exit...");
    }
}

工作正常,但没有我希望的那么好。当我运行我的程序并向其发送“Hello world”字符串时,它会收到它,然后完成作业并退出。我想让线程继续接收更多传入数据,但我不知道该怎么做。也许有人告诉我该怎么做?

要发送数据我使用这个

using System;
using System.Net;
using System.Net.Sockets;
using System.IO;

public class Program
{
public static string ipAddress = "127.0.0.1";
public static int listenerPort = 6600;
public static string message;

static void Main(string[] args)
{
    TcpListener tcpListener = new TcpListener(IPAddress.Parse(ipAddress),listenerPort);
    tcpListener.Start();

    Socket socket = tcpListener.AcceptSocket();
    Console.WriteLine("Connection accepted...");
    while (true)
    {
        if (socket.Connected)
        {
            NetworkStream networkStream = new NetworkStream(socket);
            StreamWriter streamWriter = new StreamWriter(networkStream);

            message = Console.ReadLine();
            streamWriter.WriteLine(message);
            streamWriter.Flush();
        }
    }
}

I would like to write a program to receive some data using tcpClient from a specified ip and port number. First time I did it using while(true). Friend of mine told me to use thread instead of while loop. So I did as he said.

public static void receiveThread()
{
    TcpClient tcpClient = new TcpClient();
    try
    {
        tcpClient.Connect(ipAddress, incPort);
        Console.WriteLine("Connection accepted ...");
    }
    catch (Exception e)
    {
        Console.WriteLine(e + "\nPress enter to exit...");
        Console.ReadKey();
        return;
    }
    NetworkStream stream = tcpClient.GetStream();
    StreamReader incStreamReader = new StreamReader(stream);

    try
    {
        data = incStreamReader.ReadLine();
        Console.WriteLine("Received data: {0}", data);
    }
    catch (Exception e)
    {
        Console.WriteLine(e + "\nPress enter to exit...");
    }
}

Works fine but not as good as I would like it to work. When Im running my program and sending to it for exaple "Hello world" string, it receives it and then finishing the job and exiting. I want to keep the thread up for more incoming data but I do not know how to do it. Maybe someone has a clue for me how to do it ?

To sending data Im using this

using System;
using System.Net;
using System.Net.Sockets;
using System.IO;

public class Program
{
public static string ipAddress = "127.0.0.1";
public static int listenerPort = 6600;
public static string message;

static void Main(string[] args)
{
    TcpListener tcpListener = new TcpListener(IPAddress.Parse(ipAddress),listenerPort);
    tcpListener.Start();

    Socket socket = tcpListener.AcceptSocket();
    Console.WriteLine("Connection accepted...");
    while (true)
    {
        if (socket.Connected)
        {
            NetworkStream networkStream = new NetworkStream(socket);
            StreamWriter streamWriter = new StreamWriter(networkStream);

            message = Console.ReadLine();
            streamWriter.WriteLine(message);
            streamWriter.Flush();
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

殊姿 2024-08-19 18:28:42

查看 TCPClient 对象的此属性

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.connected.aspx

您可以这样使用它

while(tcpClient.Connected)
{
    // do something while conn is open
}

Have a look at this property of the TCPClient Object

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.connected.aspx

you can use it as such

while(tcpClient.Connected)
{
    // do something while conn is open
}
夏九 2024-08-19 18:28:42

您的朋友让您使用线程,这样您的主应用程序就不会被锁定。现在您已经创建了一个新线程,您可以像以前一样在该线程内使用 while 循环。

Your friend had you use a thread so your main application wasn't locked up. Now that you've created a new thread you can use a while loop inside that thread like you were doing previously.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文