110,534
社区成员
发帖
与我相关
我的任务
分享
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace RSSReader
{
public class RssClient
{
public static string getData(string type)
{
byte[] data = new byte[1024];
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect("localhost", 8081);
s.Receive(data);
s.SendTimeout = 5000;
int n= s.Send(Encoding.ASCII.GetBytes(type+"<EOF>"));
//s.SendTo(Encoding.ASCII.GetBytes(type), new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8081));
s.Receive(data);//debug执行到这里就死掉了
s.Shutdown(SocketShutdown.Both);
s.Close();
return Encoding.UTF8.GetString(data);
}
}
}
public static string getData(string type)
{
TcpClient client = new TcpClient("localhost", 8081);
client.SendTimeout = 5000;
NetworkStream stream = client.GetStream();
try
{
String responseData = String.Empty;
string welcom_msg = "Welcome to rss server\r\n==============================";
Byte[] data = new Byte[welcom_msg.Length+2];//for "type" argument message
Int32 bytes = stream.Read(data, 0, data.Length);
data = System.Text.Encoding.ASCII.GetBytes(type);//for "type" argument message
stream.Write(data, 0, data.Length);
data = new Byte[1024];
bytes = stream.Read(data, 0, 10);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
return responseData;
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
stream.Close();
client.Close();
}
}
同样:
1. 能够收到欢迎消息
2. 似乎发送不出去,服务器没有收到消息
3. 客户端一直塞在了receive那里,没有异常。直接无视了timeout