TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
mirco.schoepf
NA
1
0
Problem reading all data from Synchronous Socket
Jan 23 2006 10:09 AM
Hi, I have the task to implements an C# application which communicates with a SAP system via HTTP. My application should act as a HTTP Server to the SAP system. SAP sends documents (PDFs, Word-Docs, Tifs) through HTTP-Requests to my Server. Archive Link is SAP´s name for that HTTP interface. Currently I am using synchronous sockets to process the communication. So basically I do something like this: Thread th = new Thread(new ThreadStart(StartListenThreaded)); th.Start(); The method StartListenThread looks like this: private void StartListenThreaded() { IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); IPEndPoint EndPoint = new IPEndPoint(ipAddress, port); Socket ss = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ss.Bind( EndPoint); ss.Listen(20); try { while(true) { Console.WriteLine("\r\nServer is waiting for a new connection..\r\n"); Socket sock = ss.Accept(); Console.WriteLine("Accepted connection form:"+sock.RemoteEndPoint); Connection con = new Connection(sock); Thread t = new Thread(new ThreadStart(con.ProcessOneRequest)); t.IsBackground=true; t.Priority=ThreadPriority.BelowNormal; t.Start(); } } catch (System.Exception e1) { Console.WriteLine("An Exception Occurred while Listening :" +e1.ToString()); } } As mentioned above There are mostly some Word-Documents or PDF files send as the body of a request. So I need to parse the body and write the content to a file. Currently I use this code to accomplish that: while (true) { if(socket.Available == 0) break; n = socket.Receive(bytes); if(n == 0) break; wr.Write(bytes,0,n); // wr is a BinaryWriter } Now my problem is that this loop breaks before all data is received from the socket. That means it runs into the socket.Available == 0 case and ends, but there is still data available. How did I find that out? Well during debugging the code. With the debugger running and when stepping through the code, obviously leaves the SAP side more time to send its data. When doing some Console.WriteLine() after the loop with the socket.Receive() I get results > 0. So there is still data available. I am not asking an SAP question here. I am aware that the problem could also be on the SAP side. However I think it is a C# issue. Has anyone experienced similar problems with sockets? Am I doing something completely wrong in handling my socket? I also don´t think that I can use Asynchronous sockets beacuse I need to process incoming requests in a determined order. I have also tryed to turn off Blocking mode by setting socket.Blocking = false but I found out that I cannon use that because I need NetworkStream at some point in my application and NetworkStream does not work without blocking mode. Thanks for your help. Mirco
Reply
Answers (
0
)
Passing Data between Classes/Functions
NUnit Testing