Java多个客户端同时连接服务端在现实生活中用得比较多同时执行多项任务第一想到的当然是多线程了下面用多线程来实现并发多连接
import javanet*;
import javaio*;
public class ThreadServer extends Thread
{
private Socket client;
public ThreadServer(Socket c)
{
thisclient=c;
}
public void run()
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(clientgetInputStream()));
PrintWriter out=new PrintWriter(clientgetOutputStream());
//Mutil User but cant parallel
while (true)
{
String str=inreadLine();
Systemoutprintln(str);
outprintln(has receive);
outflush();
if (strequals(end))
break;
}
clientclose();
}
catch (IOException ex)
{
}
finally
{
}
}
public static void main(String[] args)throws IOException
{
ServerSocket server=new ServerSocket();
while (true)
{
//transfer location change Single User or Multi User
ThreadServer mu=new ThreadServer(serveraccept());
mustart();
}
}
}