1.11M Members

Simple Client Server program

 
0
 

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");
  }
}
}
 
0
 

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.

 
0
 

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

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

 
0
 
  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.

 
0
 

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

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

 
0
 

Output after executing Server code

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

where i am going wrong

 
0
 

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

 
0
 

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());

 
0
 

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

 
0
 

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

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: