Click here to Skip to main content
13,863,067 members
Rate this:
Please Sign up or sign in to 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:--
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(Ip), Convert.ToInt32(port));
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

public void GetConnectedServer()

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

public byte[] RecvResponse()
  byte[] recBuffer1 = new byte[1024];
    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.

Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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

Socket.NoDelay Property[^]


[Edit member="Tadit"]
Link text added to reflect the article title.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

i checked the code and its all right. i advise u could debug the server program and monitor the communication bytes.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190214.1 | Last Updated 13 Oct 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100