Solved

client/server question (beginner level)

Posted on 2003-11-22
7
293 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 80
Java Filesystem manipulation of Word Docx file (find replace text) 3 66
micro services spring boot application error 3 146
eclipse argument 14 53
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

920 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

15 Experts available now in Live!

Get 1:1 Help Now