Solved

java programming in networking!

Posted on 2004-10-15
17
172 Views
Last Modified: 2010-03-31
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
Comment
Question by:sraveend
  • 10
  • 5
  • 2
17 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 12325621
> out.println("hello net");

replace that with:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s = in.readLine();
out.println(s);
0
 

Author Comment

by:sraveend
ID: 12325849
Am sorry,i've tried the above code,am getting the prompt to enter input,but it's disabled.
0
 
LVL 92

Expert Comment

by:objects
ID: 12325858
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
 

Author Comment

by:sraveend
ID: 12325882
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
 
LVL 92

Expert Comment

by:objects
ID: 12325901
> but it's disabled.

what do you mean by disabled?
0
 

Author Comment

by:sraveend
ID: 12327925
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
 
LVL 92

Expert Comment

by:objects
ID: 12330299
What prompt are you getting? The above code does not display a prompt.

0
 

Expert Comment

by:Celedor
ID: 12331079
Have you tried .flush()ing the output stream after writing the data?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

by:Celedor
ID: 12331082
PrintStream out=new PrintStream(s.getOutputStream());
out.println("hello net");
out.flush();

Try it!
0
 

Author Comment

by:sraveend
ID: 12332710
Could u please suggest me some website realted to socket with good example
0
 

Author Comment

by:sraveend
ID: 12332937
prompt means ,i meant that the screen is enabled.
0
 

Author Comment

by:sraveend
ID: 12332940
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
 

Author Comment

by:sraveend
ID: 12332950
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
 

Author Comment

by:sraveend
ID: 12334355
am sorry for disturbing u, i got the correct answer after changing the following line

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

Author Comment

by:sraveend
ID: 12334364

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
 

Author Comment

by:sraveend
ID: 12334398
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
 
LVL 92

Expert Comment

by:objects
ID: 12335182
Thats ok, happens to us all :)
Good to hear you got it resolved
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Receive file in Servlet 1 36
bunnyEars challenge 6 62
return in catch statement 1 40
firstChar challenge 13 83
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now