private Socket s; //定义Socket对象
public NetworkStream ns; //网络流
public StreamReader sr; //流读取
public StreamWriter sw;
private void button1_Click(object sender, EventArgs e)
{
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress serverIP = IPAddress.Parse("119.161.164.164"); //服务器IP
try
{
s.Connect(serverIP, 4807);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
try
{
ns = new NetworkStream(s); //实例化网络流
sr = new StreamReader(ns); //实例化流读取对象
sw = new StreamWriter(ns); //实例化写入流对象
//发送字符串
string str = "ENQUIRY"+"\t"+"B10101"+"\t"+"01"+"\t" + "GPCardBalance" + "\t\r\n" ;
str += "0000000000000005" + "\t\r\n";
sw.WriteLine(str); //将str的数据写入流
sw.Flush(); //清理缓冲区
label1.Text = sr.ReadLine(); //将从流中读取的数据写入lbInfo
}
catch (Exception ex)
{
MessageBox.Show(ex.Message); //捕获异常
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
s.Shutdown(SocketShutdown.Both);
s.Close();
}
这个是socket客户端的代码,为什么我收不到服务器返回的信息,而且程序也没捕获异常,就是死在那,好像死循环一样。