日期:2014-05-17  浏览次数:20789 次

socket的send方法问题
客户端创建一个SOCKET,去访问服务器端,第一次发送接收信息都没问题,在接收了服务器返回的信息后,继续使用send方法发送服务器就不能收到了。代码如下
  public Form1()
  {
  InitializeComponent();
  }
  Socket socket;
  private void Form1_Load(object sender, EventArgs e)
  {
  //Thread thread = new Thread(new ThreadStart(BeginListen));
  //thread.Start();
  string ip = "192.168.1.39";
  int port = Convert.ToInt32("8558");
  IPAddress ipaddress = IPAddress.Parse(ip);
  IPEndPoint ipendpoint = new IPEndPoint(ipaddress, port);
  socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  socket.Connect(ipendpoint);
  byte[] sendStr = Encoding.UTF8.GetBytes("开始连接服务器");
  socket.ReceiveTimeout = 1000;
  socket.Send(sendStr);
  LastReceive(socket);
  }

  private void button1_Click(object sender, EventArgs e)
  {
  SendMessage(textBox3.Text);
  LastReceive(socket);
  }

  public void LastReceive(Socket socket)
  {
  byte[] recvBytes = new byte[1024];
  int bytes=1;
  bytes = socket.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
  if (bytes <= 0) return;
  textBox4.Text += Encoding.UTF8.GetString(recvBytes, 0, recvBytes.Length);
  }
  public void SendMessage(string mymessage)
  {
  byte[] sendStr =Encoding.UTF8.GetBytes(mymessage);
  socket.Send(sendStr);
  //sendsocket.Shutdown(SocketShutdown.Both);
  //sendsocket.Close();
  }

------解决方案--------------------
你去下个看看 吧
http://topic.csdn.net/u/20100727/11/7846e5aa-6724-47b6-921d-c9cb994747b3.html
------解决方案--------------------
= = 服务器的接收采用循环