Below code includes both client and server code.... I am trying to read message from client and display it on server and vice versa......Problem i am facing is both the codes are executin but messages are not getting displayed ....help me

import java.io.*;
import java.net.*;

class Client 
{
 public static void main(String args[])
 {
   try 
   { 
   Socket s=new Socket("127.0.0.1",1122);

    OutputStream os=s.getOutputStream();
     BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));
     bw.write("Hii Server");

    InputStream is=s.getInputStream();
    DataInputStream dis=new DataInputStream(is);
    System.out.println(dis.readLine());


     s.close();
   os.close();
is.close();

    }
   catch(Exception e) 
  {
   System.out.println(e);
  }
}
}     

    //Server Code
    import java.io.*;
import java.net.*;

class Server
{
 public static void main(String args[])
{
  try
 {
  ServerSocket ss=new ServerSocket(1122);
  System.out.println("Waiting For Connection ......");
   while(true)
  {
  Socket s=ss.accept();
  System.out.println("Connection got accepted.....");
  System.out.println("Client Data being read.....");

   InputStream is=s.getInputStream();
    DataInputStream dis=new DataInputStream(is);
    System.out.println(dis.readLine());



   OutputStream os=s.getOutputStream();
   BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));
   bw.write("Hiii client");


  dis.close();
 s.close(); 
  }
 }

 catch(Exception e)
  {
   System.out.println("Error");
  }
}
}

Recommended Answers

All 9 Replies

Put a load of print statements into your code, printing the values of the key variables at each stage so you can see where it's going wrong.

In Server Code ....i feel this code has some problem

InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);
System.out.println(dis.readLine());

  1. With a buffered IO you may need to flush the output buffer for such short messages.
  2. This is bad mistake

    catch(Exception e)
    {
        System.out.println("Error");
    }
    

Use e.printStackTrace(); so you know what the error was and where it happened.

commented: please relpy +0

In Servr Code....It is executiing till this statement

System.out.println("Client Data being read.....");

Output after executing Server code

C:\Java>java Server
Waiting For Connection ......
Connection got accepted.....
Client Data being read.....

where i am going wrong

Did you try flushing the O/P buffers before trying to read a response?

I think i am using wrong code to read message from client...then what is solution ?

InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);
System.out.println(dis.readLine());

Did you try flushing the O/P buffers before trying to read a response?

The readLine() method waits to read an end line character (\n). Did the server send one?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.