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: Simple client-server chat program

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Simple client-server chat program

    Hi
    I just started learning java and given an assignment to write simple server-client chat program.i wrote following code but getting problem with ih.Server is running finebut on client side program ,i am getting error og "unreachable statement "for pw.println(str);.Please help me out....
    Thank You in advance
    <script type="text/javascript">
    <!--
    import java.net.*;
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    public class Client
    {
    Client()


    {
    try
    {
    String str;
    Socket s=new Socket("localhost",1113);
    System.out.println("Connection established");
    OutputStream out=s.getOutputStream();
    PrintWriter pw=new PrintWriter(out,true);
    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    while(true)
    {
    System.out.println("Enter ");
    str=in.readLine();
    }
    pw.println(str);

    }catch(Exception e){}
    }

    public static void main(String args[])
    {
    new Client();
    }
    }
    //-->
    </script>
    <script type="text/javascript">
    <!--
    import java.net.*;
    import java.io.*;
    import java.lang.*;
    class Server
    {
    public static void main(String args[])
    {
    try{
    Socket s1=new Socket();
    ServerSocket svr=new ServerSocket(1113);
    System.out.println("Server Created");
    s1=svr.accept();
    InputStream in=s1.getInputStream();
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String str;
    while(true)
    {
    str=br.readLine();
    if(str.equals("stop"))
    {break;}
    System.out.println(str);
    }
    br.close();

    s1.close();
    }

    catch(Exception e){}

    }
    }
    //-->
    </script>


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Simple client-server chat program

    Please see the following for how to use the code tags appropriately.
    http://www.javaprogrammingforums.com...-get-help.html
    When do you expect the while(true) statement to end?

  3. #3

    Default Re: Simple client-server chat program

    This site has simple solutions for TCP and UDP servers that you can use to establish if what you are doing is correct.

    A Simple Java TCP Server and TCP Client | systemBash
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  4. The Following User Says Thank You to kenster421 For This Useful Post:

    Saloni Patil (October 22nd, 2011)

  5. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple client-server chat program

    I was trying to use while for let me enter string till i type stop.....m not getting where to end it....Please suggest.....

Similar Threads

  1. Questions on simple client-server app
    By worwhite in forum Threads
    Replies: 24
    Last Post: July 30th, 2011, 08:19 PM
  2. Server-Client Chat application using UDP
    By weakprogrammer in forum Java Networking
    Replies: 3
    Last Post: July 1st, 2011, 02:35 PM
  3. simple ftp server and ftp client
    By simontkk2005 in forum Java Networking
    Replies: 4
    Last Post: January 26th, 2011, 10:29 AM
  4. Simple server-client trouble
    By DC200 in forum Java Networking
    Replies: 3
    Last Post: November 12th, 2009, 08:16 AM
  5. Replies: 1
    Last Post: April 20th, 2009, 11:17 AM