socket编程界面不响应的问题
我做的是一个图形界面,用了socket建立了client和server,点击发送就会执行发送功能。可是每次一点击发送,图形界面就出现问题不能正常工作,不能移动,不能正常关闭了。这是为什么呢?可以的话帮我修改一下程序 谢谢了!!!button触发事件:
button_1.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Client.client(text_1.getText());
}
});
Client程序:
import
import
import
import
import
import
public class Client {
public static void client(String string) {
Socket socket = null;
int serverPort=Integer.parseInt(string); //将main函数中的string变为整型
try {
socket = new Socket("127.0.0.1",serverPort); // 将IP赋给Socket的端口号
//向服务器端第一次发送字符串
OutputStream netOut = socket.getOutputStream();
DataOutputStream doc = new DataOutputStream(netOut);
DataInputStream in = new DataInputStream(socket.getInputStream());
//向服务器端第二次发送字符串
doc.writeUTF(NodeB.sendRandomb()); //调用NodeB中接收到的随机数
String res = in.readUTF();
System.out.println(res);
in.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
}
}
sever程序:
import
import
import
import
import
import
import
public class Server {
/**
* @param args
* @throws IOException
*/
File f = new File("H:\\eclipse\\workspace\\Certification","randomb.txt");// 新建一个存储数据的TXT文件
public static void main(String[] args)
{
Server manager = new Server();
manager.doListen();
}
public void doListen()
{
ServerSocket server;
try {
server = new ServerSocket(9999);
while (true)
{
Socket client = server.accept();
new Thread(new SSocket(client)).start();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
//服务器进程
public static class SSocket implements Runnable
{
Socket client;
public SSocket(Socket client)
{
this.client = client;
}
public static String listMsg;
private boolean stop = false;
public static String getListMsg(){
return listMsg;
}
public void setListMsg(String listMsg2){
SSocket.listMsg = listMsg2;
}
public boolean isStop(){
return stop;
}
public void run()
{
DataInputStream input;
DataOutputStream output;
try {
input = new DataInputStream(client.getInputStream());
output = new DataOutputStream(client.getOutputStream());
FileWriter randomb=new FileWriter("randomb.txt");
listMsg = input.readUTF();
randomb.write(listMsg );
stop=true;
randomb.flush();
randomb.close();
} catch (IOException e)
{
e.printStackTrace();
}
System.out.println(listMsg+"Server");
}
}
}