public static void Listen() throws IOException, SQLException
{
//Creates a server socket to listen to the DutyTeacher
ServerSocket mySS = new ServerSocket(5001);
Socket ss_accept = mySS.accept();
BufferedReader ss_BF = new BufferedReader(new InputStreamReader(ss_accept.getInputStream()));
String Attending = ss_BF.readLine();
//Checks content of Duty Message
if(Attending.contentEquals("Attending"))
{
//If they leave the box blank, they are attending, open JoptionPane
JOptionPane.showMessageDialog(null, "The Duty Teacher is attending");
mySS.close();
ss_accept.close();
ss_BF.close();
System.exit(1);
}
//If the box is full a conversation is started, opens conversation box
if(Attending.contentEquals("Conversation"))
{
new ConversationTeacher().setVisible(true);
mySS.close();
ss_accept.close();
ss_BF.close();
ListenForMessages();
}
/*else
{
//If neither of the others, it is a new convo message, conversation updated
ConversationTeacher.UpdateConversation();
System.out.println("6");
mySS.close();
ss_accept.close();
ss_BF.close();
Listen();
}*/
}
public static void ListenForMessages() throws IOException, SQLException
{
//Creates a server socket to listen to the DutyTeacher
System.out.println("7");
ServerSocket mySS = new ServerSocket(5001);
System.out.println("8");
Socket ss_accept = mySS.accept();
System.out.println("9");
BufferedReader ss_BF = new BufferedReader(new InputStreamReader(ss_accept.getInputStream()));
System.out.println("10");
String Attending = ss_BF.readLine();
System.out.println("11");
//Checks content of Duty Message
//If neither of the others, it is a new convo message, conversation updated
ConversationTeacher.UpdateConversation();
System.out.println("6");
mySS.close();
ss_accept.close();
ss_BF.close();
ListenForMessages();
}
5 replies to this topic
#1
Posted 04 January 2011 - 12:42 PM
Hey guys, having an issue getting my server end to start listening again. The basics of it is that this program entails a system that allows the teacher (in a classroom) to communicate with the teacher on duty (who deals with incidents). The duty's server program runs fine and is pretty much identical which is where my confusion comes from. The basis of it is, is that when i run the listen program, and "Conversation" is read, the conversation box should open up. If i run Listen or ListenForMessages after this, the program fails, having appeared to reach up to the creation of Socket ss_accept, that is it prints 7 and 8 to the console. If i comment out and have no method running after this, the conversation box works fine as needed, but can only send one way as this computer is no longer listening. Hope that's clear enough!
|
|
|
#2
Posted 04 January 2011 - 01:42 PM
Could you show us how you send the data / how you create the client socket(the dutyTeacher's socket)?
I don't quite understand why you first make a serversocket on 5001, then close it and open it again in listenForMessages();
The dutyTeacher must then create his socket again to connect to it. I'm not sure if that's happening at the dutyTeacher's side.
And then you're in an infinite loop with listenForMessages();
the way you do that loop is different than
while(true){
listenForMessages();
}
Because with your code, if it "loops" too many times, you'll get a stackoverflowError.
I don't quite understand why you first make a serversocket on 5001, then close it and open it again in listenForMessages();
The dutyTeacher must then create his socket again to connect to it. I'm not sure if that's happening at the dutyTeacher's side.
And then you're in an infinite loop with listenForMessages();
the way you do that loop is different than
while(true){
listenForMessages();
}
Because with your code, if it "loops" too many times, you'll get a stackoverflowError.
#3
Posted 04 January 2011 - 01:53 PM
Here's all the code :)
package dutymanagerduty;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DutyServer {
public static void main(String[] args) throws IOException, SQLException{
Listen();
}
public static void ConfirmAttendance(String Host) throws UnknownHostException, IOException, SQLException
{
//Connect to teacher Pc port 5001
Socket MySkt = new Socket(Host, 5001);
PrintStream myPS = new PrintStream(MySkt.getOutputStream());
//Indicate that the teacher shall be attending the incident
myPS.println("Attending");
MySkt.close();
myPS.close();
}
public static void ConfirmNewMessage(String Host) throws UnknownHostException, IOException, SQLException
{
//Connect to teacher PC port 5001
Socket MySkt = new Socket(Host, 5001);
PrintStream myPS = new PrintStream(MySkt.getOutputStream());
//Incidate that a new message is being sent
myPS.println("Message");
MySkt.close();
myPS.close();
}
public static void Listen() throws IOException, SQLException
{
//Create a server port 5000
ServerSocket mySS = new ServerSocket(5000);
Socket ss_accept = mySS.accept();
BufferedReader ss_BF = new BufferedReader(new InputStreamReader(ss_accept.getInputStream()));
//Get the input sent to this port
String temp = ss_BF.readLine();
int ID;
ResultSet rs = null;
//This checks if the message is reporting a new incident
if(temp.substring(0,1).contentEquals("I"))
{
//Gets the incident ID by removing the I
ID = Integer.parseInt(temp.substring(1, temp.length()));
//Gets the required details by searching with the ID
rs = DutyDatabaseAccess.GetDetails(ID);
//Creates the duty response interface passing in details and ID.
new DutyResponseInterface(rs,ID).setVisible(true);
}else{
//If not a new incident, update the conversation box
ConversationDuty.UpdateConversation();
}
mySS.close();
ss_accept.close();
ss_BF.close();
Listen();
}
public static void InformOfConversationStart(String Host) throws UnknownHostException, IOException, SQLException
{
//Connect to teacher PC port 5001
Socket MySkt = new Socket(Host, 5001);
PrintStream myPS = new PrintStream(MySkt.getOutputStream());
//Indicate that a conversation is being started
myPS.println("Conversation");
MySkt.close();
myPS.close();
}
}
package dutymanager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class TeacherServer {
public static void SendIncidentToDuty(int ID) throws IOException, SQLException
{
//Sets socket to connect to port 5000 on the Desired PC
Socket MySkt = new Socket("hello-PC", 5000);
PrintStream myPS = new PrintStream(MySkt.getOutputStream());
//Marks this field as an incident with the I
String Id = String.valueOf("I" + ID);
//Sends the incident ID preceeded by a I
myPS.println(Id);
MySkt.close();
myPS.close();
Listen();
}
public static void Listen() throws IOException, SQLException
{
//Creates a server socket to listen to the DutyTeacher
System.out.println("1");
ServerSocket mySS = new ServerSocket(5001);
System.out.println("2");
Socket ss_accept = mySS.accept();
System.out.println("3");
BufferedReader ss_BF = new BufferedReader(new InputStreamReader(ss_accept.getInputStream()));
System.out.println("4");
String Attending = ss_BF.readLine();
//Checks content of Duty Message
if(Attending.contentEquals("Attending"))
{
//If they leave the box blank, they are attending, open JoptionPane
JOptionPane.showMessageDialog(null, "The Duty Teacher is attending");
mySS.close();
ss_accept.close();
ss_BF.close();
System.exit(1);
}
//If the box is full a conversation is started, opens conversation box
if(Attending.contentEquals("Conversation"))
{
new ConversationTeacher().setVisible(true);
ss_BF.close();
ss_accept.close();
mySS.close();
Listen();
}
if(Attending.contentEquals("Message"))
{
//If neither of the others, it is a new convo message, conversation updated
ConversationTeacher.UpdateConversation();
System.out.println("6");
mySS.close();
ss_accept.close();
ss_BF.close();
Listen();
}
}
public static void ConfirmNewMessage() throws UnknownHostException, IOException, SQLException
{
//Connects to Duty computer port 5000
Socket MySkt = new Socket("hello-PC", 5000);
PrintStream myPS = new PrintStream(MySkt.getOutputStream());
//indicates that it is alerting a message, hence update conversation
myPS.println("Message");
MySkt.close();
myPS.close();
}
}
#4
Posted 04 January 2011 - 01:59 PM
Hey sorry just noticed i'd changed some of the code the 2nd time round. Listen for messages is now incorporated into listen but as an if statement for if the string sent = message
#5
Posted 04 January 2011 - 02:15 PM
Hey,
Just doing some testing again, and it appears that something else must be the issue, as the console prints out things from my ConversationTeacherGUI when the 'send' button is clicked by the duty teacher. E.g the message is getting through so the listening isn't the problem. The gui just crashes (frame shows but is blank inside). It stopped at the point where a new socket needed to be created but as it appears to still be listening, maybe this isn't the problem?
Just doing some testing again, and it appears that something else must be the issue, as the console prints out things from my ConversationTeacherGUI when the 'send' button is clicked by the duty teacher. E.g the message is getting through so the listening isn't the problem. The gui just crashes (frame shows but is blank inside). It stopped at the point where a new socket needed to be created but as it appears to still be listening, maybe this isn't the problem?
#6
Posted 04 January 2011 - 02:38 PM
Try to start a new thread in the actionperformed or other actionlisteners on the gui:
so if you have something like:
wrap a thread around the code in the actionperformed
This should prevent Gui lockups that occur after pressing a button or anything.
(code may be slightly wrong, didn't compile or test it here)
so if you have something like:
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
stuffYouDo();
}
});
wrap a thread around the code in the actionperformed
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
(new Thread(new Runnable(){
public void run(){
stuffYouDo();
}
}) ).start();
}
});
This should prevent Gui lockups that occur after pressing a button or anything.
(code may be slightly wrong, didn't compile or test it here)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









