c# socket 问题?

来源:百度知道 编辑:UC知道 时间:2024/05/23 23:24:49
当创建后,为什么客户端只能给服务器发送一次信息.然后就自动停止了?
客户端主要的代码
string sendStr = "";

if (sendStr == "q")
break;

sendStr = Console.ReadLine();

byte[] bs = Encoding.ASCII.GetBytes(sendStr);
Console.WriteLine("发送信息.....");
con.Send(bs, bs.Length, 0);
sendStr = "";
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = con.Receive(recvBytes, recvBytes.Length, 0);
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
Console.WriteLine("从服务器得到的信息 :{0}", recvStr);
recvStr = "";
}
服务器代码
int port = 2000;
string host = "127.0.0.1";
I

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;

namespace ChatClient
{
public partial class ChatClientForm : Form
{
private TcpClient tcpClient;
private NetworkStream Strm;
private string UserAlias;
private bool privatemode = false;
public ChatClientForm()
{
InitializeComponent();
}

private void ChatClientForm_Load(object sender, EventArgs e)
{
try
{
LoginForm dlgLogin = new LoginForm();
DialogResult result = dlgLogin.ShowDialog();
if (result == DialogResult.OK)
{