【发布时间】:2012-05-14 22:28:30
【问题描述】:
我有一个基于 tcp 的客户端-服务器应用程序。我能够发送和接收字符串,但不知道如何发送字节数组。
我正在使用以下函数将字符串从客户端发送到服务器:
static void Send(string msg)
{
try
{
StreamWriter writer = new StreamWriter(client.GetStream());
writer.WriteLine(msg);
writer.Flush();
}
catch
{
}
}
通信示例
客户端发送一个字符串:
Send("CONNECTED| 84.56.32.14")
服务器收到一个字符串:
void clientConnection_ReceivedEvent(Connection client, String Message)
{
string[] cut = Message.Split('|');
switch (cut[0])
{
case "CONNECTED":
Invoke(new _AddClient(AddClient), client, null);
break;
case "STATUS":
Invoke(new _Status(Status), client, cut[1]);
break;
}
}
我需要一些帮助来修改上面的函数,以便发送和接收字节数组以及字符串。我想这样打电话:
Send("CONNECTED | 15.21.21.32", myByteArray);
【问题讨论】:
-
一个空的 catch 块总是是一个相当糟糕的主意。
标签: c# .net-2.0 client-server tcpclient tcplistener