我正在通过tcp进行聊天室连接。
以下代码是我在线程“main”java.lang.IllegalThreadStateException
中获得异常的代码,我无法理解为什么。
在类的构造函数中,我调用start()方法给出错误。
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class TCPServer1 {
private static int idConn; //id da coneccao
public ArrayList<UserThread> al; // an ArrayList to keep the list of the Client
private SimpleDateFormat sdf;// to System.out.println time
private int porto;//porto tcp para comunicar
private boolean isOn;// switch on/off para o servidor
private boolean isFirst; //para saber qual servidor tcp esta up
TCPServer1(int porto){
this.porto = porto;
al = new ArrayList<UserThread>();
isFirst = true;
start();
}
private void start(){
isOn = true;
try{
UDPServer udpServer = new UDPServer(7000);
ServerSocket serverSocket = new ServerSocket(porto);
while(isOn){
System.out.println("TCP Waiting on port "+porto+"...");
Socket socket = serverSocket.accept(); //aceita a coneccao
if(!isOn)
break;
UserThread ut = new UserThread(socket);
al.add(ut);
ut.start();
}
//fecha a coneccao
try{
serverSocket.close();
for (int i=0;i<al.size() ;i++ ) {
UserThread ut = al.get(i);
try{
ut.in.close();
ut.out.close();
ut.socket.close();
}
catch(IOException e){e.printStackTrace();}
}
}catch(Exception ex){ex.printStackTrace();}
}catch(IOException e){e.printStackTrace();}
}
synchronized void remove(int id){
for (int i=0;i<al.size() ;i++ ) {
UserThread ut = al.get(i);
if(ut.id == id){
al.remove(i);
return;
}
}
}
private synchronized void sendToAll(String msg){
System.out.println(msg);
for (int i = al.size();--i >=0 ; ) {
UserThread ut = al.get(i);
if(!ut.writeMsg(msg)){
al.remove(i);
System.out.println("Removed " + ut.username);
}
}
}
public static void main(String[] args) {
//default -> 2000, pode ser especificado outro via terminal
int porto = 2000;
switch(args.length) {
case 1:
try {
porto = Integer.parseInt(args[0]);
}
catch(Exception e) {
System.out.println("Invalid port number.");
return;
}
case 0:
break;
default:
System.out.println("Using default port");
return;
}
TCPServer1 server = new TCPServer1(porto);
}
class UserThread extends Thread {
Socket socket;
ObjectInputStream in;
ObjectOutputStream out;
// my unique id (easier for deconnection)
int id;
// the Username of the Client
String username;
// the only type of message a will receive
Message cm;
// the date I connect
String date;
// Constructore
UserThread(Socket socket) {
// a unique id
id = ++idConn;
this.socket = socket;
try
{
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
// read the username
username = (String) in.readObject();
System.out.println(username + " just connected.");
}
catch (IOException e) {
System.out.println("Exception creating new Input/output Streams: " + e);
return;
}
// have to catch ClassNotFoundException
// but I read a String, I am sure it will work
catch (ClassNotFoundException e) {
}
}
// what will run forever
public void run() {
// to loop until LOGOUT
boolean isOn = true;
while(isOn) {
// read a String (which is an object)
try {
cm = (Message) in.readObject();
}
catch (IOException e) {
System.out.println(username + " Exception reading Streams: " + e);
break;
}
catch(ClassNotFoundException e2) {
break;
}
// the messaage part of the Message
String message = cm.getMessage();
// Switch on the type of message receive
switch(cm.getType()) {
case Message.msg:
sendToAll(username + ": " + message);
break;
case Message.logout:
System.out.println(username + " disconnected with a LOGOUT message.");
isOn = false;
break;
case Message.users:
writeMsg("List of the users connected at " + sdf.format(new Date()) + "\n");
// scan al the users connected
for(int i = 0; i < al.size(); ++i) {
UserThread ct = al.get(i);
writeMsg((i+1) + ") " + ct.username + " since " + ct.date);
}
break;
}
}
// remove myself from the arrayList containing the list of the
// connected Clients
remove(id);
close();
}
// try to close everything
private void close() {
// try to close the connection
try {
if(out != null) out.close();
}
catch(Exception e) {}
try {
if(in != null) in.close();
}
catch(Exception e) {};
try {
if(socket != null) socket.close();
}
catch (Exception e) {}
}
/*
* Write a String to the Client output stream
*/
private boolean writeMsg(String msg) {
// if Client is still connected send the message to it
if(!socket.isConnected()) {
close();
return false;
}
// write the message to the stream
try {
out.writeObject(msg);
}
// if an error occurs, do not abort just inform the user
catch(IOException e) {
System.out.println("Error sending message to " + username);
System.out.println(e.toString());
}
return true;
}
}
}
希望得到你的帮助,我已经搜索过类似的问题,但这些问题的解决方案似乎没什么帮助。