View Single Post
  #1 (permalink)  
Old 10-02-2008, 08:19 PM
kellaw kellaw is offline
Member
 
Join Date: Oct 2008
Posts: 24
kellaw is on a distinguished road
[SOLVED] Multiple client single server problem(it hangs halfway)
TestThread Class

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

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.
Reply With Quote
Sponsored Links