Sign in to follow this  
Rain Dog

C# Network problem

Recommended Posts

I have a simple client / server based licensing mechanism. The server is repsonsible for looking up user information, determining if they have a license, and sending it back to the client. The server is a c#/asp.net web service. The client is c++ using winsock and making a fake SOAP request. I am currently expieriencing problems with the dll that is responsible for generating license keys in that it is locking the asp.net process until the process is forcefully shutdown from task manager. (The c# standalone client to test this same dll worked fine.) This is not acceptable so. I decided not to run the risk of my companies web sites being out of service, I decided to write my own simple c# server app. Based off of some code I found in the interweb, this is the simple test server I came up with:
using System;
using System.Security.Cryptography;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace RSA_Keys
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>

		static TcpListener Listener;
		[STAThread]
		static void Main(string[] args)
		{
			
			IPAddress ip = IPAddress.Parse("127.0.0.1");
			Listener = new TcpListener(ip, 5550);
			Listener.Start();
			for(int i = 0; i < 10; i++)
			{
				Thread t = new Thread( new ThreadStart(Service));
				t.Start();
			}

		}
		public static void Service()
		{
			while(true)
			{
				Socket soc = Listener.Accept();
				RegistrationHandler Reg = new RegistrationHandler();

				try
				{
					
					NetworkStream s = new NetworkStream(soc); 
					StreamReader sr = new StreamReader(s);
					StreamWriter sw = new StreamWriter(s);
					sw.AutoFlush = true; // enable automatic flushing

					//Retrieve the entire request string
                                        //PROGRAM STOPS WORKING HERE
					string Request = sr.ReadToEnd();
					//Extract the parameters from the request string.
					Reg.ParseRequest(Request);

					//Do the same as the regular Web service would have done.
					byte[] License = Reg.ValidateLicenseRequest();

					//Create the response to the client
					string ClientResponse = Reg.CreateClientResponse(License);

					//Write the response to the socket.
					//sw.Write(ClientResponse);
					//s.Close();
				}
				catch(Exception e)
				{
					ExceptionLogger.LogException(e);
				}
				soc.Close();
			}
		}


		// C# to convert a string to a byte array.
		public static byte[] StrToByteArray(string str)
		{
			System.Text.ASCIIEncoding  encoding = new System.Text.ASCIIEncoding();
			return encoding.GetBytes(str);
		}

		public static string ByteArrayToString(byte[] dBytes)
		{
			System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
			return enc.GetString(dBytes);
		}
	}
}

It's not very large, the class RegistrationHandler does all the work for me. The problem is this: When a client attempts to connect, the listener picks it up just fine and the code runs until the line marked //PROGRAM FREEZES HERE. It appears that the socket is unable to receive data. The same client code successfully makes SOAP requests to the webservice, so it is not a problem with the client. Any help is appreciated.

Share this post


Link to post
Share on other sites
when you call sr.ReadToEnd(), it waits for the connection to close. That means the client will have to close the connection. Perhaps that isn't happening.

[google] turned up this.

Share this post


Link to post
Share on other sites
Ok, the problem I have then is this:

How do I make the receive function return when there is no more data left? IE: the sockets buffer contains no more data, but the connection is still open?

The Idea is to of course retrieve all of the data sent from the client and the process it, the problem is that, it gets all of the data and waits indeffinately for more.


Also, there are a few mistakes in the code I posted above, but they are fixed.

[Edited by - Rain Dog on July 1, 2005 11:06:35 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this