Results 1 to 15 of 15
  1. #1
    helioshilary is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default Really big problem in client and server program

    Friends here is my client program in java using sockets
    Java Code:
    import java.io.*;
    import java.net.*;
    class Client
    {
    int count=0;
    public static void main(String args[]) throws Exception
    {
    int count=0;
    Socket s=new Socket("LocalHost",2000);
    DataOutputStream dos=new DataOutputStream(s.getOutputStream());
    String str=new String();
    while(true)
    {
    try
    {
    Thread.sleep(1000);
    count++;
    str=""+count+"";
    dos.writeBytes("str");
    System.out.println(str);
    }
    catch(Exception e)
    {}
    }
    }
    }
    Here client sends seconds to server by using thread and incrementing a variable for every 1 second.

    And server program is
    Java Code:
    import java.io.*;
    import java.net.*;
    class Server
    {
    public static void main(String args[]) throws Exception
    {
    ServerSocket ss=new ServerSocket(2000);
    Socket s=ss.accept();
    DataInputStream dis=new DataInputStream(s.getInputStream());
    String str=dis.readLine();
    System.out.println("While");
    while(true)
    {
    System.out.println(str);
    str=dis.readLine();
    }
    }
    }
    THERE IS NO ERRORS IN ABOVE CODE

    Now the big problem is server is waiting for message from client , but client is posting the values to the server program.
    When i execute the program by executing server first next to client(which is to be) client is sending data (which i can see the values the client is printing in cmd) but server program just stays in the line "String str=dis.readLine();" and just waiting for input...
    So please help me in getting this code..
    Thanks.....
    Attached Thumbnails Attached Thumbnails Really big problem in client and server program-untitled.jpg  
    Last edited by helioshilary; 07-12-2011 at 06:50 PM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    It amazes me that people can figure out how to post multiple unnecessary animated icons which do nothing to help explain the problem, but they can't figure out how to post code so that is retains its formatting and is actually readable.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    @OP: Use code tags
    [code]
    YOUR CODE HERE
    [/code]

    And ask specific questions. If you have errors, post them as well(in code tags).

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    server is not getting data from server
    This is confusing. How is the server getting data from the server?

    Please copy and paste the output from your program that traces what it is doing.
    Add comments to the output that describes what is wrong and what you want it to do differently.

  5. #5
    helioshilary is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default

    in the image


    the main problem is the server command prompt must also get the values as it is in client command prompt,,,,,but server program is not getting any value and it is not displaying any..

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Your write methods do not match your input methods.
    The you are writing bytes and trying to read a String with a lineend.
    Change the methods so they match.

  7. #7
    helioshilary is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default

    in which code is that in server or client one?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    There is code in both the server and in the client.

  9. #9
    helioshilary is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default

    Then what method i have to use in client code and what method i have to read in server code..

  10. #10
    helioshilary is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default

    the same methods even worked for me when i try to send some string from client to server by reading from user...........

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    the same methods even worked for me
    how is the code different when it worked than in the code you posted here?

  12. #12
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    You probably need to flush() your output stream.
    Get in the habit of using standard Java naming conventions!

  13. #13
    helioshilary is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default

    The code is simple
    Java Code:
    Server code
    
    import java.io.*;
    import java.net.*;
    class Server
    {
    public static void main(String args[]) throws Exception
    {
    System.out.println("Waiting for client message.....");
    ServerSocket ss=new ServerSocket(2000);
    Socket s=ss.accept();
    DataInputStream dis=new DataInputStream(s.getInputStream());
    String str=dis.readLine();
    while(!(str.equals("123")))
    {
    System.out.println("Client :"+str);
    str=dis.readLine();
    }
    }
    }
    Java Code:
    Client code
    
    import java.io.*;
    import java.net.*;
    class Client
    {
    public static void main(String args[]) throws Exception
    {
    Socket s=new Socket("LocalHost",2000);
    DataOutputStream dos=new DataOutputStream(s.getOutputStream());
    DataInputStream dis=new DataInputStream(System.in);
    System.out.println("Enter msg:");
    String str=dis.readLine();
    while(true)
    {
    if(str.equals("123"))
    {
    dos.writeBytes(str);
    System.exit(0);
    }
    else
    dos.writeBytes(str+"\n");
    System.out.println("Enter msg:");
    str=dis.readLine();
    }
    //s.close();
    }
    }
    This code is working all i need to be executed the previous code using threads,,,please help me.....
    Attached Thumbnails Attached Thumbnails Really big problem in client and server program-untitled2.jpg  

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Do you really write your code with no indentations for nesting/scope levels?
    It makes it very hard to read if the nested code is not indented. Especially to find matching { and }

    Following from the Client code in your post #13
    dos.writeBytes(str+"\n"); // Notice the \n here
    See the second line in post #6
    Last edited by Norm; 07-13-2011 at 03:26 AM.

  15. #15
    helioshilary is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

Similar Threads

  1. Simple Client (2) - Server program
    By Reztem in forum New To Java
    Replies: 2
    Last Post: 01-09-2012, 02:05 AM
  2. Replies: 1
    Last Post: 02-16-2011, 10:05 AM
  3. Simple Socket program: Java Client- C server
    By pimmling in forum New To Java
    Replies: 1
    Last Post: 11-08-2010, 01:27 PM
  4. Client Server program, a tiny problem.
    By skarosg3 in forum Networking
    Replies: 12
    Last Post: 05-27-2010, 01:03 PM
  5. Multithreaded Client/Server Chat program
    By f0ns in forum Threads and Synchronization
    Replies: 3
    Last Post: 10-21-2009, 05:26 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
  •