学生小白一枚,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);
}
}
}
}