socket问题
客户端 static Socket c;
private void Form1_Load(object sender, EventArgs e)
{
try
{
int port = 2000;
string host = "127.0.0.1";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
c.Connect(ipe);
MessageBox.Show("与服务器连接成功!", "提示");
this.button1.Enabled = true;
}
catch
{
MessageBox.Show("与服务器连接失败!", "提示");
this.button1.Enabled = false ;
}
}
//发送按钮
private void button1_Click(object sender, EventArgs e)
{
string sendStr = textBox1.Text.Trim().ToString();
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
c.Send(bs, bs.Length, 0);
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = c.Receive(recvBytes, recvBytes.Length, 0);
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
MessageBox.Show(recvStr, "提示");
}
这样写为什么只能与服务器发生1次。在点发送按钮时就提示说“您的主机中的软件放弃了一个已经建立的链接”这该怎么改啊。
我想的是让这个客户端一直和服务器连接着