我有一个TCP客户端,它将数据发送到我的TCP服务器。 TCP服务器中的ServerSocket始终在while(true)的无限循环中等待来自客户端的传入请求。我想始终根据客户端的请求更新文本字段,我的serversocket位于客户端数据所在的while(true)中。如何实现呢?
//我当前代码的摘要
Text txtOne;
public static void main(String[] args)
{
window.open();
}
void open()
{
createContents();
}
createcontents()
{
txtOne = new Text(shell, SWT.BORDER);
Button btnStartServer = new Button(shell, SWT.NONE);
btnStartServer.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
toServerController();
}
});
}
void toServerCOntroller()
{
try
{
ServerSocket server = new ServerSocket(60000);
while (true)
{
Socket sclient = server.accept();
DataInputStream dataIn = new DataInputStream(sclient.getInputStream());
DataOutputStream dataOut = new DataOutputStream(sclient.getOutputStream());
String name = dataIn.readUTF();
txtOne.setText(name); //<----- this is where the error happen
}
}
我希望txtOne始终会根据dataIn进行更改。