1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Message from client to server

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

  1. state

    state New Member

    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

Share This Page