Results 1 to 4 of 4
  1. #1
    JavaEmpires is offline Member
    Join Date
    Dec 2007
    Posts
    19
    Rep Power
    0

    Default [B]Simple Client connected to server but not exchanging messages[/B]

    Hello all,
    I m new to Networking.
    I m trying to write a simple client-server program wherein server greets client when connected and vice versa.

    Here's is a complete Client and Server program.

    /*Server.java*/

    public class Server extends Thread {
    public static final int V_PORT = 2013;
    protected ServerSocket server_socket;

    public Server()
    {
    try{
    InetAddress iaddr = InetAddress.getLocalHost();
    server_socket = new ServerSocket(V_PORT, 5, iaddr);
    System.out.println("\nConnected to InetAddress " + server_socket.getInetAddress().toString());

    System.out.println("\nPort " + V_PORT+" is up and running");

    }


    catch(Exception e){
    System.out.println("\nError : Port " + V_PORT+" is already used by another process");
    }
    }



    public void run(){
    InputStream receive;
    OutputStream send;
    String msg = "Hello Client WelCome!";
    String output="";
    byte b[] = new byte[100];
    while(true){
    try{
    Socket client = server_socket.accept();
    send = client.getOutputStream();
    send.write(msg.getBytes());
    receive = client.getInputStream();
    receive.read(b);
    System.out.println("\nClient Said : "+ b.toString());



    }
    catch(Exception e){
    System.out.println("\nCould not connect to client" + e.toString());
    e.printStackTrace();

    }
    }

    }

    public static void main(String args[]){
    new Server().start();
    }
    }

    /*Client.java*/

    public class Client {

    public static final int V_PORT = 2013;
    public Socket client_socket = null;
    InputStream receive = null;
    OutputStream send = null;
    String msg="Hello Server";

    public Client(){
    byte b[] = new byte[100];
    try{
    InetAddress addr = InetAddress.getLocalHost();
    client_socket = new Socket(addr, V_PORT);
    System.out.println("\nConnected to port " + V_PORT);
    System.out.println("\nConnected to InetAddress " + client_socket.getInetAddress().toString());
    receive = client_socket.getInputStream();
    receive.read(b);
    System.out.println("\nString read from Server = "+b.toString());

    send = client_socket.getOutputStream();
    send.write(msg.getBytes());
    System.out.println("\nMessage Written to the server");


    }


    catch(Exception e){
    System.out.println("\nClient Error : "+e.toString());
    }
    }


    public static void main(String args[]){
    new Client();
    }
    }

    Please help me to resolve this problem.

    Thanx in advance.

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    I'm not sure I see your problem. See my screenshot to see that I was able to send a message to the server from the client, also upon connect a message was sent from the server.



    Appears to work.. no? just not sure what it is you're requiring it to do exactly.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    The problem you are seeing comes from the line:

    System.out.println("\nString read from Server = "+b.toString());

    This line is calling toString() on the byte array - which by default is simply printing the memory address. I suspect what you are trying to do is print the contents of the byte array, you need to create a string from the bytes:

    System.out.println("\nString read from Server = "+ new String(b));

    Hope that helps

  4. #4
    JavaEmpires is offline Member
    Join Date
    Dec 2007
    Posts
    19
    Rep Power
    0

    Default

    Thank you all.
    That solved my problem.
    Thank you very much

Similar Threads

  1. A Simple Web Server
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:04 PM
  2. client/server messages exchange after 5 min
    By dim_ath in forum Advanced Java
    Replies: 2
    Last Post: 01-22-2008, 08:46 AM
  3. how can server send messages every 5 min?
    By dim_ath in forum Networking
    Replies: 7
    Last Post: 01-10-2008, 03:59 PM
  4. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 AM
  5. Simple example Client Server Application
    By ferosh in forum Networking
    Replies: 1
    Last Post: 04-01-2007, 10:36 AM

Posting Permissions

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