Results 1 to 5 of 5
  1. #1
    kellaw is offline Member
    Join Date
    Oct 2008
    Posts
    24
    Rep Power
    0

    Default [SOLVED] Multiple client single server problem(it hangs halfway)

    TestThread Class

    Java Code:
    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.util.Timer;// for timer
    import java.util.TimerTask;//for timer
    
    public class TestThread {
    
      public static void main(String[] args) {
        DataInputStream inputFromClient1;
        DataOutputStream outputToClient1;
        DataInputStream inputFromClient2;
        DataOutputStream outputToClient2;
    
        ServerSocket serverSocket = null;
    
        System.out.println("MastermindServer started at " + new Date() + "\n");
        try {
          serverSocket = new ServerSocket(8000);
        } catch(IOException ex) {
          System.err.println(ex);
        }
    
        try {
          Socket socket = serverSocket.accept();
          System.out.println("sc1 connected \n\n\n\n");
    
          inputFromClient1 = new DataInputStream(socket.getInputStream());
          outputToClient1 = new DataOutputStream(socket.getOutputStream());
    
          String startMessage = "Two players now,You can start multiGame!";
          outputToClient1.writeUTF(startMessage);
          outputToClient1.writeUTF("hehehe");
          outputToClient1.writeUTF("hahaha");
            
          Socket socket1 = serverSocket.accept();
          System.out.println("sc 2 connected \n\n\n\n");
            
          inputFromClient2 = new DataInputStream(socket1.getInputStream());
          outputToClient2 = new DataOutputStream(socket1.getOutputStream());
            
          outputToClient2.writeUTF(startMessage);
          outputToClient2.writeUTF("hehehe");
          outputToClient2.writeUTF("hahaha");
    //problem starts here when both are not received
          System.out.println("Problem starts here");
          int y = new DataInputStream(socket.getInputStream()).readInt();
          System.out.println("message from client 1 : "+ y);
          int x = new DataInputStream(socket1.getInputStream()).readInt();
          System.out.println("message from client 2 : "+ x);
    
    
          outputToClient1.writeUTF("1  hehehe");
          outputToClient1.writeUTF("1  hahaha");
          outputToClient2.writeUTF("1  hehehe");
          outputToClient2.writeUTF("1  hahaha");
        } catch(IOException ex) {
          System.err.println(ex);
        }
      }
    }


    testSocket class

    Java Code:
    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import javax.swing.border.Border;//for borders
    import javax.swing.border.LineBorder;//for border lines
    import java.awt.event.ActionEvent;//for action event
    import java.awt.event.ActionListener;//for action listener
    import javax.swing.*;
    import java.util.*;
    import java.util.Timer;// for timer
    import java.util.TimerTask;//for timer
    
    
    public class testSocket {
    
      public static void main(String[] args) {
        new testSocket();
      }
      
      public testSocket() {
      	DataOutputStream outputToServer;
        DataInputStream inputFromServer;
    
        try {
          // Create a socket to connect to the server
          Socket socket = new Socket("localhost", 8000);
        
          inputFromServer = new DataInputStream(socket.getInputStream());
          outputToServer = new DataOutputStream(socket.getOutputStream());
          String message = inputFromServer.readUTF();
          System.out.println(message);//start mess
          message = inputFromServer.readUTF();
          System.out.println(message);//hehehe
          message = inputFromServer.readUTF();
          System.out.println(message);//hahaha
          message = inputFromServer.readUTF();
          
          new DataOutputStream(socket.getOutputStream()).writeInt(99);      
          
          System.out.println(message);
          message = inputFromServer.readUTF();
          System.out.println(message);
        
        } catch (IOException ex) {
          System.err.println(ex);
        }
        System.out.println("connected liao");
      }
    }

    Problem description:-

    I simplified this problem from my long code into 2 test files with the problem only.

    TestThread.java is the server
    testSocket.java is the client

    Running TestThread.java first then run testSocket twice without closing any would only show that it executes till line 46 and refuses to run further

    It would execute the TestThread till line 38 and not go further.

    Any help with this problem? Say I delete line 38 of testSocket and line 49-52 of TestThread, it would work fine but wouldn't work with input from different clients which would result in the program to hang there. I need to have input and output from different clients to the server at different time and interchangeable from one client to the other. In my program, the clients does different jobs so use of threads is out of the question.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    see other thread

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Man how many more threads are you going to start for this? I must have seen it like 20 times already between here and Sun. Do you know how obnoxious that is?

    Good luck with your problem. As far as I'm concerned, you're on your own, after this juvenile display.

  4. #4
    kellaw is offline Member
    Join Date
    Oct 2008
    Posts
    24
    Rep Power
    0

    Default

    I have to take all solutions into account to speed up things

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by kellaw View Post
    I have to take all solutions into account to speed up things
    And? Nobody likes to answer on one thread, only to find out that it was totally unneccessary as that information has already appeared in ten other threads.

    No one would hold anything against you creating one thread at Sun and one thread here, but ten on each site is just plain obnoxious and juvenile.

Similar Threads

  1. Replies: 1
    Last Post: 10-02-2008, 08:02 PM
  2. Replies: 6
    Last Post: 07-28-2008, 07:04 PM
  3. passing info between server/client problem
    By DarkBlaze in forum New To Java
    Replies: 13
    Last Post: 07-24-2008, 04:14 AM
  4. Minimal single threaded web server?
    By johann_p in forum New To Java
    Replies: 2
    Last Post: 04-24-2008, 05:37 PM
  5. client-server communication problem
    By revathi17 in forum New To Java
    Replies: 1
    Last Post: 08-09-2007, 03:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •