私は、複数のクライアントを受け入れるTCPチャットサーバの作成方法に関するチュートリアルを変更しています。私は最終的にクライアントクラスも作成しますが、これまでのところTELNETでテストしています。TCPチャットサーバ
サーバが入力を継続的にチェックして、キーワードを使用してサーバ機能をプルフォームできます。クライアントを切断する文字列 "EXIT"と "OK"を印刷する文字列 "Name:"を入力します。
これは私が考えていたものですが、それは動作しません。ここで
public void run()
{
String line;
try
{
while(true)
{
if (input.readline("EXIT"))//Should close and remove client
{
clients.remove(this);
users.remove(name);
break;
}
if(input.readline("Name:"))//Should print OK with username
{
System.out.println("OK");
}
boradcast(name,line); // method of outer class - send messages to all
}// end of while
} // try
catch(Exception e)
{
System.out.println(e.getMessage());
}
} // end of run()
}
}
は正確に何を知らなくても、サーバー全体のクラス
// Chat Server runs at port no. 9020
import java.io.*;
import java.util.*;
import java.net.*;
import static java.lang.System.out;
public class TCPServer
{
Vector<String> users = new Vector<String>();
Vector<HandleClient> clients = new Vector<HandleClient>();
int PORT = 9020;
int NumClients = 10;
public void process() throws Exception
{
ServerSocket server = new ServerSocket(PORT,NumClients);
out.println("Server Connected...");
while(true)
{
Socket client = server.accept();
HandleClient c = new HandleClient(client);
clients.add(c);
} // end of while
}
public static void main(String ... args) throws Exception
{
new TCPServer().process();
} // end of main
public void boradcast(String user, String message)
{
// send message to all connected users
for (HandleClient c : clients)
if (!c.getUserName().equals(user))
{
c.sendMessage(user,message);
}
}
class HandleClient extends Thread
{
String name = "";
BufferedReader input;
PrintWriter output;
public HandleClient(Socket client) throws Exception
{
// get input and output streams
input = new BufferedReader(new InputStreamReader(client.getInputStream())) ;
output = new PrintWriter (client.getOutputStream(),true);
output.println("Welcome to Bob's Chat Server!\n");
// read name
output.println("Please Enter a User Name: ");
name = input.readLine();
users.add(name); // add to vector
output.println("Welcome "+name+" we hope you enjoy your chat today");
start();
}
public void sendMessage(String uname,String msg)
{
output.println(uname + ":" + msg);
}
public String getUserName()
{
return name;
}
public void run()
{
String line;
try
{
while(true)
{
if (input.readline("EXIT"))
{
clients.remove(this);
users.remove(name);
break;
}
if(input.readline(name))
{
System.out.println("OK");
}
boradcast(name,line); // method of outer class - send messages to all
}// end of while
} // try
catch(Exception e)
{
System.out.println(e.getMessage());
}
} // end of run()
} // end of inner class
} // end of Server
「*動作しません」に拡張してください。コンパイルエラー?ランタイムエラー?予期しない機能ですか? – ziesemer
これはコンパイル時に得られるものです: TCPServer.java:79:シンボル シンボルを見つけることができません:メソッドreadline(java.lang.String ) 場所:クラスjava.io.BufferedReader \t \t(input.readline( "EXIT")であれば) \t \t^ TCPServer.java:85:メソッドのreadline(java.lang.Stringで):シンボル シンボルを見つけることができません 場所:クラスjava.io.BufferedReader \t \t if(input.readline( "Name:")) \t \t^ 2エラー – user1174834
"WebSockets"を使用してチャットサーバーを実行する必要があります。これが現代的なやり方です。 – djangofan