一个tcpclient连接到多个服务器 [英] One tcpclient connected to multiple servers
本文介绍了一个tcpclient连接到多个服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将Tcp客户端应用程序连接到多个套接字监听器。
我的套接字监听器是带有esp8266的arduino设备,它已经完成
但我不知道如何使用一个tcpclient来连接我的所有套接字监听器并且可以立即发送或接收数据只需发送和接收一些字符串,如9L,9H
希望你们可以帮助我,我开始C#
谢谢所有
我尝试过:
使用System;
使用System.Net;
使用System.Net.Sockets;
使用System.IO;
使用System.Threading.Tasks;
使用System.Threading;
class program
{
int port = 80;
static void Main(string [] args)
{
Program program = new Program();
program.ClientMain();
}
public void ClientMain()
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse(192.168.4.19),port);
套接字客户端=新套接字(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
client.Connect(ip);
new TcpListener(client);
}
}
公共类TcpListener
{
线程inThread,outThread;
NetworkStream流;
StreamReader阅读器;
StreamWriter编写器;
public TcpListener(套接字客户端)
{
stream = new NetworkStream(client);
reader = new StreamReader(stream);
writer = new StreamWriter(stream);
inThread = new Thread(new ThreadStart(inLoop));
inThread.Start();
outThread = new Thread(new ThreadStart(outLoop));
outThread.Start();
}
public void inLoop()
{
while(true)
{
String s = reader.ReadLine();
Console.WriteLine(server:+ s);
}
}
public void outLoop()
{
while(true)
{
writer.WriteLine(Console .ReadLine());
writer.Flush();
}
}
}
解决方案
已经有一个关于网站: [ TCP-client-server-in-C简介]
如果您阅读示例并将其作为示例,您应该可以在自己的代码中执行此操作。
goodluck:)
我想你可以用SignalR[ ^ 一>
I want to connect the Tcp client application to multiple socket listener.
my socket listener is arduino device with esp8266 and it's finished
but i don't know how to use one tcpclient to connect all my socket listener and can send or receive data immediately just send and receive some string like "9L" ,"9H"
hope u guys can help me i'm begin in C#
thanks all
What I have tried:
using System; using System.Net; using System.Net.Sockets; using System.IO; using System.Threading.Tasks; using System.Threading; class Program { int port = 80; static void Main(string[] args) { Program program = new Program(); program.ClientMain(); } public void ClientMain() { IPEndPoint ip = new IPEndPoint(IPAddress.Parse("192.168.4.19"), port); Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.Connect(ip); new TcpListener(client); } } public class TcpListener { Thread inThread, outThread; NetworkStream stream; StreamReader reader; StreamWriter writer; public TcpListener(Socket client) { stream = new NetworkStream(client); reader = new StreamReader(stream); writer = new StreamWriter(stream); inThread = new Thread(new ThreadStart(inLoop)); inThread.Start(); outThread = new Thread(new ThreadStart(outLoop)); outThread.Start(); } public void inLoop() { while (true) { String s = reader.ReadLine(); Console.WriteLine("server:" + s); } } public void outLoop() { while (true) { writer.WriteLine(Console.ReadLine()); writer.Flush(); } } }
解决方案
There is already a tutorial on the site here: [Introduction-to-TCP-client-server-in-C]
If you read the example and make it as the example you should be able to do this in your own code.
goodluck :)
i think you can usedSignalR[^]
这篇关于一个tcpclient连接到多个服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文