我在计算机和服务器之间的 Socket 通信上有问题。发生的情况是,仅当我单击特定按钮时,我才通过套接字建立通信。在第一次沟通中,一切都发生得很完美。如果我再次单击,则不会执行代码。我放了一个断点来查看发生了什么,并看到在给定的行上,他只是在尝试执行该行,但是执行了很长时间。(我总是传递相同的参数)。
长时间处理的线路
serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
客户代码
private void Form1_Load(object sender, EventArgs e)
{
msg("Client Started");
clientSocket.Connect("server_ip", 8855);
//clientSocket.Connect("127.0.0.1", 8873);
label1.Text = "Client Socket Program - Server Connected ...";
this.imprimir();
}
protected void imprimir()
{
NetworkStream serverStream = clientSocket.GetStream();
byte[] inStream = new byte[10025];
//// LINE THAT PROCESS FOR A LONG TIME IN THE SECOND TIME.
serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
string returndata = System.Text.Encoding.ASCII.GetString(inStream);
int ponto = returndata.IndexOf('.');
returndata = returndata.Substring(0, ponto);
string[] quebraretorno = returndata.Split(';');
ServiceReference1.bematechSoapClient bema = new ServiceReference1.bematechSoapClient();
string r = bema.InformacoesImovelBematech(quebraretorno[0], quebraretorno[1]);
int retorno = -1;
retorno = IniciaPorta("COM7");
if (retorno == 1)
{
ConfiguraModeloImpressora(7);
BematechTX(r);
AcionaGuilhotina(1);
FechaPorta();
}
clientSocket.Close();
}
服务器代码
public void btnImprimeBematech_Locacao()
{
Utilidade.QuebraToken tk = new Utilidade.QuebraToken();
int Credenciada = Convert.ToInt32(tk.CarregaToken(1, HttpContext.Current.Request.Cookies["token"].Value));
TcpListener serverSocket = new TcpListener(8855);
int requestCount = 0;
TcpClient clientSocket = default(TcpClient);
serverSocket.Start();
clientSocket = serverSocket.AcceptTcpClient();
requestCount = 0;
while ((true))
{
try
{
requestCount = requestCount + 1;
NetworkStream networkStream = clientSocket.GetStream();
string serverResponse = Request.QueryString["id"] + ";" + Credenciada.ToString() + ".";
Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
}
catch (Exception ex)
{
}
}
clientSocket.Close();
serverSocket.Stop();
}
谁能帮我 ?