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

Edited by ~s.o.s~: Converted to thread from code snippet

3
Contributors
9
Replies
13
Views
5 Years
Discussion Span
Last Post by NormR1
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.

Votes + Comments
please relpy
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

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

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.