14 Replies Latest reply: Feb 8, 2007 11:11 PM by 807597 RSS

    Server/Client Pair Socket Communication

    807597
      Having trouble having the text displayed for both the client and the server.

      Here's my code:
      ///////Client Side//////

      import java.net.*;
      import java.io.*;
      import java.util.*;

      public class clientSide {

      public static void main(String[] args) throws IOException {

      int port=4444;
      Socket client = null;
      Scanner in = null, systemin = null;
      PrintWriter out = null;
      InetAddress address = InetAddress.getLocalHost();


      try{
      client = new Socket(address, port);
      in = new Scanner(client.getInputStream());
      out = new PrintWriter(client.getOutputStream(), true);
      System.out.println("Connected");
      }

      catch(UnknownHostException e){
      System.err.println("Cannot connect to" + e);
      System.exit(1);
      }

      catch(IOException ev){
      System.err.println("Huh?" + ev);
      System.exit(1);
      }

      String line="";
      String otherline="";


      //Send client text to server via the PrintWriter object
      //If user types empty line, exit, Otherwise go on.
      //Second response from client or more prompt "Client Says:"

      System.out.println("What would you like to do?");
      line = in.nextLine();
      systemin = new Scanner(System.in);

      while (line != null)
      {
      System.out.println("Server Says: " + line);
      if(line.equals(" "))
      break;

      otherline = systemin.next();
      if(otherline !=null)
      {
      System.out.println("Client Says: " + otherline);
      out.println(otherline);
      }
      }

      out.close();
      in.close();
      systemin.close();
      client.close();

      }
      }

      ////Server Side////

      import java.net.*;
      import java.io.*;
      import java.util.*;

      public class serverSide {
      public static void main(String[] args) throws IOException {

      int port=4444;
      ServerSocket server = null;
      Scanner in = null, systemin = null;
      PrintWriter out = null;
      Socket sock = null;
      String inputline = "", outputline = "";


      try{
      server = new ServerSocket(port);
      }

      catch(IOException e){
      System.err.println("Cannot connect to the port \n" + e);
      System.exit(-1);
      }

      try{
      server.accept();
      sock = server.accept();
      System.out.println("Connected");
      }

      catch(IOException e) {
      System.err.println("Cannot connect to the port \n" + e);
      System.exit(-1);
      }

      in = new Scanner(sock.getInputStream());
      out = new PrintWriter(sock.getOutputStream(), true);
      System.out.println("Connected");


      outputline = in.next();
      out.println(outputline);

      systemin = new Scanner(System.in);


      while (outputline != null)
      {
      System.out.println("Client Says: " + outputline);
      // outputline = in.println(inputline);
      out.println("Server says: " + outputline);

      if (outputline.equals(" "))
      break;
      }

      }

      }




      The problem is that the client can start to connect without the server....
      trying to figure this out.

      Also, how hard would it be to implement sockets into a client/server GUI(Chatroom)?

      Thanks
        • 1. Re: Server/Client Pair Socket Communication
          EJP
          I don't know what this is supposed to do but the first thing both the server and the client do is try to read from their sockets. This isn't going to make any progress. Someone needs to write first instead of reading first.
          • 2. Re: Server/Client Pair Socket Communication
            807597
            Aright, here's my new code now:


            ///CLIENT////


            import java.io.*;
            import java.net.*;

            public class EchoClient {
            public static void main(String[] args) throws IOException {

            Socket MyClient = null;
            PrintWriter out = null;
            BufferedReader in = null;
            InetAddress address = InetAddress.getLocalHost();
            String Machina = "";
            Machina = address.getHostName();
            DataInputStream input=null;
            PrintStream output=null;

            try{
            MyClient = new Socket(Machina, 4444);
            } catch (UnknownHostException e)
            {
            System.err.println("Don't know about host: taranis.");
            System.exit(1);
            }
            catch (IOException e)
            {
            System.err.println("Couldn't get I/O for "
            + "the connection to: taranis.");
            System.exit(1);
            }

            try {
            input = new DataInputStream(MyClient.getInputStream());
            }
            catch (IOException e) {
            System.out.println(e);
            }

            try {
            output = new PrintStream(MyClient.getOutputStream());
            }
            catch (IOException e) {
            System.out.println(e);
            }



            if(MyClient != null && input != null && output !=null)
            {
            System.out.println("Connecting to Server....");
            System.out.println("Welcome to the TSS text chat client");
            System.out.println("How may I help you?");
            }


            String linu="";

            while ((linu = input.readLine()) !=null)
            {
            System.out.println("Server Says: " + linu + "\n");
            }



            try {
            output.close();
            input.close();
            MyClient.close();
            }
            catch (IOException e) {
            System.out.println(e);
            }

            }

            }


            ///SERVER


            import java.io.*;
            import java.net.*;

            public class EchoServer {
            public static void main(String[] args) throws IOException {

            ServerSocket myService = null;
            PrintWriter out = null;
            BufferedReader in = null;
            Socket serviceSocket = null;
            DataInputStream input = null;
            PrintStream output = null;
            String line=null;

            try {
            myService = new ServerSocket(4444);
            }
            catch (IOException e) {
            System.out.println(e);
            }


            try {
            serviceSocket = myService.accept();
            System.out.println("Here we go");
            input = new DataInputStream(serviceSocket.getInputStream());
            output = new PrintStream(serviceSocket.getOutputStream());
            }
            catch (IOException e)
            {
            System.out.println(e);
            }

            while(true)
            {
            line=input.readLine();
            output.println(line);
            }


            }
            }



            The problem is now it says there's a SocketException,
            One is not able to receive text from the other, and
            I can't write anything on either one.
            • 3. Re: Server/Client Pair Socket Communication
              EJP
              SocketException where? at what line of code? of the client or the server? and with what message text?

              And please don't post any more unformatted code. Nobody will read it.
              • 4. Re: Server/Client Pair Socket Communication
                807597
                for the server at line 36.

                I'm trying to have the ability to type a message after the println is displayed, but Its not letting me.
                • 5. Re: Server/Client Pair Socket Communication
                  807597
                  plus im trying to have each message in the client carry over to the server and vice versa.
                  • 6. Re: Server/Client Pair Socket Communication
                    EJP
                    And what on earth is line 36? Do you expect us to count? in your unformatted code? Please.

                    Every time you get an exception you should print out the error message and the stack trace; have a look at the line of code indicated; and look up the exception being thrown and the API you were calling when you threw it.

                    That will tell you why the exception occurred, 90% of the time. Then you'll be able to answer your own questions. Without that information, nobody can.
                    • 7. Re: Server/Client Pair Socket Communication
                      807597
                      It throws the Socket Exception Here:

                      At the client side it throws it at this line:
                      while ((linu = input.readLine()) !=null)
                      {
                      System.out.println("Server Says: " + linu + "\n");
                      }[

                      Right when the while loop starts for the client

                      I appreciate your help. I'm very new to these boards.haha.

                      Message was edited by:
                      Heyjose25points1
                      • 8. Re: Server/Client Pair Socket Communication
                        807597
                        For the client i can't type in anything after the println
                        "how may I help you?"

                        For the server, it awaits for the client's message inputted.

                        Message was edited by:
                        Heyjose25points1
                        • 9. Re: Server/Client Pair Socket Communication
                          EJP
                          You still haven't done anything about reply #1.

                          You still haven't told us what the message text in the SocketException is.

                          You said it is thrown in the server and now you say it is thrown in the client. Which is it?
                          • 10. Re: Server/Client Pair Socket Communication
                            807597
                            I was able to fix the code and thus I don't get the SocketException anymore, for some reason.

                            The Server is able to send a message to the client. The client is able to receive it. While the server's message to the client has something in it, it displays the message in the client's window. But after that, the client isn't able to respond back...it shows the null pointer exception right when the client tries to input a message back to the server.

                            Now it gives me a NullPointerException right at the bold:

                            try{
                            while ((linu = input.readLine()) != null)

                            {
                                 System.out.println("Server Says: " + linu);
                            userInput = SystemInput.readLine();
                            if (userInput != null)
                            {
                            System.out.println("Client: " + userInput);
                            out.println(userInput);
                            }
                            }
                            }

                            catch (IOException e)
                            {
                            System.err.println(e);
                            }
                            • 11. Re: Server/Client Pair Socket Communication
                              807597
                              SystemInput is the Buff Reader for (System.in)

                              and this occurs in the client.

                              Message was edited by:
                              Heyjose25points1
                              • 12. Re: Server/Client Pair Socket Communication
                                EJP
                                SystemInput is the Buff Reader for
                                (System.in)
                                Obviusly it is still null at this point.
                                • 13. Re: Server/Client Pair Socket Communication
                                  807597
                                  That was the problem, thanks alot.
                                  • 14. Re: Server/Client Pair Socket Communication
                                    807597
                                    can u post the ful code plz. because i am also getting same kind of errors and i am using same concepts in my project.if u post it imwil b very help ful to me