Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

socket.receive hangs infinitely 1

Status
Not open for further replies.

Kavius

Programmer
Apr 11, 2002
322
CA
I am attempting to loop through emails from a POP3 server and retrieve their attachments. Unfortunately, when I attempt to list an email that does not exist, my application hangs.

It appears to be hanging on the receive of the data. I attempted to have it return an error when the socket.Availabe was 0, but then it always returned an error.

Does anyone know what is going on? Why is my app hanging? If the server is returning nothing, do you know why? What does it all mean?

Below is a screen dump of a telnet session that I used to test that it was possible.
[tt]
list
+OK 2 3842
1 1572
2 2270
.
list 1
+OK 1 1572
list 2
+OK 2 2270
list 3
-ERR The specified message is out of range.
[/tt]

This is the log from the session for my application.
[tt]
list
+OK 2 3842
1 1572
2 2270
.
list 1
+OK 1 1572
list 2
+OK 2 2270
list 3
[/tt]
The code hangs at this point (I assume because there is no response). Infact, I checked and the app is hanging on the socket.receive().
Code:
byte[] buffer = new byte[MAX_BUFFER_READ_SIZE];
string line = null;
int byteCount;

try{
  byteCount = sock.Receive(buffer,buffer.Length,0);
  line = Encoding.ASCII.GetString(buffer, 0, byteCount);
}

return(line);
 
I run in such problem and here is how sock.Available==0 was managed. I hope this solution will work for you.
Code:
private int 	CONNECT_SUCCESS = 220;
private int	GENERIC_SUCCESS = 250;
private int	DATA_SUCCESS	= 354;
private int	QUIT_SUCCESS	= 221;


private static bool Check_Response(Socket sock, int response_expected )
{
	string sResponse;
	int response;
	byte[] bytes = new byte[1024];
	// Waiting for response_expected
	while (sock.Available==0)
	{
		System.Threading.Thread.Sleep(100);
	}

	sock.Receive(bytes, 0, s.Available, SocketFlags.None);
	sResponse = Encoding.ASCII.GetString(bytes);
	response = Convert.ToInt32(sResponse.Substring(0,3));
	if(response != response_expected)
		return false;
	return true;
}


public static bool Send(MailMessage message)
{
	IPHostEntry lipa = Dns.Resolve("Your SmtpServer" );
	IPEndPoint target = new IPEndPoint(lipa.AddressList[0], 25);
	Socket sock= new Socket(target.AddressFamily, SocketType.Stream,ProtocolType.Tcp);
	sock.Connect(target);
	if(!Check_Response(s, CONNECT_SUCCESS))
	{
		 // Log Server didn't respond.;
		sock.Close();
		return false;
	}
	// continue 
	Senddata(sock, string.Format("MAIL From: {0}\r\n", message.From ));
	if(!Check_Response(s, GENERIC_SUCCESS))
	{
		// Log command Failed!.
		sock.Close();
		return false;
	}
	//....
	
	
	return true;
	
}

private static void Senddata(Socket sock, string msg)
{
	byte[] msg1 = Encoding.ASCII.GetBytes(msg);
	sock.Send(msg1 , 0, msg1 .Length, SocketFlags.None);
}
-obislavu-
 
Ya, that is pretty much how I had it. The problem is that the server is not sending any information at all. Ever.

I want to wait for a response from the server (I do want a timeout, but that is a different problem), I want to receive the error so that I can tell that there are no more emails.

Is there any reason anyone can think of that I would receive *no* data at all? It only happens with the one command ("list 3" where there is no 3). Maybe this is POP3 specific, but it works fine if I telnet into the server.
 
Send a command like STAT or LAST to get the number of messages in the inbox.
Next send LIST msgnum command
where msgnum is a valid number or matche the response against the following regular expression:
/^-ERR\s*/
The $2 will be the error message.
When you receive the dot that means all messages where read.
-obislavu-

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top