我可以在differante端口从服务器中恢复数据TCP [英] haw can I recive data TCP from Server in differante ports
问题描述
你好
i我试图连接远程服务器如何向不同端口的客户端发送数据
port 1:11006
port 2:11003
port 3:11005
port 4:11000
这个TCP数据托盘保存在二进制文件中
但我有网络流问题
我可以添加多个网络流
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Net;
使用System。 Net.Sockets;
使用System.IO;
使用System.Threading;
命名空间connexion_Station_ELTA
{
公共部分类Form1:表格
{
公共字符串IpMachine =;
public string readData = null;
public NetworkStream serverStream = default(NetworkStream);
public NetworkStream serverStream1;
/ public static FileStream TM_File = new FileStream(@d:\DATA \TM_File.bin,FileMode.Append,FileAccess.Write);
public static BinaryWriter Write_TM = new BinaryWriter(TM_File);
public Form1()
{
InitializeComponent();
string hostName = Dns.GetHostName();
IPHostEntry IP = Dns.GetHostEntry(hostName);
IPAddress [] addr = IP.AddressList;
IpMachine = addr [2] .ToStri ng();
textBox1.Text = IpMachine;
}
// procedur de connexion a chaque port a complete ..........................
public void connexion(TcpClient客户端,int PORT_sorce,int PORT_dest)
{
IPEndPoint IPLOCAL =新IPEndPoint(IPAddress.Parse(IpMachine),PORT_sorce);
Client = new TcpClient(IPLOCAL);
Client.Connect(IPAddress.Parse(1.0.3.111),PORT_dest);
serverStream = Client.GetStream();
}
private void Form1_Load(object sender,EventArgs e)
{
}
private void button1_Click(object sender,EventArgs e)
{
// Cinnexion Firewell
connexion(ClientFW,37631,22);
// Fichier qui contien login et password
string file = @c:\ openn_RM_11006.bin;
FileStream fs = new FileStream(file,FileMode.Open,FileAccess.Read);
BinaryReader Br = new BinaryReader(fs);
byte [] outStream = Br.ReadBytes(44);
serverStream.Write(outStream,0,outStream.Length);
serverStream.Flush();
线程ctThread =新线程(getMessage);
ctThread.Start();
connexion(ClientRM,37634,23);
}
private void button2_Click(object sender,EventArgs e)
{
}
public void getMessage()
{
while(true)
{
byte [] inStream = new byte [172];
serverStream.Read( inStream,0,inStream.Length);
FileStream RM_File = new FileStream(@d:\DATA \ RMM_File。 bin,FileMode.Append,FileAccess.Write);
BinaryWriter Write_RM = new BinaryWriter(RM_File);
string returndata = System.Text.Encoding.ASCII.GetString( inStream);
readData = returndata;
Write_RM.Write(inStream);
msg();
}
}
public void msg()
{
if(this.InvokeRequired)
this.Invoke(new MethodInvoker(msg));
其他
textBox1.Text = textBox1.Text + Environment.NewLine +RM>> + readData;
}
/// deuxiem port la meme选择了一个faire
private void button4_Click(object sender,EventArgs e)
{
// Cinnexion Firewell
connexion(ClientFW,37635,22);
// Fichier qui contien login et password
string file = @c:\ openn_TM_11006.bin;
FileStream fs = new FileStream(file,FileMode.Open,FileAccess.Read);
BinaryReader Br = new BinaryReader(fs);
byte [] outStream = Br.ReadBytes(44);
serverStream.Write(outStream,0,outStream.Length);
serverStream.Flush();
线程ctThread1 =新线程(getMessage1 );
ctThread1.Start();
connexion(ClientTM,37634,24);
}
private void button2_Click(object sender,EventArgs e)
{
}
public void getMessage1()
{
while(true)
{
byte [] inStream = new byte [172];
serverStream.Read(inStream,0,inStream.Length);
FileStream RM_File = new FileStream(@d:\DATA \TM_File.bin,FileMode.Append,FileAccess.Write);
BinaryWriter Write_TM = new BinaryWriter(TM_File);
string returndata = System.Text.Encoding.ASCII.GetString(inStream);
readData1 = returndata;
Write_TM.Write(inStream);
msg1();
}
}
public void msg1()
{
if(this.InvokeRequired)
this.Invoke(new MethodInvoker(msg)) ;
else
textBox1.Text = textBox1.Text + Environment.NewLine +RM>> + readData1;
}
public TcpClient ClientFW {get;组; } $ / $
public TcpClient ClientRM {get;组; }
private void button3_Click(object sender,EventArgs e)
{
RM_File.Close();
ClientFW.Close();
ClientRM.Close();
}
}
}
----------------------------------------- -----
端口已连接好但我不知道如何将蚀刻端口中的不同数据分开
我没有查看你的代码转储,因为你没有对它进行评论(顺便说一下,它没有正确格式化)。
这个想法是:你必须有TcpListener
的单独实例,并且每个端口都有单独的TcpClient
实例。此外,您应该更好地为每个端口实例使用两个单独的线程:一个侦听新连接,另一个根据您的应用程序层协议执行数据交换:从/向网络流读取/写入。在客户端,每个端口需要一个单独的线程。
有关其他细节,请查看我过去的答案:
套接字编程中的业余问题 [ ^ ],
如何知道AA Tcp连接是否结束 [ ^ ],
来自同一端口号码的多个客户端 [ ^ ]。
-SA
Hello
i am trying to connect in the distant server how send data to clients in different ports
port 1 :11006
port 2 :11003
port 3 :11005
port 4 :11000
this TCP data t tray to save its in binary file
but I have problem with Network Stream
I can add more than one network stream
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace connexion_Station_ELTA
{
public partial class Form1 : Form
{
public string IpMachine = "";
public string readData = null;
public NetworkStream serverStream = default(NetworkStream);
public NetworkStream serverStream1;
/ public static FileStream TM_File = new FileStream(@"d:\DATA\TM_File.bin", FileMode.Append, FileAccess.Write);
public static BinaryWriter Write_TM = new BinaryWriter(TM_File);
public Form1()
{
InitializeComponent();
string hostName = Dns.GetHostName();
IPHostEntry IP = Dns.GetHostEntry(hostName);
IPAddress[] addr = IP.AddressList;
IpMachine = addr[2].ToString();
textBox1.Text = IpMachine;
}
// procedur de connexion a chaque port a complete ..........................
public void connexion(TcpClient Client, int PORT_sorce, int PORT_dest)
{
IPEndPoint IPLOCAL = new IPEndPoint(IPAddress.Parse(IpMachine), PORT_sorce);
Client = new TcpClient(IPLOCAL);
Client.Connect(IPAddress.Parse("1.0.3.111"), PORT_dest);
serverStream = Client.GetStream();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//Cinnexion Firewell
connexion(ClientFW, 37631, 22);
//Fichier qui contien login et password
string file = @"c:\open_RM_11006.bin";
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
BinaryReader Br = new BinaryReader(fs);
byte[] outStream = Br.ReadBytes(44);
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
Thread ctThread = new Thread(getMessage);
ctThread.Start();
connexion(ClientRM, 37634, 23);
}
private void button2_Click(object sender, EventArgs e)
{
}
public void getMessage()
{
while (true)
{
byte[] inStream = new byte[172];
serverStream.Read(inStream, 0, inStream.Length);
FileStream RM_File = new FileStream(@"d:\DATA\RM_File.bin", FileMode.Append, FileAccess.Write);
BinaryWriter Write_RM = new BinaryWriter(RM_File);
string returndata = System.Text.Encoding.ASCII.GetString(inStream);
readData = returndata;
Write_RM.Write(inStream);
msg();
}
}
public void msg()
{
if (this.InvokeRequired)
this.Invoke(new MethodInvoker(msg));
else
textBox1.Text = textBox1.Text + Environment.NewLine + "RM>>" + readData;
}
/// deuxiem port la meme chose a faire
private void button4_Click(object sender, EventArgs e)
{
//Cinnexion Firewell
connexion(ClientFW, 37635, 22);
//Fichier qui contien login et password
string file = @"c:\open_TM_11006.bin";
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
BinaryReader Br = new BinaryReader(fs);
byte[] outStream = Br.ReadBytes(44);
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
Thread ctThread1 = new Thread(getMessage1);
ctThread1.Start();
connexion(ClientTM, 37634, 24);
}
private void button2_Click(object sender, EventArgs e)
{
}
public void getMessage1()
{
while (true)
{
byte[] inStream = new byte[172];
serverStream.Read(inStream, 0, inStream.Length);
FileStream RM_File = new FileStream(@"d:\DATA\TM_File.bin", FileMode.Append, FileAccess.Write);
BinaryWriter Write_TM = new BinaryWriter(TM_File);
string returndata = System.Text.Encoding.ASCII.GetString(inStream);
readData1 = returndata;
Write_TM.Write(inStream);
msg1();
}
}
public void msg1()
{
if (this.InvokeRequired)
this.Invoke(new MethodInvoker(msg));
else
textBox1.Text = textBox1.Text + Environment.NewLine + "RM>>" + readData1;
}
public TcpClient ClientFW { get; set; }
public TcpClient ClientRM { get; set; }
private void button3_Click(object sender, EventArgs e)
{
RM_File.Close();
ClientFW.Close();
ClientRM.Close();
}
}
}
----------------------------------------------
the port is connected OK but I don't know how a get the different data in etch port separated
I did not look through your code dump, because you did not comment on it (and it is not formatted properly, by the way).
The idea is: you have to have separate instances ofTcpListener
and separate instances ofTcpClient
per port. Also, you should better use two separate threads per the instance of port: one listening for new connection, another one performing the data exchange according to your application-layer protocol: reading/writing from/to network stream. On client side, you would need one separate thread per port.
For some other detail, please see my past answers:
an amateur question in socket programming[^],
How Do I Get To Know If A A Tcp Connection Is Over[^],
Multple clients from same port Number[^].
—SA
这篇关于我可以在differante端口从服务器中恢复数据TCP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!