473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

having trouble with asynch sockets

im trying to do a get on a http server and keep appending the return data to
a string and then i even tried an array of strings. But very odd things are
happening. I am completely missing the header but i would get the second
packet twice. Its obviously my code, or im just never gonna get c# working.
please help here is my code.

command button
--------------------------
ManualResetEvent wait = new ManualResetEvent(false);
if(sck.connect(host,"80") == 1)
{
sck.write(head);

while(true)// the correct thing i would assume is get the the byte size
from a http header but not all servers will tell you
{

wait.WaitOne(5000,false); // im waiting 5 seconds just for this example
but still it doesnt matter
retrn=sck.readbuff();

if(retrn.Length > 0)
{
break;
}

}

my sock class
----------------
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace hp
{

public class hpsocket
{

private IPEndPoint iep ;
private AsyncCallback callbackProc ;
private int port ;
private Socket sock ;
int closed = 0;
string rec="";
int tmewait=0;
Byte[] buff = new Byte[32767];
ManualResetEvent wait = new ManualResetEvent(false);
public int connect(string svr,string prt)
{
port = int.Parse(prt);
IPHostEntry IPHost = Dns.Resolve(svr);
string []aliases = IPHost.Aliases;
IPAddress[] addr = IPHost.AddressList;

try
{
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
iep = new IPEndPoint(addr[0],port);
sock.Blocking = false ;
callbackProc = new AsyncCallback(ConnectCallback);
sock.BeginConnect(iep , callbackProc, sock ) ;
}
catch(Exception ex)
{
//MessageBox.Show(ex.Message , "Application Error!!!" ,
MessageBoxButtons.OK , MessageBoxIcon.Stop );
return 0;

}

while(true)
{
if (sock.Connected)
{
return 1;
}
tmewait++;
wait.WaitOne(500,false);
if (tmewait > 15)
{
return 0;
}
}
}
public int close()
{
try
{
sock.Shutdown( SocketShutdown.Both );
sock.Close();
closed = 1;
}
catch(Exception ex)
{
}
return 0;
}

public string readbuff()
{
return rec;
}
public int write(string str)
{
rec="";
try
{
Byte[] smk = new Byte[str.Length];
for ( int i=0; i < str.Length ; i++)
{
Byte ss = Convert.ToByte(str[i]);
smk[i] = ss ;
}

IAsyncResult ar2 = sock.BeginSend(smk , 0 , smk.Length ,
SocketFlags.None , callbackProc , sock );
sock.EndSend(ar2);
}
catch(Exception ers)
{
return 0;
//MessageBox.Show("ERROR IN RESPOND OPTIONS");
}
return 1;
}

public void OnRecievedData( IAsyncResult ar )
{
int nBytesRec;
Socket sock = (Socket)ar.AsyncState;
try
{
nBytesRec = sock.EndReceive( ar );
}
catch(Exception er)
{
nBytesRec = 0;
}

if( nBytesRec > 0 )
{
rec = Encoding.ASCII.GetString( buff, 0, nBytesRec );

}
else
{
if (closed == 0)
{
//sock.Shutdown( SocketShutdown.Both );
//sock.Close();
}
}

}


public void ConnectCallback( IAsyncResult ar )
{
try
{
Socket sock1 = (Socket)ar.AsyncState;
if ( sock1.Connected )
{
AsyncCallback recieveData = new AsyncCallback( OnRecievedData );
sock1.BeginReceive( buff, 0, buff.Length, SocketFlags.None, recieveData
, sock1 );
}
}
catch( Exception ex )
{
//MessageBox.Show( this, ex.Message, "Setup Recieve callbackProc
failed!" );
}
}

}
}

Nov 17 '05 #1
1 1750
what is the point of these threads when i help people time and time again,
but nobody helps me

"Michael Evanchik" wrote:
im trying to do a get on a http server and keep appending the return data to
a string and then i even tried an array of strings. But very odd things are
happening. I am completely missing the header but i would get the second
packet twice. Its obviously my code, or im just never gonna get c# working.
please help here is my code.

command button
--------------------------
ManualResetEvent wait = new ManualResetEvent(false);
if(sck.connect(host,"80") == 1)
{
sck.write(head);

while(true)// the correct thing i would assume is get the the byte size
from a http header but not all servers will tell you
{

wait.WaitOne(5000,false); // im waiting 5 seconds just for this example
but still it doesnt matter
retrn=sck.readbuff();

if(retrn.Length > 0)
{
break;
}

}

my sock class
----------------
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace hp
{

public class hpsocket
{

private IPEndPoint iep ;
private AsyncCallback callbackProc ;
private int port ;
private Socket sock ;
int closed = 0;
string rec="";
int tmewait=0;
Byte[] buff = new Byte[32767];
ManualResetEvent wait = new ManualResetEvent(false);
public int connect(string svr,string prt)
{
port = int.Parse(prt);
IPHostEntry IPHost = Dns.Resolve(svr);
string []aliases = IPHost.Aliases;
IPAddress[] addr = IPHost.AddressList;

try
{
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
iep = new IPEndPoint(addr[0],port);
sock.Blocking = false ;
callbackProc = new AsyncCallback(ConnectCallback);
sock.BeginConnect(iep , callbackProc, sock ) ;
}
catch(Exception ex)
{
//MessageBox.Show(ex.Message , "Application Error!!!" ,
MessageBoxButtons.OK , MessageBoxIcon.Stop );
return 0;

}

while(true)
{
if (sock.Connected)
{
return 1;
}
tmewait++;
wait.WaitOne(500,false);
if (tmewait > 15)
{
return 0;
}
}
}
public int close()
{
try
{
sock.Shutdown( SocketShutdown.Both );
sock.Close();
closed = 1;
}
catch(Exception ex)
{
}
return 0;
}

public string readbuff()
{
return rec;
}
public int write(string str)
{
rec="";
try
{
Byte[] smk = new Byte[str.Length];
for ( int i=0; i < str.Length ; i++)
{
Byte ss = Convert.ToByte(str[i]);
smk[i] = ss ;
}

IAsyncResult ar2 = sock.BeginSend(smk , 0 , smk.Length ,
SocketFlags.None , callbackProc , sock );
sock.EndSend(ar2);
}
catch(Exception ers)
{
return 0;
//MessageBox.Show("ERROR IN RESPOND OPTIONS");
}
return 1;
}

public void OnRecievedData( IAsyncResult ar )
{
int nBytesRec;
Socket sock = (Socket)ar.AsyncState;
try
{
nBytesRec = sock.EndReceive( ar );
}
catch(Exception er)
{
nBytesRec = 0;
}

if( nBytesRec > 0 )
{
rec = Encoding.ASCII.GetString( buff, 0, nBytesRec );

}
else
{
if (closed == 0)
{
//sock.Shutdown( SocketShutdown.Both );
//sock.Close();
}
}

}


public void ConnectCallback( IAsyncResult ar )
{
try
{
Socket sock1 = (Socket)ar.AsyncState;
if ( sock1.Connected )
{
AsyncCallback recieveData = new AsyncCallback( OnRecievedData );
sock1.BeginReceive( buff, 0, buff.Length, SocketFlags.None, recieveData
, sock1 );
}
}
catch( Exception ex )
{
//MessageBox.Show( this, ex.Message, "Setup Recieve callbackProc
failed!" );
}
}

}
}

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Barry Anderberg | last post by:
I am using asynch sockets and so I call BeginReceive and then in my callback function I have certain events that would cause me to want to throw an exception. The problem of course is that the...
11
by: Glen Wolinsky | last post by:
This is my first attempt as asynchronous processing. I have created a small test app as proof of concept, but I am having one proglem. In the example (code listed below), my callback routine has...
2
by: Tim Marsden | last post by:
HI Firstly, I am new to ASP.NET and struggling with the following concepts. I have a win form which gathers information, eg date range , product code etc. when I press a button, a request is...
2
by: ghost | last post by:
As the Subject indicates I have written a web page that makes use of several web service calls. Some of the web service calls are very fast so they called synchronously. Some web service calls...
2
by: Techno_Dex | last post by:
What is the correct way to debug a WS which makes it's call Asynch?
4
by: EM_J | last post by:
I am implementing this interface in one of my pages. The RaiseCallbackEvent method runs a task for about 3 seconds. I've noticed when I am on that page and click a tab to navigate to another...
0
by: rossabri | last post by:
This topic has been addressed in limited detail in other threads: "sockets don't play nice with new style classes :(" May 14 2005....
0
by: Patino | last post by:
I have a particular WS consumer application (Windows app) that was not able to read an error from the WS app because it was calling a method asynch. The client app just hangs there. But once I...
6
by: David | last post by:
http://msdn2.microsoft.com/en-us/library/bew39x2a(VS.80).aspx I was looking at above link and I just don't see the advantage of this. The main thread is just stopping and waiting for each of the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.