Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Message from client to server (http://www.go4expert.com/forums/message-client-server-t26719/)

state 17Sep2011 15:07

Message from client to server
 
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 Code:

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 Code:

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

ewaldhorn 23Sep2011 12:23

Re: Message from client to server
 
Hi.

It would appear that your server does not run in a loop. The basic principle of a server is that it continues to listen on the socket.

Perhaps you'd be better served reading the example at http://systembash.com/content/a-simp...nd-tcp-client/ as it's very clear and easy to understand.

Best of luck.


All times are GMT +5.5. The time now is 06:57.