这是用c#编写的一个简单的服务器代码。一旦连接到服务器,我想给客户端一个欢迎消息。欢迎消息将显示在客户端的屏幕上。我将如何做到这一点?在c#中向客户端屏幕发送欢迎消息
部分示例代码:
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Xml.Serialization;
namespace server
{
class Program
{
static void Main(string[] args)
{
TcpListener tcpListener = new TcpListener(IPAddress.Any, 1234);
tcpListener.Start();
while (true)
{
TcpClient tcpClient = tcpListener.AcceptTcpClient();
byte[] data = new byte[1024];
NetworkStream ns = tcpClient.GetStream();
string[] arr1 = new string[] { "one", "two", "three" };
var serializer = new XmlSerializer(typeof(string[]));
serializer.Serialize(tcpClient.GetStream(), arr1);
int recv = ns.Read(data, 0, data.Length); //getting exception in this line
string id = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(id);
}
}
}
}
什么是需要修改发送欢迎信息?
你检查你的NetworkStream变量“NS”有写法?或者是你可以通过tcpClient.GetStream到StreamWriter类并调用写入方法 – Viru
可以请你给我一个示例代码片段? @Viru – ACE