How do I make java multiple connections?
-
Ok so basically I've wrote easy java code that communicates with my flash game (actionscript 2) With flash, I got no problems, but with java, I have no idea how can I convert my java socket server code to threaded one.. Help? Code: import java.io.*; import java.net.*; public class server { public static void main(String args[]) { while(true){ // Message terminator char EOF = (char)0x00; int maxPlayers = 10; try { ServerSocket s = new ServerSocket(2807); System.out.println("Server started."); // wait for incoming connections Socket incoming = s.accept(); BufferedReader data_in = new BufferedReader( new InputStreamReader(incoming.getInputStrea… PrintWriter data_out = new PrintWriter(incoming.getOutputStream()); System.out.println("User connected to the server."); data_out.println("You have connected to the server."+EOF); data_out.flush(); boolean quit = false; while (!quit) { String msg = data_in.readLine(); //if (msg == null) quit = true; if (!msg.trim().equals("EXIT")) { System.out.println("User: "+msg.trim()); data_out.println("User: "+msg.trim()+EOF); data_out.flush(); } //else //{ // quit = true; //} } } catch (Exception e) { //System.out.println("Connection lost"); } } } }
-
Answer:
You probably need to read up on Java threading, but the easiest way is to put your socket code into a method with the signature "public void run()". Make your class implement Runnable, then do: Server myServer=new Server(); Thread thread=new Thread(myServer); thread.start(); ADVANCED: I don't think ServerSocket.accept() is thread safe, so you probably want your main method to loop on the accept method. Then, you want to create a new Thread instance every time a Socket is returned. But, you need to pass that Socket instance to the new Thread. The only way to do that is with a Thread subclass that takes a Socket in its constructor. ... Socket incoming=s.accept(); Thread customThread=new ServerThread(incoming); customThread.start(); ...
Leonid at Yahoo! Answers Visit the source
Related Q & A:
- How do I make a window in Java?Best solution by Stack Overflow
- How could I aggregate among multiple collections at once with loop?Best solution by stackoverflow.com
- How do I make my web browser java compatible?Best solution by answers.yahoo.com
- How can I enable Java script?Best solution by eHow old
- How do I hook up multiple units to a Sound Bar?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.