C# socket programming help

Yeah, I know the subject is vague, so I apologize :-)

I'm basically learning c# in my new job during the past month, and I'm feeling fairly comfortable with ASP.net, etc.  I was given the task of making a basic socket listener that would basically listen on a specific port for data, and then write that data to a file.  Eventually the data will be coming at any particular time, and won't be having any sort of header or footer or anything to handshake with.  Pretty much anything that comes on that port we will be using, and right now I'd just like anything that comes on the port to be written to the file.

I found a sample project here:
http://www.codeproject.com/csharp/TCPServerAll.asp df=100&forumid=30826&exp=0&select=702231
that I based my stuff on.  I ended up taking out the part that looks for a header to create the file name (I hard coded that for now) and tweaked it a little bit.  What's happening though is that there is no "eof" being sent (because the data won't have that) and I think its not in turn closing the file once it opens it.  I'm able to telnet to my service and send it data and it works, but if I exit without giving it the "eof" then it hangs and won't accept any more data until I stop and restart the service. 

Can I modify this project to either close the file after every line write (that seems wasteful) or maybe I'm going about this all wrong   Can anyone point me in a better direction   Thanks!


Answer this question

C# socket programming help

  • Ruhina

    Sorry for not helping you from my first post but i just realised that i didn't fully understand what your problem really was.

    Some "telnet" application may send the data different..What i mean by different Well, for example, they may interpreter chars like UP ARROW and stuff like that.I'm not sure if your TCP server can handle special chars like this, and maybe it freezes when you try that.If you want, you can me send me your whole code at my mail, so i can have it a look and help you better this time! :)

    Buffer is the default buffersize of WinSock.



  • ElmueSoft

    Another update...I changed it so that basically for each "line" it reads, it opens the file, writes to the file, then closes it.  It seems kind of inefficient, but it works.  At least it does in telnet.  I can open up any number of telnet sessions, type stuff, and it shows up in the file.  Good deal!  But when I have my people try and use another program (I'm not sure what it is) that is supposed to basically connect to a port and dump data to it.  When I try this, it just freezes the process.  Is there something different in the way telnet would connect vs some other program   It should just be listening on the port for any data to be sent to it! 

    What about buffer sizes   Is there something where it can only have so much sent at one time   I'm trying to look through my tutorial I based it on (mentioned above) to see if theres something in that.  Any more ideas would be great!  Thanks!

  • MarcusLCox

    Thank you for your response!  Yes, I'm having a lot of fun learning this....I'm coming from classic ASP and a VB6 background....but I think I'm picking up a lot here.  Anyway....

    I'm still learning the ins and outs of the Socket class.  I think right now the tutorial I based my project on has a timeout of 15 seconds or something before it kills the connection that was created if there was no activity.  I've found where to change this as well.  What I think is happening is that there is a command to "close" the file, and that's waiting for some data that never comes because that is not the nature of the data I will be receiving.  The same goes for the filename: the data that will be sent is just going to be an occasional stream of data that I think eventually will be loaded into a database or something.  Right now I'm just trying to append everything to a single text file to just get a dump of whats being read.

    But my problem seems to be that the connection timesout because of inactivity,  but the file hasn't been "closed", so then subsequent attempts to connect and open the file look to be failing.  Maybe I'm off base with this completely.  Would it be bad to open and close the file for each line that is read from the stream   The nature of the data being read is that I won't have an "end of file" or some sort of code to look for to terminate the stream.  Its just going to be random data every once in awhile.  Thanks for all your help!!

  • Umeshpradhan

    Hello,

    Nice move to start learning C#..You'll see that is very handy and easy going on many stuff.

    About your project and question..You should set a timeout to the socket, or implement a ping command request at your service so you can check if the user is still alive or not.Setting timeout is very easy, it's a property exposed under the Socket class namespace.That's for the timeout part..To be sure it works ok, try testing your service from different IPs (and then check the threads opened by your connection requests).Also note that every connection should start its own thread as long as you don't want to be able to handle 1 connection per time.In addition note that you could implement of "file handling" in your service (i mean you don't have to hardcode it).The data could be something like this "Filename:Text to be written".You parse by IndexOf or Split (exposed under StringBuilder class/object) to get the Filename and Text to be written.

    Let me know if you need anything else.

    Giz.-


  • C# socket programming help