Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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:--
C#
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;
}

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

Regards
Vaibhav
Posted
v3

If you need immediate send have you considered disabling Nagle algorithm(TCP_NODELAY option)?

Socket.NoDelay Property[^]

Regards!
Lukasz


[Edit member="Tadit"]
Link text added to reflect the article title.
[/Edit]
 
Share this answer
 
v3
i checked the code and its all right. i advise u could debug the server program and monitor the communication bytes.
 
Share this answer
 
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.
 
Share this answer
 
Comments
vaibhav vijay 27-Sep-13 8:41am    
I have tried with nonblocking but its not solving the issue.
koll Zhu 27-Sep-13 22:30pm    
its so puzzling...... i once played a trick to attack my colleague's web server.i used socket(tcp) to simulate http protocal(make http message manual and send it ceaselessly to web server identified by an ip address and port 80).
i use the nonblocking method ,it works fine... and the logical code is almost the sanme to urs
vaibhav vijay 28-Sep-13 1:41am    
Actually my all above code is within a asmx webservice.And its my requirement that i have to be in receive all the time whether i m sending any thing or not so if i make the socket non-blocking it will throw exception continuously which can be a performance overhead with my application.Is there any parameter configuration over webservice which might be blocking my send.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900