还是一个socket的问题,大人物们,谢谢了!

lewiha 2004-11-22 12:45:51
这是一个多客户端的服务器上的代码,我的问题是:为什么在给每个客户打开一个thread后
在thread里的run中为什么还有个死循环while(true),它有什么作用?但运行后,确不会一直
运行那个死循环?
见代码:(第二个while(true))
// MultiThreadsServer.java: The server can communicate with
// multiple clients concurrently using the multiple threads
import java.io.*;
import java.net.*;

public class MultiThreadServer
{
// Main method
public static void main(String[] args)
{
try
{
// Create a server socket
ServerSocket serverSocket = new ServerSocket(8000);

// To number a client
int clientNo = 1;
Socket connectToClient = serverSocket.accept();

while (true)
{
// Listen for a new connection request


// Print the new connect number on the console
System.out.println("Start thread for client " + clientNo);

// Find the client's hostname, and IP address
InetAddress clientInetAddress =
connectToClient.getInetAddress();
System.out.println("Client " + clientNo + "'s hostname is "
+ clientInetAddress.getHostName());
System.out.println("Client " + clientNo + "'s IP Address is "
+ clientInetAddress.getHostAddress());

// Create a new thread for the connection
HandleAClient thread = new HandleAClient(connectToClient, clientNo);

// Start the new thread
thread.start();

// Increment clientNo
clientNo++;
}
}
catch(IOException ex)
{
System.err.println(ex);
}
}
}

// Define the thread class for handling a new connection
class HandleAClient extends Thread
{
private Socket connectToClient; // A connected socket
private int clientNo; // Indicate client no

// Construct a thread
public HandleAClient(Socket socket, int clientNo)
{
connectToClient = socket;
this.clientNo = clientNo;
}

// Implement the run() method for the thread
public void run()
{
try
{
// Create data input and output streams
DataInputStream isFromClient = new DataInputStream(
connectToClient.getInputStream());
DataOutputStream osToClient = new DataOutputStream(
connectToClient.getOutputStream());

// Continuously serve the client
while (true) //就是这个,为什么要用?
{
// Receive radius from the client
double radius = isFromClient.readDouble();
System.out.println("radius received from client " +
clientNo + ": " + radius);

// Compute area
double area = radius*radius*Math.PI;

// Send area back to the client
osToClient.writeDouble(area);
System.out.println("Area found: " + area);
}
}
catch(IOException ex)
{
System.err.println(ex);
}
}
}
...全文
106 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Avampire 2004-11-22
  • 打赏
  • 举报
回复
Thread并不一定需要while循环,只有你的thread类的功能实现需要while的时候才需要 楼主的thread类并不需要
Avampire 2004-11-22
  • 打赏
  • 举报
回复
这句: Socket connectToClient = serverSocket.accept();应该放进while,否则不能保持监听;第二个while应该是不需要的,如果没有异常抛出的话是死循环。

chanceqw 2004-11-22
  • 打赏
  • 举报
回复
Thread一般都是一直运行的啊
while(true){}循环保证这个Thread一直运行
这个程序中,如果客户端断开链接,那么就会产生IOException从而终止这个线程
还有一种做法是用while(aFlag){}
在循环内可能改变aFlag从而终止线程
funcreal 2004-11-22
  • 打赏
  • 举报
回复
如果不相信的话,可以利用swing试一下。你把这个进程写到窗体的构建中,那么这个窗体是不会显示的,因为进程被阻塞了。
funcreal 2004-11-22
  • 打赏
  • 举报
回复
double radius = isFromClient.readDouble();一句会阻塞该进程。
也就是说它会等到客户端发过来一个对象以后,才会继续执行。这不是真正的死循环。它是有结束条件的。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧