Posted: Wed May 25, 2011 7:30 pm Post subject: Multi Threading a Server
Two threads connect, if i do the first client again it stops the server though... Any suggestions?
Extra Info: I wanna make an online rpg.. I suck at DatagramSockets and stuff but would that be better?
CODE:
package as;
import java.net.*;
import java.io.*;
public class Main {
static ServerSocket server;
static Socket socket;
static Thread th[] = new inout[11];
static int connec;
public static void main(String[] args) throws IOException {
Main m = new Main();
System.out.println("Starting server.");
server = new ServerSocket(7777);
while(true){
socket = server.accept();
System.out.println("Connection.");
connec++;
for (int i = 0; i < 11; i++){
if (th[i] == null){
(th[i] = new inout(socket)).start();
break;
}
}
}
}
}
class inout extends Thread{
static DataInputStream in;
static DataOutputStream out;
static Socket socket;
static Thread thred;
String k;
public inout(Socket s){
socket = s;
}
public void run(){
try {
Main m = new Main();
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
while(true){
k = inout.in.readUTF();
System.out.println(k);
}