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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Socket programming problem ---Help needed.

Status
Not open for further replies.

faywang

Programmer
Joined
Sep 13, 2005
Messages
3
Location
CA
I am trying to retrieve data from a provided API by socket.

If I use a telnet client software to retrieve data, the process is as follows:
1. Run the API
2. Telnet to "Localhost:9998" and get the response data "OPP 1.0 127.0.0.1: 4467". "4467" is a randomly generated port.
3. Send web request by entering the following URL in IE browser:
symbol=MSFT"
The format and parameters of this URL are provided by the API provider. "4467" is the number which is generated in the last response.
4. Then the response data will be shown on the telnet screen.

However, problem occurs when I try to connect by socket. For the first request, it works well and I get the data in the format of "OPP 1.0 127.0.0.1:4467". Then I send the web request. When the program comes to reading stream buffer, it stuck there.

Then I tried to add a listener or connect by tcpclient, the problem still can't be solved.

Can anybody tell me where my problem is? Or could you tell me how to diagnose the problem? I'll be very appreciated.

The source code is as follows:
IPHostEntry heserver = new IPHostEntry();
heserver = Dns.GetHostByName("localhost");
ipePoint = new IPEndPoint(heserver.AddressList[0],9998);
Socket socket = new Socket(ipePoint.AddressFamily,SocketType.Stream,ProtocolType.Tcp);
socket.Connect(ipePoint);
if (socket.Connected )
{
networkStream =new NetworkStream(socket);
streamReader = new StreamReader(networkStream);
String port = streamReader.ReadLine(); //read the first line "OPP 1.0"
port = streamReader.ReadLine(); //read the second line"127.0.0.1:4467"

// request from web and get response from telnet
String URL = " + port + "&symbol=MSFT";
WebRequest WReq = WebRequest.Create(URL);

while (true)
{
//set a buffer of 20 characters
char[] buf = new char[20];
streamReader.ReadBlock(buf,0,20);
String msg = buf.ToString();
}
}
 
Your while(true) loop should be controlled by the value you get back from the ReadBlock method. You should loop until it returns 0, meaning there was no more data to be read.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
I'm really an IDIOT. You know where is the problem?? The web request is not sent!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top