Message from client to server

Discussion in 'Java' started by state, Sep 17, 2011.

  1. state

    state New Member

    Joined:
    Sep 14, 2011
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    Hi guys
    My last post did not find any replies.I hope I have not offended anyone.I am new in this forum and this is my second post.I am trying to send a message from client to server and back from server to client.In the client window I get a null whereas in the server window the message is printed.
    CLIENT
    HTML:
    import java.net.*;
    import java.io.*;
    import java.lang.*;
    public class ClientSort
    {
        public static void main(String a[])
        {
            Socket s=null;
            try
            {
                s=new Socket("localhost",6767);
                String mes,msg;
                BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
                PrintWriter out=new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true);
                mes="Sent";
                out.println(mes);
                msg=in.readLine();
                System.out.println(msg);
            }
            catch(IOException e)
            {
                System.out.println(e.getMessage());
            }
            finally
            {
                try{
                s.close();}
                catch(IOException i){System.out.println(i.getMessage());}
            }
        }
    }    
    
    SERVER
    HTML:
    import java.io.*;
    import java.lang.*;
    class ServerSort
    {
        public static void main(String a[]) throws IOException
        {
            ServerSocket ss=new ServerSocket(6767);
            Socket s=null;
            try
            {
                String mes,msg;
                s=ss.accept();
                BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
                PrintWriter out=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
                mes=in.readLine();
                System.out.println(mes);
                msg="Received";
                out.println(msg);
            }
            catch(IOException i)
            {
                System.out.println(i.getMessage());
            }
            finally
            {
                try{
                s.close();}
                catch(IOException io){System.out.println(io.getMessage());}
            }
        }
    }
    
    Kindly Help.Thanks
     
  2. ewaldhorn

    ewaldhorn New Member

    Joined:
    Feb 16, 2010
    Messages:
    36
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    Cape Town, South Africa
    Home Page:
    http://www.javak.co.za

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice