Solved

client/server question (beginner level)

Posted on 2003-11-22
7
297 Views
Last Modified: 2010-03-31
Hi, below are two simple files. clientz is a client and serverz is the server.
I can read strings from the server but I want the server to also be able to read strings from the client (on the same port, in this program port no 5003). How to I go about this?. this is really wrecking my head, i just cant figure it out. Any help or suggestions to code/design would be great, thanks.

/*************************************clientz.java*********************************/import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.lang.*;

public class clientz extends Frame {
      TextArea display;
      
      public clientz(){
            super("clientz");
            display = new TextArea(20 , 10);
            add("Center" , display);
            resize(300 , 150);
            show();
      }
      
      public void runClient(){
            Socket client;
            InputStream input;
            //input.read();
            
            try{
                  
                  //receiving message from server      
                  client = new Socket(InetAddress.getLocalHost(),5003);
                  input = client.getInputStream();
                  display.appendText("\nCreated input Stream\n");
                  display.appendText("\nText from server is\n\t");
                  char c;
                  
                  while((c = (char) input.read() ) != '\n')
                        display.appendText(String.valueOf( c ));
            
                  display.appendText("\n");
                  client.close();
                  
                  //sending message to server
      
            }
            catch(IOException e) {
                  e.printStackTrace();
            }
      }
      
            public boolean handleEvent( Event e )
{
      if( e.id == Event.WINDOW_DESTROY ){
            hide();
            dispose();
            System.exit( 0 );
      }
      
      return super.handleEvent( e );
}

public static void main( String args[]){

      clientz c = new clientz();
      c.runClient();

}
}
/***********************************serverz.java*********************************************/
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;

public class serverz extends Frame{
      TextArea display;
      
      public serverz(){
            super("serverz");
            display = new TextArea(20 , 5);
            add("Center" , display);
            resize(300 , 150);
            show();
      }
      
      public void runserverz(){
      
      ServerSocket serve;
      Socket connection;
      OutputStream output;
      
      try{
            //sending message to client
            serve = new ServerSocket(5003 , 1000);
            connection = serve.accept();
            display.setText("\nConnection received");
            output = connection.getOutputStream();
            String s = new String("Has connected\n");
            
            for(int i = 0; i < s.length(); ++i)
                  output.write((int) s.charAt(i));
            
            display.appendText(
                  "\nSent message");
                  
            //receiving message from client
      
      }
      
      catch(IOException e){
            e.printStackTrace();
      }
}
      public boolean handleEvent( Event e )
{
      if( e.id == Event.WINDOW_DESTROY ){
            hide();
            dispose();
            System.exit( 0 );
      }
      
      return super.handleEvent( e );
}

public static void main(String args[]){
      serverz s = new serverz();
      s.runserverz();
}
}
0
Comment
Question by:eire_ireland
[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
  • 4
  • 3
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9803813
You just need to get and use the output stream on the client and input stream on the server respectively. Don't forget to close them after use.
0
 

Author Comment

by:eire_ireland
ID: 9803831
Could you give me an example of this plz, pseudocode will do cos I was tryin something like that and it would'nt work... again thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9803843
Show what you had and we'll try to correct it
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:eire_ireland
ID: 9803923
           //sending message to server
            Socket connection;
            OutputStream output = connection.getOutputStream();
            String s = new String("Has connected\n");
            
            for(int i = 0; i < s.length(); ++i)
                  output.write((int) s.charAt(i));

            OutputStream output = connection.getOutputStream();
                                      ^
--------------------Configuration: j2sdk1.4.0_02 <Default>--------------------
C:\Program Files\Xinox Software\JCreator Pro\MyProjects\clientz.java:41: variable connection might not have been initialized
            OutputStream output = connection.getOutputStream();
                                      ^
Process completed.
/**************************************************************/

Thats the error its giving.

If I initialize the connection again without specifying a different port number it throws an error specifying the port number is already in use.
Any ideas?



                  
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 9804197
You can do this sort of thing:

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.lang.*;

public class clientz extends Frame {
     TextArea display;

     public clientz(){
          super("clientz");
          display = new TextArea(20 , 10);
          add("Center" , display);
          resize(300 , 150);
          show();
     }

     public void runClient(){
          Socket client = null;
          InputStream input = null;
          //input.read();
          PrintWriter output = null;

          try{

               //receiving message from server
               client = new Socket(InetAddress.getLocalHost(),5003);
               input = client.getInputStream();
               display.appendText("\nCreated input Stream\n");
               display.appendText("\nText from server is\n\t");
               char c;

               while((c = (char) input.read() ) != '\n')
                    display.appendText(String.valueOf( c ));

               display.appendText("\n");

               //sending message to server
               output = new PrintWriter(new OutputStreamWriter(client.getOutputStream()), true);
               String message = "Client to Server!";
               output.println(message);

          }
          catch(IOException e) {
               e.printStackTrace();
          }
          finally {
            try {
              input.close();
              output.close();
              client.close();
            }
            catch(IOException e) {
              e.printStackTrace();
            }
          }
     }

          public boolean handleEvent( Event e )
{
     if( e.id == Event.WINDOW_DESTROY ){
          hide();
          dispose();
          System.exit( 0 );
     }

     return super.handleEvent( e );
}

public static void main( String args[]){

     clientz c = new clientz();
     c.runClient();

}
}


//-------------------------------


import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;

public class serverz extends Frame{
     TextArea display;

     public serverz(){
          super("serverz");
          display = new TextArea(20 , 5);
          add("Center" , display);
          resize(300 , 150);
          show();
     }

     public void runserverz(){

     ServerSocket serve = null;
     Socket connection;
     OutputStream output = null;

     BufferedReader input = null;

     try{
          //sending message to client
          serve = new ServerSocket(5003 , 1000);
          connection = serve.accept();
          display.setText("\nConnection received");
          output = connection.getOutputStream();
          String s = new String("Has connected\n");

          for(int i = 0; i < s.length(); ++i)
               output.write((int) s.charAt(i));

          display.appendText(
               "\nSent message");

          //receiving message from client
          input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
          String buffer = null;
          while((buffer = input.readLine()) != null) {
            display.appendText("\n" + buffer);
          }


     }

     catch(IOException e){
          e.printStackTrace();
     }
     // Always close
     finally {
       try {
         output.close();
         input.close();
         serve.close();
       }
       catch(IOException e) {
         e.printStackTrace();
       }
     }
}
     public boolean handleEvent( Event e )
{
     if( e.id == Event.WINDOW_DESTROY ){
          hide();
          dispose();
          System.exit( 0 );
     }

     return super.handleEvent( e );
}

public static void main(String args[]){
     serverz s = new serverz();
     s.runserverz();
}
}
0
 

Author Comment

by:eire_ireland
ID: 9807228
Thats greay, thanks a mil
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9807389
:-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java exception bubble up 2 24
Java: anonymous class 4 38
ejb message driven bean mdb creation steps 2 35
Bot application - advice 3 60
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

749 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