Message from client to server

state's Avatar, Join Date: Sep 2011
Go4Expert 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 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
0
ewaldhorn's Avatar, Join Date: Feb 2010
Go4Expert Member
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.