Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-02-2008, 08:19 PM
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-02-2008, 09:02 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Gone to Costa Rica
Posts: 2,223
Norm is on a distinguished road
see other thread
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-02-2008, 10:38 PM
Senior Member
 
Join Date: Jun 2008
Posts: 442
masijade is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-03-2008, 02:39 PM
Member
 
Join Date: Oct 2008
Posts: 24
kellaw is on a distinguished road
I have to take all solutions into account to speed up things
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-03-2008, 09:41 PM
Senior Member
 
Join Date: Jun 2008
Posts: 442
masijade is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Multiple client single server problem(it hangs halfway) kellaw Networking 1 10-02-2008 09:02 PM
Adding Multiple Panels and Single Scroll bar on the JFrame SANDY_INDIA AWT / Swing 6 07-28-2008 08:04 PM
passing info between server/client problem DarkBlaze New To Java 13 07-24-2008 05:14 AM
Minimal single threaded web server? johann_p New To Java 2 04-24-2008 06:37 PM
client-server communication problem revathi17 New To Java 1 08-09-2007 04:21 PM


All times are GMT +3. The time now is 10:02 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org