从 ip 和端口接收数据的线程
我想编写一个程序来使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 TCPClient 对象的此属性
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.connected.aspx
您可以这样使用它
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 循环。
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.