Multi Thread Help

Thread Solved

Join Date: Jul 2008
Posts: 92
Reputation: bloody_ninja is an unknown quantity at this point 
Solved Threads: 1
bloody_ninja bloody_ninja is offline Offline
Junior Poster in Training

Multi Thread Help

 
0
  #1
Jul 18th, 2008
  1. package Ninja;
  2.  
  3.  
  4.  
  5.  
  6. import java.io.*;
  7. import java.awt.*;
  8. import java.awt.event.*;
  9.  
  10. import javax.swing.*;
  11.  
  12. import java.net.*;
  13.  
  14. public class NinjaServer implements Runnable {
  15.  
  16. public final static int NULL = 0;
  17. public final static int DISCONNECTED = 1;
  18. public final static int DISCONNECTING = 2;
  19. public final static int BEGIN_CONNECT = 3;
  20. public final static int CONNECTED = 4;
  21.  
  22. public final static String statusMessages[] = {
  23. " Can't Connect!!! Don't Try Again!", " Disconnected",
  24. " Disconnecting...", " Connecting... Please Wait", " Connected"
  25. };
  26. public final static NinjaServer NinjaObj = new NinjaServer();
  27. public final static String END_CHAT_SESSION =
  28. new Character((char)0).toString();
  29.  
  30. public static String hostIP = "localhost";
  31. public static int port = 1337;
  32. public static int connectionStatus = DISCONNECTED;
  33. public static boolean isHost = true;
  34. public static String statusString = statusMessages[connectionStatus];
  35. public static StringBuffer toAppend = new StringBuffer("");
  36. public static StringBuffer toSend = new StringBuffer("");
  37.  
  38.  
  39. // Various GUI components and info
  40. public static JFrame mainFrame = null;
  41. public static JTextArea chatText = null;
  42. public static JTextField chatLine = null;
  43. public static JPanel statusBar = null;
  44. public static JLabel statusField = null;
  45. public static JTextField statusColor = null;
  46. public static JTextField ipField = null;
  47. public static JTextField portField = null;
  48. public static JRadioButton hostOption = null;
  49. public static JRadioButton guestOption = null;
  50. public static JRadioButton NinjaOption = null;
  51. public static JButton helpButton = null;
  52. public static JButton connectButton = null;
  53. public static JButton disconnectButton = null;
  54.  
  55. public static ServerSocket hostServer = null;
  56. public static Socket socket = null;
  57. public static BufferedReader in = null;
  58. public static PrintWriter out = null;
  59.  
  60. private static JPanel initOptionsPane() {
  61. JPanel pane = null;
  62. ActionAdapter buttonListener = null;
  63.  
  64. JPanel optionsPane = new JPanel(new GridLayout(4, 1));
  65.  
  66. pane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  67. pane.add(new JLabel("Computer Name:"));
  68. ipField = new JTextField(10); ipField.setText(hostIP);
  69. ipField.setEnabled(false);
  70. ipField.addFocusListener(new FocusAdapter() {
  71. public void focusLost(FocusEvent e) {
  72. ipField.selectAll();
  73. // Should be editable only when disconnected
  74. if (connectionStatus != DISCONNECTED) {
  75. changeStatusNTS(NULL, true);
  76. }
  77. else {
  78. hostIP = ipField.getText();
  79. }
  80. }
  81. });
  82.  
  83. pane.add(ipField);
  84. optionsPane.add(pane);
  85.  
  86. // Port input
  87. pane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  88. pane.add(new JLabel("Port Number:"));
  89. portField = new JTextField(10); portField.setEditable(true);
  90. portField.setText((new Integer(port)).toString());
  91. portField.addFocusListener(new FocusAdapter() {
  92. public void focusLost(FocusEvent e) {
  93. // should be editable only when disconnected
  94. if (connectionStatus != DISCONNECTED) {
  95. changeStatusNTS(NULL, true);
  96. }
  97. else {
  98. int temp;
  99. try {
  100. temp = Integer.parseInt(portField.getText());
  101. port = temp;
  102. }
  103.  
  104. catch (NumberFormatException nfe) {
  105. portField.setText((new Integer(port)).toString());
  106. mainFrame.repaint();
  107. }
  108. }
  109. }
  110. });
  111. pane.add(portField);
  112. optionsPane.add(pane);
  113.  
  114. buttonListener = new ActionAdapter() {
  115. public void actionPerformed(ActionEvent e) {
  116. if (connectionStatus != DISCONNECTED) {
  117. changeStatusNTS(NULL, true);
  118. }
  119.  
  120. else {
  121. isHost = e.getActionCommand().equals("host");
  122.  
  123. // Cannot supply host IP if host option is chosen
  124. if (isHost) {
  125. ipField.setEnabled(false);
  126. ipField.setText("localhost");
  127. hostIP = "localhost";
  128. }
  129. else {
  130. ipField.setEnabled(true);
  131. }
  132. }
  133. }
  134. };
  135. ButtonGroup bg = new ButtonGroup();
  136. hostOption = new JRadioButton("Host", true);
  137. hostOption.setMnemonic(KeyEvent.VK_H);
  138. hostOption.setActionCommand("host");
  139. hostOption.addActionListener(buttonListener);
  140. guestOption = new JRadioButton("Guest", false);
  141. guestOption.setMnemonic(KeyEvent.VK_G);
  142. guestOption.setActionCommand("guest");
  143. guestOption.addActionListener(buttonListener);
  144.  
  145. NinjaOption = new JRadioButton ("Ninja", false);
  146. NinjaOption.setMnemonic(KeyEvent.VK_N);
  147. NinjaOption.setActionCommand("Ninja");
  148. NinjaOption.addActionListener(buttonListener);
  149.  
  150. bg.add(hostOption);
  151. bg.add(guestOption);
  152. bg.add(NinjaOption);
  153. pane = new JPanel(new GridLayout(1, 2));
  154. pane.add(hostOption);
  155. pane.add(guestOption);
  156. pane.add(NinjaOption);
  157. optionsPane.add(pane);
  158.  
  159. JPanel buttonPane = new JPanel(new GridLayout(1, 2));
  160. buttonListener = new ActionAdapter() {
  161. public void actionPerformed(ActionEvent e) {
  162. // Request a connection initiation
  163. if (e.getActionCommand().equals("connect")) {
  164. changeStatusNTS(BEGIN_CONNECT, true);
  165. }
  166. // Disconnect
  167. else {
  168. changeStatusNTS(DISCONNECTING, true);
  169. }
  170. }
  171. };
  172. connectButton = new JButton("Connect");
  173. connectButton.setMnemonic(KeyEvent.VK_C);
  174. connectButton.setActionCommand("connect");
  175. connectButton.addActionListener(buttonListener);
  176. connectButton.setEnabled(true);
  177. disconnectButton = new JButton("Disconnect");
  178. disconnectButton.setMnemonic(KeyEvent.VK_D);
  179. disconnectButton.setActionCommand("disconnect");
  180. disconnectButton.addActionListener(buttonListener);
  181. disconnectButton.setEnabled(false);
  182. buttonPane.add(connectButton);
  183. buttonPane.add(disconnectButton);
  184. optionsPane.add(buttonPane);
  185.  
  186. return optionsPane;
  187. }
  188. // Initialize all the GUI components and display the frame
  189. private static void initGUI() {
  190. // Set up the status bar
  191. statusField = new JLabel();
  192. statusField.setText(statusMessages[DISCONNECTED]);
  193. statusColor = new JTextField(1);
  194. statusColor.setBackground(Color.red);
  195. statusColor.setEditable(false);
  196. statusBar = new JPanel(new BorderLayout());
  197. statusBar.add(statusColor, BorderLayout.EAST);
  198. statusBar.add(statusField, BorderLayout.CENTER);
  199.  
  200. // Set up the options pane
  201. JPanel optionsPane = initOptionsPane();
  202.  
  203. // Set up the chat pane
  204. JPanel chatPane = new JPanel(new BorderLayout());
  205. chatText = new JTextArea(15, 30);
  206. chatText.setLineWrap(true);
  207. chatText.setEditable(false);
  208. chatText.setForeground(Color.green);
  209. JScrollPane chatTextPane = new JScrollPane(chatText,
  210. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  211. JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  212. chatLine = new JTextField();
  213. chatLine.setEnabled(false);
  214. chatLine.addActionListener(new ActionAdapter() {
  215. public void actionPerformed(ActionEvent e) {
  216. String s = chatLine.getText();
  217. if (!s.equals("")) {
  218. appendToChatBox("OUTGOING: " + s + "\n");
  219. chatLine.selectAll();
  220.  
  221. // Send the string
  222. sendString(s);
  223. }
  224. }
  225. });
  226. chatPane.add(chatLine, BorderLayout.SOUTH);
  227. chatPane.add(chatTextPane, BorderLayout.CENTER);
  228. chatPane.setPreferredSize(new Dimension(200, 200));
  229.  
  230. // Set up the main pane
  231. JPanel mainPane = new JPanel(new BorderLayout());
  232. mainPane.add(statusBar, BorderLayout.SOUTH);
  233. mainPane.add(optionsPane, BorderLayout.WEST);
  234. mainPane.add(chatPane, BorderLayout.CENTER);
  235.  
  236. // Set up the main frame
  237. mainFrame = new JFrame("Simple Instant Messenger (SIM)");
  238. mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  239. mainFrame.setContentPane(mainPane);
  240. mainFrame.setSize(mainFrame.getPreferredSize());
  241. mainFrame.setLocation(1, 1);
  242. mainFrame.pack();
  243. mainFrame.setVisible(true);
  244. }
  245.  
  246. /////////////////////////////////////////////////////////////////
  247.  
  248. // The thread-safe way to change the GUI components while
  249. // changing state
  250. private static void changeStatusTS(int newConnectStatus, boolean noError) {
  251. // Change state if valid state
  252. if (newConnectStatus != NULL) {
  253. connectionStatus = newConnectStatus;
  254. }
  255.  
  256. // If there is no error, display the appropriate status message
  257. if (noError) {
  258. statusString = statusMessages[connectionStatus];
  259. }
  260. // Otherwise, display error message
  261. else {
  262. statusString = statusMessages[NULL];
  263. }
  264.  
  265. // Call the run() routine (Runnable interface) on the
  266. // error-handling and GUI-update thread
  267. SwingUtilities.invokeLater(NinjaObj);
  268. }
  269.  
  270. /////////////////////////////////////////////////////////////////
  271.  
  272. // The non-thread-safe way to change the GUI components while
  273. // changing state
  274. private static void changeStatusNTS(int newConnectStatus, boolean noError) {
  275. // Change state if valid state
  276. if (newConnectStatus != NULL) {
  277. connectionStatus = newConnectStatus;
  278. }
  279.  
  280. // If there is no error, display the appropriate status message
  281. if (noError) {
  282. statusString = statusMessages[connectionStatus];
  283. }
  284. // Otherwise, display error message
  285. else {
  286. statusString = statusMessages[NULL];
  287. }
  288.  
  289. // Call the run() routine (Runnable interface) on the
  290. // current thread
  291. NinjaObj.run();
  292. }
  293.  
  294. /////////////////////////////////////////////////////////////////
  295.  
  296. // Thread-safe way to append to the chat box
  297. private static void appendToChatBox(String s) {
  298. synchronized (toAppend) {
  299. toAppend.append(s);
  300. }
  301. }
  302.  
  303. /////////////////////////////////////////////////////////////////
  304.  
  305. // Add text to send-buffer
  306. private static void sendString(String s) {
  307. synchronized (toSend) {
  308. toSend.append(s + "\n");
  309. }
  310. }
  311.  
  312. /////////////////////////////////////////////////////////////////
  313.  
  314. // Cleanup for disconnect
  315. private static void cleanUp() {
  316. try {
  317. if (hostServer != null) {
  318. hostServer.close();
  319. hostServer = null;
  320. }
  321. }
  322. catch (IOException e) { hostServer = null; }
  323.  
  324. try {
  325. if (socket != null) {
  326. socket.close();
  327. socket = null;
  328. }
  329. }
  330. catch (IOException e) { socket = null; }
  331.  
  332. try {
  333. if (in != null) {
  334. in.close();
  335. in = null;
  336. }
  337. }
  338. catch (IOException e) { in = null; }
  339.  
  340. if (out != null) {
  341. out.close();
  342. out = null;
  343. }
  344. }
  345.  
  346. /////////////////////////////////////////////////////////////////
  347.  
  348. // Checks the current state and sets the enables/disables
  349. // accordingly
  350. public void run() {
  351. switch (connectionStatus) {
  352. case DISCONNECTED:
  353. connectButton.setEnabled(true);
  354. disconnectButton.setEnabled(false);
  355. ipField.setEnabled(true);
  356. portField.setEnabled(true);
  357. hostOption.setEnabled(true);
  358. guestOption.setEnabled(true);
  359. NinjaOption.setEnabled(true);
  360. chatLine.setText(""); chatLine.setEnabled(false);
  361. statusColor.setBackground(Color.red);
  362. break;
  363.  
  364. case DISCONNECTING:
  365. connectButton.setEnabled(false);
  366. disconnectButton.setEnabled(false);
  367. ipField.setEnabled(false);
  368. portField.setEnabled(false);
  369. hostOption.setEnabled(false);
  370. guestOption.setEnabled(false);
  371. NinjaOption.setEnabled(false);
  372. chatLine.setEnabled(false);
  373. statusColor.setBackground(Color.yellow);
  374. break;
  375.  
  376. case CONNECTED:
  377. connectButton.setEnabled(false);
  378. disconnectButton.setEnabled(true);
  379. ipField.setEnabled(false);
  380. portField.setEnabled(false);
  381. hostOption.setEnabled(false);
  382. guestOption.setEnabled(false);
  383. NinjaOption.setEnabled(false);
  384. chatLine.setEnabled(true);
  385. statusColor.setBackground(Color.green);
  386. break;
  387.  
  388. case BEGIN_CONNECT:
  389. connectButton.setEnabled(false);
  390. disconnectButton.setEnabled(false);
  391. ipField.setEnabled(false);
  392. portField.setEnabled(false);
  393. hostOption.setEnabled(false);
  394. guestOption.setEnabled(false);
  395. NinjaOption.setEnabled(false);
  396. chatLine.setEnabled(false);
  397. chatLine.grabFocus();
  398. statusColor.setBackground(Color.yellow);
  399. break;
  400. }
  401.  
  402. // Make sure that the button/text field states are consistent
  403. // with the internal states
  404. ipField.setText(hostIP);
  405. portField.setText((new Integer(port)).toString());
  406. hostOption.setSelected(isHost);
  407. guestOption.setSelected(!isHost);
  408. NinjaOption.setSelected(!isHost);
  409. statusField.setText(statusString);
  410. chatText.append(toAppend.toString());
  411. toAppend.setLength(0);
  412.  
  413. mainFrame.repaint();
  414. }
  415.  
  416.  
  417.  
  418. /////////////////////////////////////////////////////////////////
  419.  
  420. // The main procedure
  421. public static void main(String args[]) throws IOException {
  422. String s;
  423.  
  424. initGUI();
  425.  
  426. while (true) {
  427. try { // Poll every ~10 ms
  428. Thread.sleep(10);
  429. }
  430. catch (InterruptedException e) {}
  431.  
  432. switch (connectionStatus) {
  433. case BEGIN_CONNECT:
  434. try {
  435. // Try to set up a server if host
  436. if (isHost) {
  437. hostServer = new ServerSocket(port);
  438. socket = hostServer.accept();
  439.  
  440. Thread t = new Thread();
  441. t.start();
  442. }
  443.  
  444. // If guest, try to connect to the server
  445. else {
  446. socket = new Socket(hostIP, port);
  447. }
  448.  
  449. in = new BufferedReader(new
  450. InputStreamReader(socket.getInputStream()));
  451. out = new PrintWriter(socket.getOutputStream(), true);
  452. changeStatusTS(CONNECTED, true);
  453. }
  454. // If error, clean up and output an error message
  455. catch (IOException e) {
  456. cleanUp();
  457. changeStatusTS(DISCONNECTED, false);
  458. }
  459. break;
  460.  
  461. case CONNECTED:
  462. try {
  463. // Send data
  464. if (toSend.length() != 0) {
  465. out.print(toSend); out.flush();
  466. toSend.setLength(0);
  467. changeStatusTS(NULL, true);
  468. }
  469.  
  470. // Receive data
  471. if (in.ready()) {
  472. s = in.readLine();
  473. if ((s != null) && (s.length() != 0)) {
  474. // Check if it is the end of a trasmission
  475. if (s.equals(END_CHAT_SESSION)) {
  476. changeStatusTS(DISCONNECTING, true);
  477. }
  478.  
  479. // Otherwise, receive what text
  480. else {
  481. appendToChatBox("INCOMING: " + s + "\n");
  482. changeStatusTS(NULL, true);
  483. }
  484. }
  485. }
  486. }
  487. catch (IOException e) {
  488. cleanUp();
  489. changeStatusTS(DISCONNECTED, false);
  490. }
  491. break;
  492.  
  493. case DISCONNECTING:
  494. // Tell other chatter to disconnect as well
  495. out.print(END_CHAT_SESSION); out.flush();
  496.  
  497. // Clean up (close all streams/sockets)
  498. cleanUp();
  499. changeStatusTS(DISCONNECTED, true);
  500. break;
  501.  
  502. default: break; // do nothing
  503. }
  504. }
  505. }
  506. }
  507.  
  508. ////////////////////////////////////////////////////////////////// //
  509.  
  510. // Action adapter for easy event-listener coding
  511. class ActionAdapter implements ActionListener {
  512. public void actionPerformed(ActionEvent e) {}
  513. }
  514.  
  515. ////////////////////////////////////////////////////////////////// //

I have two problems with this instant messenger at the current moment.

First : I still can't figure out how to get multiple clients to connect to the host. It says connected, but there is no actual Incoming and Outgoing messages.

Second : When I connect to the server as guest, once connected, it switches over to Ninja
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 92
Reputation: bloody_ninja is an unknown quantity at this point 
Solved Threads: 1
bloody_ninja bloody_ninja is offline Offline
Junior Poster in Training

Re: Multi Thread Help

 
0
  #2
Jul 20th, 2008
^bump
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 92
Reputation: bloody_ninja is an unknown quantity at this point 
Solved Threads: 1
bloody_ninja bloody_ninja is offline Offline
Junior Poster in Training

Re: Multi Thread Help

 
0
  #3
Jul 21st, 2008
Someone please help?

Okay, let me shorten this a little.

My goal is to make a multiclient Instant Messenger program. When executing, there are no errors, but it can only support host / client. I am not sure where to put and exactly what to put to make it a multi user program.

  1. public static void main(String args[]) throws IOException {
  2. String s;
  3.  
  4. initGUI();
  5.  
  6. while (true) {
  7.  
  8. NinjaServer n = null;
  9. try { // Poll every ~10 ms
  10. Thread.sleep(10);
  11. }
  12. catch (InterruptedException e) {}
  13.  
  14. switch (connectionStatus) {
  15. case BEGIN_CONNECT:
  16. try {
  17. // Try to set up a server if host
  18. if (isHost) {
  19. hostServer = new ServerSocket(port);
  20. socket = hostServer.accept();
  21.  
  22. Thread t = new Thread(n);
  23. t.start();
  24.  
  25. }
  26.  
  27. // If guest, try to connect to the server
  28. else {
  29. socket = new Socket(hostIP, port);
  30. }
  31.  
  32. in = new BufferedReader(new
  33. InputStreamReader(socket.getInputStream()));
  34. out = new PrintWriter(socket.getOutputStream(), true);
  35. changeStatusTS(CONNECTED, true);
  36. }
  37. // If error, clean up and output an error message
  38. catch (IOException e) {
  39. cleanUp();
  40. changeStatusTS(DISCONNECTED, false);
  41. }
  42. break;
  43.  
  44. case CONNECTED:
  45. try {
  46. // Send data
  47. if (toSend.length() != 0) {
  48. out.print(toSend); out.flush();
  49. toSend.setLength(0);
  50. changeStatusTS(NULL, true);
  51. }
  52.  
  53. // Receive data
  54. if (in.ready()) {
  55. s = in.readLine();
  56. if ((s != null) && (s.length() != 0)) {
  57. // Check if it is the end of a trasmission
  58. if (s.equals(END_CHAT_SESSION)) {
  59. changeStatusTS(DISCONNECTING, true);
  60. }
  61.  
  62. // Otherwise, receive what text
  63. else {
  64. appendToChatBox("INCOMING: " + s + "\n");
  65. changeStatusTS(NULL, true);
  66. }
  67. }
  68. }
  69. }
  70. catch (IOException e) {
  71. cleanUp();
  72. changeStatusTS(DISCONNECTED, false);
  73. }
  74. break;
  75.  
  76. case DISCONNECTING:
  77. // Tell other chatter to disconnect as well
  78. out.print(END_CHAT_SESSION); out.flush();
  79.  
  80. // Clean up (close all streams/sockets)
  81. cleanUp();
  82. changeStatusTS(DISCONNECTED, true);
  83. break;
  84.  
  85. default: break; // do nothing
  86. }
  87. }
  88. }
  89. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 92
Reputation: bloody_ninja is an unknown quantity at this point 
Solved Threads: 1
bloody_ninja bloody_ninja is offline Offline
Junior Poster in Training

Re: Multi Thread Help

 
0
  #4
Jul 22nd, 2008
No one has any input or advice ?

Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 43
Reputation: sanzilla has a little shameless behaviour in the past 
Solved Threads: 3
sanzilla sanzilla is offline Offline
Unverified User

Re: Multi Thread Help

 
0
  #5
Jul 23rd, 2008
When executing, there are no errors, but it can only support host / client.
hmm , I have traced your code , this like a Serial port RS-232 communication .

I think you have to connect to the server to enable the multi user chat , like the IRC chat ! I think you have to change the coce

  1. // Try to set up a server if host
  2. if (isHost) {
  3. hostServer = new ServerSocket(port);
  4. socket = hostServer.accept();
  5. Thread t = new Thread(n);
  6. t.start();
  7. }
  8. // If guest, try to connect to the server
  9. else {
  10. socket = new Socket(hostIP, port);
  11.  
  12. }
something should be change here ....


just put a watch point on the variable socket and see what is the really value on it on the runtime . after executing the above me quoted code .
Last edited by sanzilla; Jul 23rd, 2008 at 3:33 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 43
Reputation: sanzilla has a little shameless behaviour in the past 
Solved Threads: 3
sanzilla sanzilla is offline Offline
Unverified User

Re: Multi Thread Help

 
0
  #6
Jul 23rd, 2008
hmm , nice chat program that you copied and pasted from ,


public static String hostIP = "localhost";
and localhost means your computer , then you are using the server as your computer
if you are start chat with him . Look at the quoted text in my previous post . If your friend is starting chat with you then his computer is working as a server and your computer just accept that connection .

So if you want's to chat more than 2 users , then you have to make a intermeadiate server and make that server to work as a router to forward your msg to the friend .
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 92
Reputation: bloody_ninja is an unknown quantity at this point 
Solved Threads: 1
bloody_ninja bloody_ninja is offline Offline
Junior Poster in Training

Re: Multi Thread Help

 
0
  #7
Jul 23rd, 2008
I dont get what you are trying to say. I do connect to the server, but that doesn't enable multiple clients.

What is a watch point? Sorry , I just started recently and don't know all the terms yet.

What I don't get is why it won't just allow more clients, because I am starting new threads and everything.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 92
Reputation: bloody_ninja is an unknown quantity at this point 
Solved Threads: 1
bloody_ninja bloody_ninja is offline Offline
Junior Poster in Training

Re: Multi Thread Help

 
0
  #8
Jul 23rd, 2008
This guy , the only guy that attempted helping, responded 32 minutes ago. Now he is banned.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 43
Reputation: sanzilla has a little shameless behaviour in the past 
Solved Threads: 3
sanzilla sanzilla is offline Offline
Unverified User

Re: Multi Thread Help

 
0
  #9
Jul 23rd, 2008
  1. // Try to set up a server if host
  2. if (isHost) {
  3. hostServer = new ServerSocket(port);
  4. socket = hostServer.accept();
  5. Thread t = new Thread(n);
  6. t.start();
  7. }
  8. // If guest, try to connect to the server
  9. else {
  10. socket = new Socket(hostIP, port);
  11.  
  12. }
Look at this code , think what was happning
in this code , in nutshell let me explain ,

when you wants to start the chat with your friend , your machine localhost is act like a server and your friend is just accepting that incomming connection . and when your friend is starting the chat then his computer was act like a server and you just act like a host .


so you have to change the code to always connects to a dedicated server and that server should be programmed to route and forward messages . then you are post your message to the server , and the server will post it to all the clients currently chatting except you . so change the code to do that .

if you dont know who to use the watch points and breakpoints lean debugging first , that is very useful when you are developing more complex software .
use the netbeans , that will help you to debug with a GUI , otherwise you have to go to the command prompt . so
http://debuggercore.netbeans.org/nonav/sourcesDemo.html
may be help you
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 92
Reputation: bloody_ninja is an unknown quantity at this point 
Solved Threads: 1
bloody_ninja bloody_ninja is offline Offline
Junior Poster in Training

Re: Multi Thread Help

 
0
  #10
Jul 23rd, 2008
I am not even trying to connect to friends yet. I am just trying to runn multiple clients on my own computer and connect them all to myself. Except only one connects.

But what you said defeats my purpose. I also want the server to be able to communicate to clients, not just relay messages.

Otherwise, if multiple people used it, one person would have to run a client and a server on the same computer.

"when you wants to start the chat with your friend , your machine localhost is act like a server and your friend is just accepting that incomming connection ."

I thought that the server accepts the incoming connections.

And I am currently using Eclipse, don't want to change mid project, so there has to be a way.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC