Tyron_Zayne 2017-03-19 12:38 采纳率: 0%
浏览 1249
已结题

关于用Java GUI 做一个TCPClient&server请求响应,输出结果却无限循环的问题

学生小白一枚,TCPclient里输入文字后,不知道为何一直循环。

 import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class TCPserverGUI extends Frame implements ActionListener{

    private static ServerSocket servSock;
    private static final int PORT = 9652;
    private Button btnStartServer;
    private static TextArea tareceiver;



    public TCPserverGUI(){

        setTitle("TCP Server");
        setLayout(new FlowLayout());

        btnStartServer =new Button("Start");
        add(btnStartServer);

        btnStartServer.addActionListener(this);

        tareceiver = new TextArea();
        tareceiver.setEditable(false);
        add(tareceiver);
    }

    public static void main(String[] args){
        TCPserverGUI Frame = new TCPserverGUI();
        Frame.setSize(400,300);
        Frame.setVisible(true);
        Frame.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent evt) {
                  System.exit(0);
            }
        }
        );      
    }

    public void actionPerformed(ActionEvent e){
        tareceiver.setText("Opening port...\n");
        try
        {
            servSock = new ServerSocket(PORT); //Step 1.
        }
        catch(IOException ioEx)
        {
            tareceiver.setText("Unable to attach to port!");
            System.exit(1);
        }
        do
        {
            handleClient();
        }while (true);
    }

    private static void handleClient(){
        Socket link = null; //Step 2.
        try
        {
            link = servSock.accept(); //Step 2.
            Scanner input = new Scanner(link.getInputStream());//Step 3.
            PrintWriter output = new PrintWriter( link.getOutputStream(),true); //Step 3.
            int numMessages = 0;
            String message = input.nextLine(); //Step 4.
            while (!message.equals("***CLOSE***"))
            {
                tareceiver.setText("Message received from " + InetAddress.getLocalHost().toString() + ":"+message.toString());
                numMessages++;
                output.println("Message " + numMessages + ": " + message); //Step 4.
                message = input.nextLine();
            }
            output.println(numMessages + " messages received.");//Step 4.
        }
        catch(IOException ioEx)
        {
            ioEx.printStackTrace();
        }
        finally
        {
            try
            {
                tareceiver.setText("\n* Closing connection... *");
                link.close(); //Step 5.
            }
            catch(IOException ioEx)
            {
                tareceiver.setText("Unable to disconnect!");
                System.exit(1);
            }
        }
    }
}
 import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class TCPclientGUI extends Frame implements ActionListener{

    private static InetAddress host;
    private static final int PORT = 9652;

    private Label lblEnterMessage;
    private static TextField tfmessage;
    private static TextArea taresult;
    private Button btnStart;
    private Thread thread1;

    public TCPclientGUI(){

        setLayout(new BorderLayout(3,3));
        lblEnterMessage = new Label("Enter Message");
        add(lblEnterMessage, BorderLayout.WEST);

        tfmessage = new TextField(50);
        tfmessage.setEditable(true);
        add(tfmessage,BorderLayout.NORTH);

        taresult = new TextArea(50,100);
        taresult.setEditable(false);
        add(taresult,BorderLayout.CENTER);

        btnStart = new Button("Send");
        add(btnStart,BorderLayout.SOUTH);

        btnStart.addActionListener(this);
    }

    public static void main(String[] args){
        TCPclientGUI Frame = new TCPclientGUI();
        Frame.setSize(400,300);
        Frame.setVisible(true);
        Frame.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent evt) {
                  System.exit(0);
            }
        }
        );
        Thread thread1 =new Thread(){
            public void run(){

            }
        };
        thread1.start();
    }


    public void actionPerformed(ActionEvent e){
        try
        {
            host = InetAddress.getLocalHost();
        }
        catch(UnknownHostException uhEx)
        {
            taresult.setText("Host ID not found!");
            System.exit(1);
        }
        accessServer();

    }

    private static void accessServer(){
        Socket link = null; //Step 1.
        try
        {
            link = new Socket(host,PORT); //Step 1.
            Scanner input = new Scanner(link.getInputStream()); //Step 2.
            PrintWriter output = new PrintWriter(link.getOutputStream(),true); //Step 2.
            //Set up stream for keyboard entry...
            String userEntry = tfmessage.getText();

            String message = null , response;
            do
            {
                System.out.print("");
                message = userEntry;
                output.println(message); //Step 3.
                response = input.nextLine(); //Step 3.
                taresult.append("\nSERVER> " + response);

            }while (!message.equals("***CLOSE***"));
        }
        catch(IOException ioEx)
        {
            ioEx.printStackTrace();
        }
        finally
        {
            try
            {
                taresult.setText("\n* Closing connection... *");
                link.close(); //Step 4.
            }
            catch(IOException ioEx)
            {
                taresult.setText("Unable to disconnect!");
                System.exit(1);
            }
        }
    }

}
  • 写回答

1条回答 默认 最新

  • Tealar 2017-03-20 05:55
    关注

    图片说明你这里搞了一个死循环,进去就出不来了吧

    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)