自己建立的sys类 public static NetworkStream ns = null;
public static StreamReader sr = null;
public static TcpClient clientsocket = null;
/// <summary>
/// 向服务器发起SOCKET请求
/// </summary>
/// <param name="import">发起的请求字符串</param>
/// <returns>返回服务器结果</returns>
public static string sendSoket(string import)
{
string sendSoket_ = "";
try
{
Byte[] outbytes = Encoding.GetEncoding("GB2312").GetBytes(import.ToCharArray());
sys.ns.Write(outbytes, 0, outbytes.Length);
string serverresponse = "";
serverresponse = sys.sr.ReadLine();
sendSoket_ = serverresponse.Trim();
}
catch (Exception ex)
{
sendSoket_ = "Exc|" + ex.ToString();
}
return sendSoket_;
}为什么软件会死掉,这个程序有什么问题吗?
public static StreamReader sr = null;
public static TcpClient clientsocket = null;
/// <summary>
/// 向服务器发起SOCKET请求
/// </summary>
/// <param name="import">发起的请求字符串</param>
/// <returns>返回服务器结果</returns>
public static string sendSoket(string import)
{
string sendSoket_ = "";
try
{
Byte[] outbytes = Encoding.GetEncoding("GB2312").GetBytes(import.ToCharArray());
sys.ns.Write(outbytes, 0, outbytes.Length);
string serverresponse = "";
serverresponse = sys.sr.ReadLine();
sendSoket_ = serverresponse.Trim();
}
catch (Exception ex)
{
sendSoket_ = "Exc|" + ex.ToString();
}
return sendSoket_;
}为什么软件会死掉,这个程序有什么问题吗?
如果要在其它线程中读写NetworkStream中的内容,考虑使用 BeginWrite 和 EndWrite,如果要异步传输只能抛弃TcpClient了……
ivorstar(懒)的确是同步方式通信的,这段程序是在主线程中执行的.为什么说异步传输要抛弃TcpClient ???像 我问题如何解决!!???
2、对方发送过来的数据中没有换行符!
没有换行符???
-----------------------------------------------------------------
是的,ReadLine方法要读取到一个换行符才会返回。