mercredi 1 juillet 2015

Clients unable to send data to server

I am unable to find any error and i just cant get this to work. The program is mean to run a server and have clients able to send data to each other. This problem is that I just cant get any data sent. I followed this tutorial(https://www.youtube.com/watch?v=_1ThWf9Fkfo) so that it could be multi-threaded but i just cant make it work.

Server class:

import java.io.*;
import java.net.*;


public class Server {

    static ServerSocket serverSocket;
    static Socket socket;
    static DataOutputStream out;
    static Users[] user = new Users[10];
    static DataInputStream in;

    public static void main(String[] args) throws Exception {
        System.out.println("Starting Server.");
        serverSocket=new ServerSocket(7777);
        System.out.println("Server Started.");

        while(true){

        socket = serverSocket.accept();
        for (int i = 0; i <10; i++){
        System.out.println("Connection from: " + socket.getInetAddress());
        out = new DataOutputStream(socket.getOutputStream());
        in = new DataInputStream(socket.getInputStream());
        if (user[i] == null) {
        user[i] = new Users(out, in, user);
            Thread thread = new Thread();
        thread.start();
        break;
        }
    }
    }
}
}

class Users implements Runnable{
    DataOutputStream out;
    DataInputStream in;
    Users[] user = new Users[10];

    public Users(DataOutputStream out, DataInputStream in, Users[] user){
        this.out = out;
        this.in = in;
        this.user =user;

    }

    public void run() {
        while(true) {
        try {
            String message = in.readUTF();
            for (int i = 0;i<10;i++) {
                if(user[i] != null) {
                    user[i].out.writeUTF(message);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
}

Client Class:

import java.net.*;
import java.util.Scanner;
import java.io.*;



public class Client {
    static Socket socket;
    static DataInputStream in;
    static DataOutputStream out;

    public static void main(String[] args) throws Exception {
        System.out.println("Connecting...");
        socket = new Socket("localhost",7777);
        System.out.println("Connection Succesful");
        in = new DataInputStream(socket.getInputStream());
        out = new DataOutputStream(socket.getOutputStream());
        Input input = new Input(in);
        Thread thread = new Thread(input);
        thread.start();
        Scanner sc = new Scanner(System.in);
        while(true) {
            String sendMessage = sc.nextLine();
            out.writeUTF(sendMessage);

    }
}
}


class Input implements Runnable{

    private 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) {
                e.printStackTrace();
            }


        }

    }


}

I am pretty sure that it is the clients not being able to send

Aucun commentaire:

Enregistrer un commentaire