Socket problem
-
can someone help me with this little problem. Im trying to send a few bytes at a time over the connected network. I call socket.BeginSend() to send it async-ly. the problem is when i send my data the server is recieving to much or two little information. im positive the server is working find and im pretty sure the problem lies in the IAsyncResult method called by socket.BeginSend(). Here ill include as little code as possible (since theres alot to make this work) //i read the data here, i only want to send the ammount that //i read of by, sybolized by the variable(read) while((read=st.Read(by,0,by.Length))!= 0) { StateObject ss = new StateObject(s); ss.buffer = by; ss.Available=read; //send by,offset 0 so it reads from begining //followed by only read length incase we are //near the end of the file. s.BeginSend(by,0,read,SocketFlags.None,new AsyncCallback(sen.MainSent),ss); //Thread.Sleep(1); by = new byte[12]; Application.DoEvents(); } //now for the AsyncCallback MainSent public void MainSent(IAsyncResult e) { try { StateObject j = (StateObject)e.AsyncState; int i = j.socket.EndSend(e); if(i>0) { //if i isnt zero there is still data left to be sent. //here is the problem, how do i tell it to only send //the remaining bytes of j.buffer j.socket.BeginSend(j.buffer,i,j.buffer.Length-i,0,new AsyncCallback(MainSent),j); }catch{} } thnx for your time, i hope i can find help so i can just move on from this stupid issue. p.s: the MainSent callback was wrote with the fact in mind that i would send a entire files bytes at once. but now its wrote to only send pieces at a time (in this case 12kb at a time) ~jesse The Code Project Is Your Friend...
-
can someone help me with this little problem. Im trying to send a few bytes at a time over the connected network. I call socket.BeginSend() to send it async-ly. the problem is when i send my data the server is recieving to much or two little information. im positive the server is working find and im pretty sure the problem lies in the IAsyncResult method called by socket.BeginSend(). Here ill include as little code as possible (since theres alot to make this work) //i read the data here, i only want to send the ammount that //i read of by, sybolized by the variable(read) while((read=st.Read(by,0,by.Length))!= 0) { StateObject ss = new StateObject(s); ss.buffer = by; ss.Available=read; //send by,offset 0 so it reads from begining //followed by only read length incase we are //near the end of the file. s.BeginSend(by,0,read,SocketFlags.None,new AsyncCallback(sen.MainSent),ss); //Thread.Sleep(1); by = new byte[12]; Application.DoEvents(); } //now for the AsyncCallback MainSent public void MainSent(IAsyncResult e) { try { StateObject j = (StateObject)e.AsyncState; int i = j.socket.EndSend(e); if(i>0) { //if i isnt zero there is still data left to be sent. //here is the problem, how do i tell it to only send //the remaining bytes of j.buffer j.socket.BeginSend(j.buffer,i,j.buffer.Length-i,0,new AsyncCallback(MainSent),j); }catch{} } thnx for your time, i hope i can find help so i can just move on from this stupid issue. p.s: the MainSent callback was wrote with the fact in mind that i would send a entire files bytes at once. but now its wrote to only send pieces at a time (in this case 12kb at a time) ~jesse The Code Project Is Your Friend...
I take it english isnt your 1st language, so your question is a little hard to understand. A good place to start trying to locate your problem is to put some actual error handling into that catch block, so that we can see any error that occurs,
if(i>0) { //if i isnt zero there is still data left to be sent. //here is the problem, how do i tell it to only send //the remaining bytes of j.buffer j.socket.BeginSend(j.buffer,i,j.buffer.Length-i,0,new AsyncCallback(MainSent),j); } catch(Exception e) { Console.WriteLine(e); }
-
I take it english isnt your 1st language, so your question is a little hard to understand. A good place to start trying to locate your problem is to put some actual error handling into that catch block, so that we can see any error that occurs,
if(i>0) { //if i isnt zero there is still data left to be sent. //here is the problem, how do i tell it to only send //the remaining bytes of j.buffer j.socket.BeginSend(j.buffer,i,j.buffer.Length-i,0,new AsyncCallback(MainSent),j); } catch(Exception e) { Console.WriteLine(e); }