Results 1 to 5 of 5
  1. #1
    scriptkiddie is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default Help what is the error

    Hi friends,
    I begin to learn java about a month ago. I just got a client server chat program.It compiles well and makes the server and client classes. But when running the server message is passed to the client . The vice versa is not happening. I dont know any details of socket programming. So please explain the program for me. Please. Thx in advance



    SERVER



    import java.net.*;
    import java.io.*;
    public class Server extends Thread
    {
    public static ServerSocket sc;
    public String str;
    public BufferedReader r1,r2;
    public PrintStream w;
    public Server(Socket s)
    {
    try
    {
    r1=new BufferedReader(new InputStreamReader(s.getInputStream()));
    r2=new BufferedReader(new InputStreamReader(System.in));
    w=new PrintStream(s.getOutputStream());
    this.start();
    for(;;)
    {
    str=r2.readLine();
    w.println("server:"+str);
    }
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    }
    public static void main(String args[])
    {
    try
    {
    sc=new ServerSocket(4520);
    new Server (sc.accept());
    }
    catch(Exception e)
    {
    System.out.println(e);
    }}}





    CLIENT




    import java.net.*;
    import java.io.*;
    public class Client extends Thread
    {
    public Socket s;
    public String str;
    public BufferedReader r1,r2;
    PrintStream w;
    public Client()
    {
    try
    {
    InetAddress address=InetAddress.getLocalHost();
    s=new Socket(address,4520);
    r1=new BufferedReader(new InputStreamReader(s.getInputStream()));
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    }
    public void run()
    {
    try
    {
    for(;;)
    System.out.println(this.r1.readLine());
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    }
    public static void main(String args[])
    {
    try
    {
    String ad=String.valueOf(InetAddress.getLocalHost())+":";
    Client A=new Client();
    A.r2=new BufferedReader(new InputStreamReader(System.in));
    A.w=new PrintStream(A.s.getOutputStream());
    A.start();
    for(;;)
    {
    String s=A.r2.readLine();
    A.w.println(ad+s);
    }
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    }
    }

  2. #2
    scriptkiddie is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default

    One minute. The Smiley there isnt by me.(;)

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by scriptkiddie View Post
    ...I dont know any details of socket programming. So please explain the program for me....

    ..
    So read a tutorial on socket programming. That will help you to understand the code and to write your own code too.

    P.S:The smileys are there because you did not use code tags to post code.

  4. #4
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    the problem is the smiley!

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    That is a good point, though it may have nothing to do with the problem at hand (I have no idea, can't read unformatted code).

    Why use this bizarre construct:
    Java Code:
    for(;;) {
    // do something
    }
    ?

Similar Threads

  1. Replies: 3
    Last Post: 01-23-2009, 08:53 AM
  2. error 530 error authentication required
    By rgale in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-12-2008, 04:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •