Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java socket

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java socket

    Hey. i have this problem where my java socket client instantlly shuts down after i run it. heres the code:
    package basic;
     
    import java.io.*;
    import java.net.*;
     
    public class LoginClient {
        public static void main(String argv[]) throws Exception {
            String sentence;
            String modifiedSentence;
            BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
     
            Socket clientSocket = new Socket("localhost", 6789);
            DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
            BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
     
            sentence = inFromUser.readLine();
            outToServer.writeBytes(sentence + '\n');
            modifiedSentence = inFromServer.readLine();
            System.out.println(modifiedSentence);
        }
    }
    I dont understand what is wrong. im begginer.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java socket

    The program will stop executing when the main() method exits unless there is one or more threads running.

    What do you want the program to do after it prints out the modifiedSentence?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java socket

    Quote Originally Posted by Norm View Post
    The program will stop executing when the main() method exits unless there is one or more threads running.

    What do you want the program to do after it prints out the modifiedSentence?
    I dont really know. I took this code from the internet. tryed to to while(true) loop but it still instantlly exitted.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java socket

    A while(true) loop should not instantly exit, unless there was an exception thrown.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 12
    Last Post: October 22nd, 2013, 09:11 PM
  2. Java Socket Programming
    By varunkukreja24 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 17th, 2013, 11:34 PM
  3. Java socket corruption
    By newbie14 in forum Java Networking
    Replies: 17
    Last Post: July 3rd, 2012, 08:56 AM
  4. Socket java client
    By atef201080 in forum Java Networking
    Replies: 4
    Last Post: July 13th, 2011, 08:35 AM
  5. Java socket programming
    By lucy in forum Java Networking
    Replies: 1
    Last Post: April 26th, 2010, 09:13 PM