如何从一个端口发送数据并从套接字编程c#中接收另一个端口。 [英] How to Send data from one port and receive from another in socket programming c#.
本文介绍了如何从一个端口发送数据并从套接字编程c#中接收另一个端口。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要创建客户端服务器应用程序,其中服务器侦听一个端口并在另一个端口上为同一客户端发送响应。我已经创建了这样的东西,这是行不通的,我想我甚至不在附近发送和接收不同的端口。虽然我已经为同一个端口实现了这一点。帮助高度赞赏。谢谢
服务器: -
I need to create client server application in which server listens on one port and sends response on another port for same client. I have created something like this which is not working and i guess i am not even near to send and receive on different port. Though i have achieved this for same port. Help highly Appreciated. Thanks
Server:-
public void createListener()
{
TcpListener tcpListener = null;
IPAddress ipAddress = IPAddress.Any;
try
{
tcpListener = new TcpListener(ipAddress, 1818);
tcpListener.Start();
output = "Waiting for a connection...";
}
catch (Exception e)
{
output = "Error: " + e.ToString();
MessageBox.Show(output);
}
while (true)
{
Thread.Sleep(10);
TcpClient tcpClient = tcpListener.AcceptTcpClient();
byte[] bytes = new byte[256];
NetworkStream stream = tcpClient.GetStream();
stream.Read(bytes, 0, bytes.Length);
SocketHelper helper = new SocketHelper();
helper.processMsg(tcpClient, stream, bytes);
}
}
class SocketHelper
{
TcpClient mscClient;
string mstrMessage;
string mstrResponse;
byte[] bytesSent;
public void processMsg(TcpClient client, NetworkStream stream, byte[] bytesReceived)
{
mstrMessage = Encoding.ASCII.GetString(bytesReceived, 0, bytesReceived.Length);
mscClient = client;
if (mstrMessage.Length>0)
{
mstrResponse = mstrMessage;
}
else
{
mstrResponse = "You have sent blank message";
}
bytesSent = Encoding.ASCII.GetBytes(mstrResponse);
stream.Write(bytesSent, 0, bytesSent.Length);
}
}
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "Now Listening";
this.createListener();
}
客户: -
Client:--
static string output = "";
public Client()
{
InitializeComponent();
}
public void createListener()
{
TcpListener tcpListener = null;
IPAddress ipAddress = IPAddress.Any;
try
{
tcpListener = new TcpListener(ipAddress, 1819);
tcpListener.Start();
output = "Waiting for a connection...";
}
catch (Exception e)
{
output = "Error: " + e.ToString();
MessageBox.Show(output);
}
while (true)
{
Thread.Sleep(10);
TcpClient tcpClient = tcpListener.AcceptTcpClient();
byte[] bytes = new byte[256];
NetworkStream stream = tcpClient.GetStream();
stream.Read(bytes, 0, bytes.Length);
string output = "";
}
}
static string Connect(string serverIP, string message)
{
string output = "";
try
{
Int32 port = 1818;
TcpClient client = new TcpClient(serverIP, port);
Byte[] data = new Byte[256];
data = System.Text.Encoding.ASCII.GetBytes(message);
NetworkStream stream = client.GetStream();
stream.Write(data, 0, data.Length);
output = "Sent: " + message;
MessageBox.Show(output);
data = new Byte[256];
String responseData = String.Empty;
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
output = "Received: " + responseData;
MessageBox.Show(output);
stream.Close();
client.Close();
return responseData;
}
catch (ArgumentNullException e)
{
output = "ArgumentNullException: " + e;
MessageBox.Show(output);
return e.Message;
}
catch (SocketException e)
{
output = "SocketException: " + e.ToString();
MessageBox.Show(output);
return e.Message;
}
}
private void button1_Click(object sender, EventArgs e)
{
string serverIP = "192.168.0.205";
string message = textBox1.Text;
textBox2.Text=Connect(serverIP, message);
}
推荐答案
请参阅我对该问题的评论。您可以在我过去的答案中找到一些有用的想法:
来自同一端口号码的多个客户端 [ ^ ],
业余爱好者套接字编程中的问题 [ ^ ] 。
您描述的内容与发布者 - 订阅者模型类似。您可以将TCP连接视为订阅。这样,您就不需要客户倾听。即使你需要它,你也不需要一个单独的端口(这没关系)。
-SA
Please see my comments to the question. You can find some useful ideas in my past answers:
Multple clients from same port Number[^],
an amateur question in socket programming[^].
What you describe looks similar to publisher-subscriber model. You can treat TCP connection as subscription. This way, you won't need clients to listen. And even if you needed that, you would not need a separate port (it just doesn't matter).
—SA
以下文章对此有所帮助。
使用异步UDP套接字的聊天应用程序 [ ^ ]
好工作hitesh
this following article helped for this.
A Chat Application Using Asynchronous UDP sockets[^]
good job hitesh
这篇关于如何从一个端口发送数据并从套接字编程c#中接收另一个端口。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文