下面事例,提问如题.下面是服务端.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
public class TcpTimeClient
{
private const int portNum = 80;//设为80是为了能穿过防火墙.  private const string hostName ="28.72.197.52";//
  // 这是客户端的IP.也就是要接收信息的主机.服务器要知道它,才能让客户端收到. public static int Main(String[] args)
{
try
{
      TcpClient client = new TcpClient(hostName,portNum);
NetworkStream ns = client.GetStream();
 /////////////////////////////////////////////////////////////////////
 ///如果去掉这部分将被写入文件的文本.而用人工输入汉字到1.txt  
 ///则在客户端不会出现乱码.估计就是此处惹的祸
         string path = @"c:\1.txt"; // Delete the file if it exists.
if (!File.Exists(path)) 
{
// Create the file.
using (FileStream fs1 = File.Create(path)) {}
} // Open the stream for writing.
using (FileStream fs1 = File.OpenWrite(path)) 
{

                                   Byte[] info =  new UTF8Encoding(true).GetBytes(
                   "This is to test the OpenWrite method.中华人民共和国"); // Add some information to the file.
fs1.Write(info, 0, info.Length);
}
/////////////////如果去掉上面这些行,则OK ,如果?//////////
///请指教 ,想用它来发信息,所以不想去掉上面几行,信息不都是存在文件中的.///////////////////////////////从文件读取到网络流,以备传送//////////////////////
     FileStream fs  =   File.Open   ("C:\\1.txt",FileMode.Open,System.IO.FileAccess.ReadWrite);
int data = fs.ReadByte();
Console.WriteLine("jfjdkfj");
   while(data!=-1)

       ns.WriteByte((byte)data);
   data = fs.ReadByte();

Console.WriteLine("网络流发送成功");
 
client.Close();
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
return 0;
}
}
////////////////////////////////////////
///服务端往客户端发信息.但事先.客户端要找开才收得到.且客户端不能在远方的局域网内.
////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
///////////为了能让您更方便地调试,我把客户端也抄下来了///////
////////////但我估计下面没错///////////////////////////////
 
using System;
using System.Net.Sockets;
using System.Text;
using System.IO;
public class TcpTimeServer
{
private const int portNum = 80;
public static int Main(String[]args)
{
bool done = false;
TcpListener listener = new TcpListener(portNum);
listener.Start();
while(!done)
{
Console.Write("等待连接...");
TcpClient client = listener.AcceptTcpClient();
Console.WriteLine("接到连接");
NetworkStream ns = client.GetStream();
  try
{
  StreamReader sr = new StreamReader(ns,System.Text.Encoding.Default);
string result  = sr.ReadToEnd();
Console.WriteLine(result);
  ns.Close();
client.Close();
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}
listener.Stop();
return 0;
}
}
/////////////////////////////////////////////////////////////
///////////////好郁闷啊,只想发送消息都这么困难吗/////////////
////////////////////////////////////////////////////////////
////////非常感谢你有这么强的助人为乐精神,读完了此程序///////
////////非常感谢你有这么强的助人为乐精神,读完了此程序///////
///////////////盼您的回复///////////////////////////////////
///////////////盼您的回复///////////////////////////////////

解决方案 »

  1.   

    编码问题吧??
    Encoding.UTF8
      

  2.   

    用序列化做就没问题(BinaryStream)
      

  3.   

    SERVER:     TcpListener listener=new TcpListener(5000);
                connection=listener.AcceptSocket();
                socketStream=new NetworkStream(connection);
                BinaryWriter writer=new BinaryWriter(socketStream);
                BinaryReader reader=new BinaryReader(socketStream);
                writer.Write("发送的内容");
                接收的字符=reader.ReadString();CLIENT:     TcpListener client=new TcpClient();
                client.Connect("192.168.0.13",5000);  //主机地址和端口
                connection=listener.AcceptSocket();
                socketStream=client.GetStream();
                BinaryWriter writer=new BinaryWriter(socketStream);
                BinaryReader reader=new BinaryReader(socketStream);
                writer.Write("发送的内容");
                接收的字符=reader.ReadString();
      

  4.   

    server:
    byte[] bytes =Encoding.Unicode.GetBytes(datagram);
    client:
    string returnData=Encoding.Unicode.GetString(receiveBytes,0,receiveBytes.Length);