在socket之后不在服务器上传递数据。发送C# [英] Data is not delivered at server after socket.Send C#

查看:68
本文介绍了在socket之后不在服务器上传递数据。发送C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的端部编写了一个套接字客户端程序,我将循环数据发送到服务器。但是当我通过socket.send(buffer);循环发送数据时数据没有发送到服务器端。



如果我在两个数据包之间提供一点睡眠,那么它就能正常传送。但是我的睡眠非常关键对于我的应用程序。我需要在启动时同时发送数据包。它是一个金融域应用程序,因此需要同时发送。





一些代码示例: -

I have written a socket client program at my end in which i am sending data in a loop to the server.But when i m sending data i a loop through "socket.send(buffer);" the data is not delivered to the server side.

If i provide a bit of sleep between two data packet then it is delivered properly.But my that sleep is very critical for my application.I need to send packet at the same time when it is initiated.Its a financial domain application so delivery at same time is required.


some code sample:--

IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(Ip), Convert.ToInt32(port));
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);


public void GetConnectedServer()
{
server.Connect(ipep);
}

public void SendData(byte[] data)
{
int sentbytes = server.Send(data,SocketFlags.None);
}

public byte[] RecvResponse()
{
  byte[] recBuffer1 = new byte[1024];
  try
  {
    int recvBytes = server.Receive(recBuffer1, SocketFlags.Peek);
    recBuffer1=new byte[recvBytes];
    recvBytes = server.Receive(recBuffer1, 0, recvBytes, SocketFlags.None);
   }
   catch(SocketException ex)
   {
   throw ex;
   }
return recBuffer1;
}



以上是用于与服务器通信的客户端功能。我收到的工作正常。



问候

Vaibhav


Above is the client fucntions used for coomunication to the server.My receive is working fine.

Regards
Vaibhav

推荐答案

如果您需要立即发送,您考虑过禁用 Nagle 算法( TCP_NODELAY 选项)?



Socket.NoDelay Property [ ^ ]



问候!

Lukasz







添加链接文字以反映文章标题。

[/ Edit]
If you need immediate send have you considered disabling Nagle algorithm(TCP_NODELAY option)?

Socket.NoDelay Property[^]

Regards!
Lukasz



Link text added to reflect the article title.
[/Edit]


我检查了代码及其正确。我建议你可以调试服务器程序并监控通信字节。
i checked the code and its all right. i advise u could debug the server program and monitor the communication bytes.


你选择同步方法发送,它只是将字节复制到基本系统的发送缓冲区。而msdn说为了提高网络的效率,基本系统可能会延迟发送消息,直到它有足够多的字节为止。这就是为什么当你使线程sleep.u可以尝试非阻塞发送时它运行良好的原因。
u choose the synchronical method send,it just copys the bytes to the sending buffer of the basic system.and the msdn says,in order to improve the efficience of the network,the basic system may delay the sending acntion until it has sufficient many bytes.maybe its why it works well when u make the thread sleep.u can try the nonblocking send.


这篇关于在socket之后不在服务器上传递数据。发送C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆