Click here to Skip to main content
15,911,132 members
Home / Discussions / Java
   

Java

 
GeneralRe: Chat Server Issue Pin
pasztorpisti6-Aug-12 5:03
pasztorpisti6-Aug-12 5:03 
GeneralRe: Chat Server Issue Pin
Joshua Waring6-Aug-12 5:33
Joshua Waring6-Aug-12 5:33 
GeneralRe: Chat Server Issue Pin
pasztorpisti6-Aug-12 6:22
pasztorpisti6-Aug-12 6:22 
AnswerRe: Chat Server Issue Pin
BobJanova6-Aug-12 5:23
BobJanova6-Aug-12 5:23 
GeneralRe: Chat Server Issue Pin
pasztorpisti6-Aug-12 6:29
pasztorpisti6-Aug-12 6:29 
GeneralRe: Chat Server Issue Pin
Member 93392916-Aug-12 16:11
Member 93392916-Aug-12 16:11 
GeneralRe: Chat Server Issue Pin
pasztorpisti6-Aug-12 19:55
pasztorpisti6-Aug-12 19:55 
GeneralRe: Chat Server Issue Pin
Joshua Waring7-Aug-12 0:17
Joshua Waring7-Aug-12 0:17 
OKAY so here comes the update, I've added it into threads but now a unknown error occurs. Thanks to a debugger I know where and when it occurs but I don't know why. I get a exceptionNullPointer at line 11 of Users.java
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

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

public class Room implements Runnable{
	public void run(){
		ServerSocket serversocket;
		Socket socket;
		Clients client = new Clients();
		try{
			// Created a socket in which clients can connect to.
			serversocket = new ServerSocket(7776);
			// Waits for a client to connect to the server //	
			while(true){
				socket = serversocket.accept();
				System.out.println("Client connected" + socket);
				// Starts their thread
				client.newThread(socket);
			}
		}catch(IOException e){
			System.out.println(e);
		}
	}
	public static void main(String[] args){
		(new Thread(new Room())).start();
	}
}

Java
import java.io.BufferedReader;
import java.io.IOException;
import java.net.Socket;

public class Clients implements Runnable{
	Socket socket1;
	public void run(){
		Socket socket2 = socket1;
		Users userfunction = new Users();
		BufferedReader bf;
		bf = userfunction.addStream(socket2);
		while(true){
			// Receives a Message.
			try{
				String out = bf.readLine();
				System.out.println(out);
				if(out != null)
					userfunction.sendMessageALL(out);
				
			}catch(IOException e){
				System.out.println(e);
			}
			try{
				Thread.sleep(10);
			} catch (InterruptedException e) {}
		}
	}
	public void newThread(Socket socket){
		// Creates one thread for ever client, to parse the socket I sent it to socket1 which is then
		// received in the thread which is taken to socket2..
		socket1 = socket;
		(new Thread(new Clients())).start();
	}
}

Java
import java.net.*;
import java.io.*;
import java.util.ArrayList;

public class Users {
	ArrayList<BufferedReader> recieveAL = new ArrayList<BufferedReader>();
	ArrayList<PrintWriter> sendAL = new ArrayList<PrintWriter>();
	// Creates the BufferedReader and PrintWriter then returns the Reader since the Writer it maintained in the class for the sendMessageAll method.
	public BufferedReader addStream(Socket socket){
		BufferedReader br;
		PrintWriter pw;
		try{
			br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			pw = new PrintWriter(socket.getOutputStream(), true);
			recieveAL.add(br);
			sendAL.add(pw);
			return br;
		}catch(IOException e){
			e.printStackTrace();
		}
		return null;
	}
	// send a message to everyone
	public void sendMessageALL(String send){
		if(send != null){
			for(int x = 0; x < sendAL.size(); x++){
				PrintWriter pw = sendAL.get(x);
				pw.println(send);
				pw.close();
			}
		}
	}
	// Send a message to one person
	public void sendMessage(String send,int x){
		PrintWriter pw = sendAL.get(x);
		pw.println(send);
		pw.close();
	}
}

GeneralRe: Chat Server Issue Pin
pasztorpisti7-Aug-12 0:23
pasztorpisti7-Aug-12 0:23 
GeneralRe: Chat Server Issue Pin
Joshua Waring7-Aug-12 1:07
Joshua Waring7-Aug-12 1:07 
GeneralRe: Chat Server Issue Pin
pasztorpisti7-Aug-12 1:42
pasztorpisti7-Aug-12 1:42 
GeneralRe: Chat Server Issue Pin
Joshua Waring8-Aug-12 0:10
Joshua Waring8-Aug-12 0:10 
GeneralRe: Chat Server Issue Pin
pasztorpisti7-Aug-12 0:52
pasztorpisti7-Aug-12 0:52 
GeneralRe: Chat Server Issue Pin
Joshua Waring7-Aug-12 1:57
Joshua Waring7-Aug-12 1:57 
GeneralRe: Chat Server Issue Pin
pasztorpisti7-Aug-12 2:33
pasztorpisti7-Aug-12 2:33 
GeneralRe: Chat Server Issue Pin
Joshua Waring7-Aug-12 3:18
Joshua Waring7-Aug-12 3:18 
GeneralRe: Chat Server Issue Pin
pasztorpisti7-Aug-12 3:50
pasztorpisti7-Aug-12 3:50 
Questionplease help Pin
Mohammed Kherfan5-Aug-12 0:25
Mohammed Kherfan5-Aug-12 0:25 
AnswerRe: please help Pin
Richard MacCutchan5-Aug-12 1:21
mveRichard MacCutchan5-Aug-12 1:21 
AnswerRe: please help Pin
wo_buhui6-Aug-12 23:18
wo_buhui6-Aug-12 23:18 
Questioninteracting with parent Pin
JR2122-Aug-12 7:11
JR2122-Aug-12 7:11 
AnswerRe: interacting with parent Pin
Nagy Vilmos2-Aug-12 20:40
professionalNagy Vilmos2-Aug-12 20:40 
GeneralRe: interacting with parent Pin
JR2122-Aug-12 21:09
JR2122-Aug-12 21:09 
GeneralRe: interacting with parent Pin
Nagy Vilmos2-Aug-12 21:19
professionalNagy Vilmos2-Aug-12 21:19 
QuestionI want help Pin
Mohammed Kherfan1-Aug-12 22:47
Mohammed Kherfan1-Aug-12 22:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.