Why my run method is not working?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phpuser123
    New Member
    • Dec 2009
    • 108

    Why my run method is not working?

    This program consists of a client1 named client1 and a server named server.My client is able to connect to the server but the server cannot catch what my client is sending as inputs.

    Here are the codes
    client codes

    <code>
    import java.io.Buffere dInputStream;

    import java.io.Buffere dOutputStream;
    import java.io.IOExcep tion;
    import java.io.InputSt reamReader;
    import java.io.OutputS treamWriter;
    import java.net.InetAd dress;
    import java.net.Socket ;
    import java.io.*;
    import java.util.Scann er;


    public class client1 {
    public static void main(String[] args) {
    /** Define a host server */
    String host = "localhost" ;
    /** Define a port */
    int port = 29999;

    StringBuffer instr = new StringBuffer();
    String TimeStamp;
    System.out.prin tln("SocketClie nt initialized");
    Scanner sc=new Scanner(System. in);

    try {

    Socket connection = new Socket(host, port);





    PrintWriter osw = new PrintWriter(con nection.getOutp utStream());
    TimeStamp = new java.util.Date( ).toString();

    BufferedReader bf=new BufferedReader( new InputStreamRead er(connection.g etInputStream() ));//delete this line

    String server_chat;;
    String user_chat;
    osw.println("se nding client input to server");
    while ((( sc.hasNextLine( ))) && (!(user_chat=sc .nextLine()).eq uals("quit")) && (((server_chat= bf.readLine())! =null) )){
    System.out.prin tln("Server says"+user_chat );
    osw.println("se nding client input to server");
    }

    }
    catch (IOException f) {
    System.out.prin tln("IOExceptio n: " + f);
    }
    catch (Exception g) {
    System.out.prin tln("Exception: " + g);
    }
    }
    }
    </code>



    server codes

    <code>


    //package bdn;
    import java.net.*;

    import java.io.*;
    import java.util.*;

    public class server implements Runnable {

    private Socket connection;
    private String TimeStamp;
    private int ID;
    public static void main(String[] args) {
    int port = 29999;
    int count = 0;
    try{
    ServerSocket socket1 = new ServerSocket(po rt);
    System.out.prin tln("MultipleSo cketServer Initialized");
    // while (true) {

    Socket connection = socket1.accept( );

    server runnable = new server(connecti on, ++count);
    Thread thread = new Thread(runnable );

    thread.start();

    // }
    }
    catch (Exception e) {System.out.pri ntln(e.getMessa ge());}
    }
    server(Socket s, int i) {
    this.connection = s;
    this.ID = i;
    }
    public void run() {
    System.out.prin tln("Testing run ok");
    try {

    String character;



    PrintWriter osw = new PrintWriter(con nection.getOutp utStream());


    // StringBuffer process = new StringBuffer();
    BufferedReader bf=new BufferedReader( new InputStreamRead er(connection.g etInputStream() ));
    while(((charact er = bf.readLine()) !=null)) {
    System.out.prin tln("Server is writing");

    osw.print("char acter");

    }



    }
    catch (Exception e) {
    System.out.prin tln(e);
    }
    finally {
    try {
    connection.clos e();
    }
    catch (IOException e){System.out.p rintln(e);}
    }
    }
    }
    </code>
    Where have I erred in the codes and how can I improve them?


    Thanks in adv
    Last edited by phpuser123; Sep 13 '10, 08:47 AM. Reason: There was an error in the code
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Does it at least prints
    Code:
    System.out.println("Testing run ok");
    this?

    I do not think following code is right
    Code:
    Runnable runnable = new MultipleSocketServer(connection, ++count);
    It gives error for me. I can not compile.
    Is this another class in your project?

    Also please wrap the code in code tags.

    Regards
    Dheeraj Joshi

    Comment

    Working...