Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Multi Threading a Server
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Donny




PostPosted: 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);
            }

        } catch (IOException ex) {}
        }
}

CODE:

package as;

import java.applet.Applet;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.*;
import java.io.*;

public class Client extends Applet implements KeyListener {
    static Socket socket;
    static DataOutputStream out;
    public void init() {
        try {
            addKeyListener(this);
            socket = new Socket("localhost", 7777);
            out = new DataOutputStream(socket.getOutputStream());
        } catch (UnknownHostException ex) {} catch (IOException ex) {}
       
    }

    public void keyTyped(KeyEvent e) {

    }

    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == 37){
            try {
                out.writeUTF("fuck2");
            } catch (IOException ex) {}
        }
    }

    public void keyReleased(KeyEvent e) {
    }
}
Sponsor
Sponsor
Sponsor
sponsor
Donny




PostPosted: Wed May 25, 2011 7:34 pm   Post subject: RE:Multi Threading a Server

Disregard the cussing in the client file! haha i was frustrated so i was using random words.. lol
Donny




PostPosted: Wed May 25, 2011 7:38 pm   Post subject: RE:Multi Threading a Server

Disregard everything... I'm an idiot. static is way overused
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: