1,105,332 Community Members

Simple Client Server program

Member Avatar
nHulk
Light Poster
46 posts since Feb 2011
Reputation Points: -5 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
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");
  }
}
}
Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
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.

Member Avatar
nHulk
Light Poster
46 posts since Feb 2011
Reputation Points: -5 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
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());

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
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.

Member Avatar
nHulk
Light Poster
46 posts since Feb 2011
Reputation Points: -5 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

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

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

Member Avatar
nHulk
Light Poster
46 posts since Feb 2011
Reputation Points: -5 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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

Member Avatar
nHulk
Light Poster
46 posts since Feb 2011
Reputation Points: -5 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
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());

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: