Chat Server-client を使用して割り当てを行っています。サーバーを起動する方法は次のとおりです
public static void StartServer(){
// Create socket
try {
serversocket = new ServerSocket(ServerPort);
} catch (Exception e) {
isError = true;
ERRORCODE = "ERROR! Cannot create a new socket! " + e.getMessage();
return;
}
// A new thread to wait for connection
Thread TH_Wait4Connection = new Thread(){
public void run(){
while(true){
String ERRORHere = "-1"; // To specify whre the Errors are
try {
Connection = new Socket();
Connection = serversocket.accept();
} catch (Exception e) {
ERRORCODE = ERRORHere + " : " + e.getMessage();
return;
}
// Another Thread to handle a connection
try {
ERRORHere = "1";
Thread Client = new Thread(new ConnHandler(Connection));
ERRORHere = "2";
threadList.add(Client);
ERRORHere = "3";
Client.start();
ERRORHere = "4";
} catch (Exception e) {
ERRORCODE = ERRORHere + " : " + e.getMessage();
return;
}
try {Thread.sleep(10);} catch (Exception e) {}
} // End why loop
} // End run()
};
TH_Wait4Connection.start();
}
私がEclipseでデバッグすると、クライアントはサーバーに接続でき、すべて問題なく、サーバーはスレッドを作成し、例外はキャッチされません。しかし、実行すると、最後のキャッチとエラーコードに入ります
ERRORCODE = ERRORHere + " : " + e.getMessage();
は
1 : 6 > 4
それらのエラーは何ですか?そして、それを修正する方法は?
読んでくれてありがとう。
クラス ConnHandler を更新する
public class ConnHandler implements Runnable{
public ConnHandler(Socket Connection) throws Exception{
InputStream IS = Connection.getInputStream();
byte[] InData = new byte[1024];
int bytesCount = IS.read(InData);
// Remove first 6 bytes
byte[] NewInData = Arrays.copyOfRange(InData, 6, bytesCount);
}
public void run(){}
}