?
Solved

java programming in networking!

Posted on 2004-10-15
17
Medium Priority
?
187 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 2000 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
Industry Leaders: 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
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…
Suggested Courses
Course of the Month9 days, 1 hour left to enroll

765 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