Solved

java programming in networking!

Posted on 2004-10-15
17
176 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 105
recursion example 16 126
configure dependency in POM for new database 3 25
Is there a simpler dropbox system? 10 19
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

770 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