• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

java programming in networking!

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

public class Client1{
public static final void main(String[] args)
{
try{
InetAddress me=InetAddress.getByName(InetAddress.getLocalHost().getHostName());
Socket s=new Socket(me,80);
PrintStream out=new PrintStream(s.getOutputStream());
out.println("hello net");

}
catch(IOException ie)
{
      ie.printStackTrace(System.err);
}
}
}

import java.net.*;
import java.io.*;
public class TestAdd{
public static final void main(String[] args)
{
while(true){
try{
ServerSocket ss=new ServerSocket(80);
new process(ss.accept()).start();
try
{
      Thread.currentThread().sleep(100);
}catch(InterruptedException ie)
{
}

}
catch(IOException e)
{
}
}
}
}

class process extends Thread
{
      private Socket s;
      public process(Socket soc)
      {
            s=soc;
    }

   public final void run()
   {
         BufferedReader in=null;
         try
         {
               in=new BufferedReader(new InputStreamReader(s.getInputStream()));
       }
       catch(IOException eo)
       {
         try
         {
               s.close();
         }
         catch(Exception e){}
         return;
       }
    while(true)
    {
       try{
            System.out.println(in.readLine());
         try{
               Thread.currentThread().sleep(100);
            }catch(InterruptedException e)
            {}
        }
       catch(Exception e)
       {
               try{
           s.close();
        }
       catch(Exception ex){}
       return;
  }
    }
   }
}

I tried modifying the above client program to receive input from the screen  instead of passing just a String,but i got plenty of errors,can we do that,is there any optons,am new to this networking?


0
sraveend
Asked:
sraveend
  • 10
  • 5
  • 2
1 Solution
 
objectsCommented:
> out.println("hello net");

replace that with:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s = in.readLine();
out.println(s);
0
 
sraveendAuthor Commented:
Am sorry,i've tried the above code,am getting the prompt to enter input,but it's disabled.
0
 
objectsCommented:
you're prompting for input on the server, is that what you want?
Can you explain what it is you are trying to achieve?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
sraveendAuthor Commented:
Actually my program, takes the input
out.println("hello net");
from the client and it prints  on the server.
i just want to prompt the user to enter input from client & whatever the user types must be displayed in the server.    
 
0
 
objectsCommented:
> but it's disabled.

what do you mean by disabled?
0
 
sraveendAuthor Commented:
disabled means am getiing a prompt, but whatever i type is not being displayed on the screen(both client &server).
am not able to view what i type ...............as well as no action is taking place..........it's not stuck.............i could close both the client and server.....
0
 
objectsCommented:
What prompt are you getting? The above code does not display a prompt.

0
 
CeledorCommented:
Have you tried .flush()ing the output stream after writing the data?
0
 
CeledorCommented:
PrintStream out=new PrintStream(s.getOutputStream());
out.println("hello net");
out.flush();

Try it!
0
 
sraveendAuthor Commented:
Could u please suggest me some website realted to socket with good example
0
 
sraveendAuthor Commented:
prompt means ,i meant that the screen is enabled.
0
 
sraveendAuthor Commented:
PrintStream out=new PrintStream(s.getOutputStream());
out.println("hello net");
out.flush();

am getting correct output for the above code,but not with
PrintStream out=new PrintStream(s.getOutputStream());
String yy = xx.readLine();
out.println(yy);
out.flush();
0
 
sraveendAuthor Commented:
am not getting correct output for the code

PrintStream out=new PrintStream(s.getOutputStream());
String yy = xx.readLine();
out.println(yy);
out.flush();
 
no runtime or compile time error,just  xx.readLine() is not receiving any input String.  
 
0
 
sraveendAuthor Commented:
am sorry for disturbing u, i got the correct answer after changing the following line

xx=new BufferedReader(new InputStreamReader(System.in));
0
 
sraveendAuthor Commented:

after changing my client program to

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

public class Client2{
public static final void main(String[] args)
{
try{
BufferedReader xx=null;
InetAddress me=InetAddress.getByName(InetAddress.getLocalHost().getHostName());
Socket s=new Socket(me,80);
xx=new BufferedReader(new InputStreamReader(System.in));
PrintStream out=new PrintStream(s.getOutputStream());
String yy = xx.readLine();
out.println(yy);
out.flush();
}
catch(IOException ie)
{
      ie.printStackTrace(System.err);
}
}
}
0
 
sraveendAuthor Commented:
i wrote the code
  xx=new BufferedReader(new InputStreamReader(s.getInputStream());
It was my mistake,am extremely sorry for confusing u....i didn't notice my error
0
 
objectsCommented:
Thats ok, happens to us all :)
Good to hear you got it resolved
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 10
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now