在线等!关于Socket客户端一次连接,多次发送问题?
问题是:
现在用Socket做通讯,服务器那边一直在监听,客户端每发送一次数据前要先Connect一下服务器,然后Send数据,Send完了之后,都要Shutdown和Close一下,这样服务器会接受到一条来自客户端的消息,但是如果我Send之后,不做Shutdown和Close操作,继续用先前Connect之后的Socket实例直接Send的话,服务会收不到我的消息,所以现在我没发送一次都要Connect和Close一下,请问这个是什么原因啊?如何解决Connect一次,可以多次发送的问题呢?谢谢。
我的相关代码如下:
客户端:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace KBSSimulateServer.Common
{
public class MiniSocketClient
{
private IPAddress m_ipaServerIp;
private int m_nPort = 0;
private IPEndPoint ipepServer;
public MiniSocketClient(string strIpAddress, int nPort)
{
m_ipaServerIp = IPAddress.Parse(strIpAddress);
m_nPort = nPort;
}
public void TestConnect()
{
IPEndPoint ipepServer = new IPEndPoint(m_ipaServerIp, m_nPort);
Socket sktClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sktClient.Connect(ipepServer);
sktClient.Shutdown(SocketShutdown.Both);
sktClient.Close();
}
public void SendAndReceive(string strSend, BaseForm frmBase, Port enmPort)
{
int nBytes = 0;
try
{
ipepServer = new I