NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Networking/While loop

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
JayManley



Joined: 21 Mar 2017
Posts: 1

PostPosted: Tue Mar 21, 2017 8:55 pm    Post subject: Networking/While loop Reply with quote

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);
        }
    }
   
}
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo