Results 1 to 2 of 2
- 10-02-2008, 06:20 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 24
- Rep Power
- 0
[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.
- 10-02-2008, 07:03 PM #2
Similar Threads
-
[SOLVED] Multiple client single server problem(it hangs halfway)
By kellaw in forum Threads and SynchronizationReplies: 4Last Post: 10-03-2008, 07:41 PM -
[SOLVED] Multiple client single server problem(it hangs halfway)
By kellaw in forum NetworkingReplies: 1Last Post: 10-02-2008, 07:02 PM -
Adding Multiple Panels and Single Scroll bar on the JFrame
By SANDY_INDIA in forum AWT / SwingReplies: 6Last Post: 07-28-2008, 06:04 PM -
passing info between server/client problem
By DarkBlaze in forum New To JavaReplies: 13Last Post: 07-24-2008, 03:14 AM -
client-server communication problem
By revathi17 in forum New To JavaReplies: 1Last Post: 08-09-2007, 02:21 PM
Bookmarks