multithreading - Need help getting my basic socket-server working -
so, managed basic single-connection server running, i'm trying convert multi-threaded server small multilayer project wanted do, i'm self-taught , stumble on things, , networking main 1 i've been trying understand long while now, possibly guys can help..
problem: when had 1 thread writeutf
, readutf
worked fine, sent utf , closed connections, although i've moved multi-thread i'm not sure if it's client not sending utf, server not receiving utf, or server not re-sending utf, perhaps guys let me know, here's code.
server sided code
server.java
package testserver.net; /** * server class, server main class * instantiating related server-side functionality, * server listens incoming connections aswell handles * data transmitted between two. * * * @author christian */ import java.io.*; import java.net.*; import java.sql.*; import testserver.net.players.player; public class server { player[] player = new player[config.max_connections]; serversocket serversocket; socket socket; dataoutputstream out; datainputstream in; public server() throws ioexception { // open server on following port system.out.println("attempting setup server..."); serversocket = new serversocket(43594); system.out.println("server officially setup on port: 43594"); // tell server accept connections while (true) { socket = serversocket.accept(); for(int = 0; < config.max_connections; i++) { system.out.println("server waiting connections..."); system.out.println("connection from: " +socket.getinetaddress()); // setup server send out data. out = new dataoutputstream(socket.getoutputstream()); in = new datainputstream(socket.getinputstream()); if(player[i] == null) { player[i] = new player(out, in, player); thread thread = new thread(); thread.start(); break; // end loop start listening more connections } } } } public static void main(string args[]) throws ioexception { new server(); } }
player.java
package testserver.net.players; import testserver.net.*; import java.io.*; public class player implements runnable { player[] player = new player[config.max_connections]; dataoutputstream out; datainputstream in; public player(dataoutputstream out, datainputstream in, player[] player) { this.in = in; this.out = out; this.player = player; } public void run() { while(true) { try { string message = in.readutf(); for(int = 0; < config.max_connections; i++) { if(player[i] != null) { player[i].out.writeutf(message); } } } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } } }
client sided code
client.java
package testclient.net; import java.net.*; import java.util.scanner; import java.io.*; import testclient.net.util.input; public class client { socket socket; datainputstream in; dataoutputstream out; public client() throws unknownhostexception, ioexception { // set client connect (ip, port); system.out.println("attempting connect server on port: 43594"); socket = new socket("localhost", 43594); system.out.println("successfully connected server..."); in = new datainputstream(socket.getinputstream()); out = new dataoutputstream(socket.getoutputstream()); input input = new input(in); thread thread = new thread(input); thread.start(); scanner scanner = new scanner(system.in); while(true) { string sendmessage = scanner.nextline(); out.writeutf(sendmessage); } } public static void main(string args[]) throws unknownhostexception, ioexception { new client(); } }
input.java
package testclient.net.util; import java.io.datainputstream; import java.io.ioexception; public class input implements runnable { datainputstream in; public input(datainputstream in) { this.in = in; } public void run() { while (true) { string message; try { message = in.readutf(); system.out.println(message); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } } }
you create , start thread
object in server, failed pass player
runnable it.
Comments
Post a Comment