Solved

java programming in networking!

Posted on 2004-10-15
17
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

695 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