Networking/While loop

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Networking/While loop

JayManley
Im testing out the networking side if things, fairly new to Java so i apologise for my newbiness. I am trying to create a client-side application that will connect to a server and retrieve data through the input stream using a While loop and storing it into a string variable named SocketData. Here is my code, please tell me what I am doing wrong...


Code:
package networking.test.app;

import java.net.*;
import java.io.*;

public class NetworkingTestApp {
   
    public static Socket ClientSocket;
    public static DataInputStream IStream;
    public static DataOutputStream OStream;
    public static String SocketData;
   
    public static void ConnectClient(String Host, int Port) {
        try {
            ClientSocket = new Socket(Host, Port);
            IStream = new DataInputStream(ClientSocket.getInputStream());
            OStream = new DataOutputStream(ClientSocket.getOutputStream());
        } catch(UnknownHostException e) {
            System.out.println(e);
        } catch(IOException e) {
            System.out.println(e);
        }
    }
   
    public static void main(String[] args) {
        ConnectClient("127.0.0.1", 2602);
        String St;
        try {
            while ((St = IStream.readLine()) != null) {
                SocketData = SocketData + St;
            }
            System.out.println(SocketData);
        } catch (IOException e) {
            System.out.println(e);
        }
    }
   
}






Loading...