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();
}
}
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();
}
}