Hey guys i'm having some trouble reading from objectinputStream twice, it just stops after reading ones.
It dosn't throw any exceptions, just hangs on the spot (see code below u have highlighted the important bits with ##)
Note: i'm using threads;
Code
package testingchat;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author
*/
public class ChatServer {
private ArrayList<ObjectOutputStream> streamList; //list of clients Currently connected
private ObjectOutputStream testOfflineStream = null;// used for removing offlineClient
private UserData verifyUserName = null; //verifys in coming clients username with the collection of clients
private Set<UserData> dataList = new HashSet<UserData>();
private ArrayList<ObjectInputStream> inStreamList;
/**
* Constructor
*/
public ChatServer() {
streamList = new ArrayList<ObjectOutputStream>();
inStreamList = new ArrayList<ObjectInputStream>();
}
/**
* Accepting the Connection from Client
*/
public void makeConnection() {
ObjectOutputStream oos = null;
try {
ServerSocket serversocket = new ServerSocket(5000);
System.out.println("connecting to client......");
while (true) {
Socket client = serversocket.accept();
System.out.println("Connected to client");
oos = new ObjectOutputStream(client.getOutputStream());
streamList.add(oos);
Thread newThread = new Thread(new ClientHandler(client));
newThread.start();//<[################]Thread starting here###########b]
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* This Inner class reads the object sent from client (via Socket).
* which is then sent to appropriate method to be processed further, then
* later on the object is sent to multiple clients.
*/
public class ClientHandler implements Runnable {
UserData inComingData;
ObjectInputStream ois = null; //Used to read object from Socket
ObjectInputStream stream = null;
UserData testObject1 = new UserData();
/*
* Inner Class constructor
*/
public ClientHandler(Socket clientSocket) {
try {
System.out.println("72");
ois = new ObjectInputStream(clientSocket.getInputStream());
System.out.println("line 74");
inStreamList.add(ois); <###i have a collection of objectinputstream so i can read from them later on[###]
System.out.println("line 78");
testObject1 = (UserData) ois.readObject(); <[####]i'm reading it here first[###]
System.out.println("Lisening 76");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Used by Thread
*/
public void run() {
int counter = 0;
System.out.println("line 87");
try {
System.out.println("line 90");
System.out.println("line 92");
for(ObjectInputStream incoming : inStreamList){
System.out.println("line 94");
inComingData = (UserData) incoming.readObject(); <[###]But for some reason it stops here doesn't go further[###]-------------------------------------------
System.out.println("line 96");
if(inComingData.getUserVerification().equals("Checking") &&
inComingData.getUserName().equals(testObject1.getUserName())){
System.out.println("line 99");
counter++;
}
if(counter > 1){
System.out.println("line 103");
inComingData.setUserVerification("Not Valid");
System.out.println("Sent line 100");
}
<[####]Don't have to worry about the code below just wanna check if its possible to read twice from objectinputStream. whats really annoying[####
System.out.println("90");
if (inComingData.getUserVerification().equals("Checking")) {
dataList.add(inComingData);//inComingData is added to list of data
System.out.println("94");
confirmUserName(inComingData);
System.out.println("line 81 "+inComingData );
} else if (inComingData.getUserVerification().equals("Valid")) {
sendData(inComingData);
System.out.println("line 84 "+inComingData );
}
}
System.out.println("line 77"+inComingData.getUserVerification());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* This method is used for sending Data to multiple clients.
* @param user
*/
public synchronized void sendData(UserData data) throws InterruptedException {
try {
System.out.println(streamList);
System.out.println("102");
for (ObjectOutputStream outGoing : streamList) {
testOfflineStream = outGoing;
outGoing.writeObject(data);
outGoing.flush();
System .out.println("sent to client");
}
if(data.getUserVerification().equals("Not Valid")){
dataList.remove(data);
}
} catch (IOException ex) {
removeOfflineStream(testOfflineStream);
System.out.println("Deleting Stream");
return;
}
}
/**
* Checking whether the Incoming Client name is not already taken
* The InComingData contains client name
* @param oCheckUser
*/
public void confirmUserName(UserData oCheckUser) {
verifyUserName = oCheckUser;
UserData check = new UserData();
UserData check1 = new UserData();
int numOfUsers = 0;
System.out.printf("%s" ,inStreamList);
try {
for (ObjectInputStream users : inStreamList)
{
System.out.println("146");
UserData check3 = (UserData) users.readObject();
System.out.println("149");
if (oCheckUser.getUserName().equals(check3.getUserName()))
{
numOfUsers++;
System.out.println("152");
}
}
} catch (Exception e) {
e.printStackTrace();
}
if(numOfUsers > 1){
try {
verifyUserName.setUserVerification("Not Valid");
System.out.println("line 126 " + verifyUserName);
sendData(verifyUserName);
} catch (InterruptedException ex) {
Logger.getLogger(ChatServer.class.getName()).log(Level.SEVERE, null, ex);
}
}else if (numOfUsers <= 1){
try {
for (ObjectInputStream users : inStreamList) {
check1 = (UserData) users.readObject();
verifyUserName.addUsersName(check1.getUserName());
verifyUserName.setUserVerification("Valid");
sendData(verifyUserName);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}}
/**
* Removing all offline streams in this method
* @param stream
*/
public void removeOfflineStream(ObjectOutputStream stream) {
ArrayList<ObjectOutputStream> offLineStream = new ArrayList<ObjectOutputStream>();
//Adding all offline clients to list
for (ObjectOutputStream clientStream : streamList) {
if (clientStream.equals(stream)) {
offLineStream.add(clientStream);
}
}
//removing clients from streamList
streamList.removeAll(offLineStream);
offLineStream.clear();
confirmUserName(verifyUserName);
return;
}
}
/**
* Main method
* @param args
*/
public static void main(String[] args) {
new ChatServer().makeConnection();
}
}
Thanks guys.
Edited by: 802041 on Nov 16, 2010 5:13 PM
Edited by: 802041 on Nov 16, 2010 5:13 PM