public static void main(String[] args) throws IOException, InterruptedException{
//TCP component
int port = 8081;
String host = "localhost";
Socket nisSocket = null;
PrintWriter out = null;
BufferedReader in = null;
TCPClient t = new TCPClient();
try {
nisSocket = new Socket(host, port);
out = new PrintWriter(nisSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(nisSocket.getInputStream()));
} catch (UnknownHostException e) {
System.out.println(e.toString() + " :Don't know about " + host);
System.exit(1);
} catch (IOException e) {
System.out.println(e.toString() + " :Couldn't get I/O for the connection to " + host);
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer, fromUser;
while ((fromServer = in.readLine()) != null) {
System.out.println("Server: " + fromServer);
if (fromServer.equalsIgnoreCase("Don't miss me! Bye Bye!"))
break;
if (fromServer.equalsIgnoreCase("Thank For Your Guessing. Have a Nice Day =)"))
break;
while (true) {
synchronized (t.lock) {
try {
t.lock.wait();
TCPProtocol abc = new TCPProtocol();
System.out.println("try again");
fromUser = abc.Jusertxt.getText().toString();
System.out.println("\nClient: " + fromUser);
out.println(fromUser);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
System.out.println("try again");
//End connection
out.flush();
out.close();
in.close();
stdIn.close();
//if (!nisSocket.isClosed())
nisSocket.close();
System.out.println("TCP session closed.");
}
public String pullText(){
TCPProtocol pro = new TCPProtocol();
String result = pro.Jusertxt.getText().toString();
pro.Jusertxt.setText("");
return result;
}
}
复制代码
TCPProtocol
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;
public class TCPProtocol extends JFrame{
public JFrame frame;
public JTextArea Jservertxt, Jusertxt;
public JLabel label;
public String usertxt, servertxt;
public JButton Jsend;
private JScrollPane scl, Uscl;
private boolean getPressed =false;
String init = "Hi, i am your lame joker. Are you ready to play now?<Y/N>";
String correct ="Great Job! You are Correct!";
String[] riddle = {"What do you call a fake noodle?",
"why did the turtle cross the road?",
"Why shouldn't you believe a man in bed?",
"Which travels faster, heat or cold?",
"Why do UKM Students feel stronger on Saturday and Sunday?",
"What do you call a sleeping bull?",
"They come out at night without being called, and are lost in the day without being stolen. What are they?",
"What comes down but never goes up?",
"What has one eye but cannot see?",
"What kind of tree can you carry in your hand?",
"How do you make the number one disappear?",
"What goes up but never comes down?",
"We see it once in a year, twice in a week, and never in a day. What is it?",
"How many months have 28 days?",
"If a blue house is made out of blue bricks, a yellow house is made out of yellow bricks and a pink house is made out of pink bricks, what is a green house made of?",
"What goes up a chimney down but can? come down a chimney up?",
"What is at the end of a rainbow?",
"What can you catch but not throw?",
"What is as light as a feather, but even the world? strongest man couldn? hold it for more than a minute?"};
String[] answer = {"Impasta",
"shell station",
"lying",
"cold",
"week day",
"bulldozer",
"stars",
"rain",
"needle",
"palm",
"letter",
"your ",
"e",
"all",
"glass",
"umbrella",
"letter w",
"cold",
"breath"};
String [] actualAnswer = {"Impasta",
"To get to the shell station.",
"Because he is lying.",
"Heat. Because you can catch 'cold' easily.",
"Because all the other days are week (weak) days.",
"A bulldozer!",
"S (Stars)",
"R (Rain)",
"N (A needle)",
"P (A palm!)",
"L (Add the letter G and it? GONE)",
"Y (Your age!) ",
"E (The letter ??",
"M (All 12 months!)",
"G (Glass)",
"U (An umbrella)",
"W (The letter W!)",
"C (A cold)",
"B (His breath!)"};
String next = "Ready For Next Question? <Y/N>";
//String next = "Do you have exercise often?";
//String isOk = "Is ok. Give It A Try Next Time. Have a Nice Day =)";
String isOk = "Don't miss me! Bye Bye!";
//String bye = "Thank For Your Guessing. Have a Nice Day =)";
String bye="I Wont't Deactivated Until You Say Are Happy With My Joke.";
String again = "Play Again? <Y/N>";
int INITIAL = 0;
int STEP1 = 1; //asking question part
int STEP2 = 2; //confirm answer part
int question = 0;
int noOfQuestionAnswered = 0;
int state = INITIAL;
String comp;
public String processAnswer(String input)
{
String output = null;
if (state == STEP1) //asking question part
{
if (input.equalsIgnoreCase("y"))
{
output = riddle[question];
state = STEP2;
}
else
{
if(input.indexOf("Satisfied") != -1)
{
if (noOfQuestionAnswered == 0)
output = isOk;
else
output = bye;
}
else
{
output = isOk;
}
}
}
else if (state == STEP2) // confirm answer part
{
if (input.indexOf(answer[question]) != -1)
{
output = correct +next;
state = STEP1;
}
else
{
output = "Oh no, you answer wrongly ! "+ "Correct Answer: " + actualAnswer[question]+ " . " + again;
state = STEP1;
}
question = (question + 1) % 19;
noOfQuestionAnswered++;
}
else if (state == INITIAL)
{
output = init;//"Learn Learn English. Are you ready to play now?<Y?N>(try to get Y/N)"
private Object lock = new Object(); // added lock for wait/notify coordination
private String Data ; //changed scope to make it accessible in getMessage()
String init = "Hi, i am your lame joker. Are you ready to play now?<Y/N>";
String correct ="Great Job! You are Correct!";
String[] riddle = {"What do you call a fake noodle?",
"why did the turtle cross the road?",
"Why shouldn't you believe a man in bed?",
"Which travels faster, heat or cold?",
"Why do UKM Students feel stronger on Saturday and Sunday?",
"What do you call a sleeping bull?",
"They come out at night without being called, and are lost in the day without being stolen. What are they?",
"What comes down but never goes up?",
"What has one eye but cannot see?",
"What kind of tree can you carry in your hand?",
"How do you make the number one disappear?",
"What goes up but never comes down?",
"We see it once in a year, twice in a week, and never in a day. What is it?",
"How many months have 28 days?",
"If a blue house is made out of blue bricks, a yellow house is made out of yellow bricks and a pink house is made out of pink bricks, what is a green house made of?",
"What goes up a chimney down but can? come down a chimney up?",
"What is at the end of a rainbow?",
"What can you catch but not throw?",
"What is as light as a feather, but even the world? strongest man couldn? hold it for more than a minute?"};
String[] answer = {"Impasta",
"shell station",
"lying",
"cold",
"week day",
"bulldozer",
"stars",
"rain",
"needle",
"palm",
"letter",
"your ",
"e",
"all",
"glass",
"umbrella",
"letter w",
"cold",
"breath"};
String [] actualAnswer = {"Impasta",
"To get to the shell station.",
"Because he is lying.",
"Heat. Because you can catch 'cold' easily.",
"Because all the other days are week (weak) days.",
"A bulldozer!",
"S (Stars)",
"R (Rain)",
"N (A needle)",
"P (A palm!)",
"L (Add the letter G and it? GONE)",
"Y (Your age!) ",
"E (The letter ??",
"M (All 12 months!)",
"G (Glass)",
"U (An umbrella)",
"W (The letter W!)",
"C (A cold)",
"B (His breath!)"};
String next = "Ready For Next Question? <Y/N>";
//String next = "Do you have exercise often?";
//String isOk = "Is ok. Give It A Try Next Time. Have a Nice Day =)";
String isOk = "Don't miss me! Bye Bye!";
//String bye = "Thank For Your Guessing. Have a Nice Day =)";
String bye="I Wont't Deactivated Until You Say Are Happy With My Joke.";
String again = "Play Again? <Y/N>";
int INITIAL = 0;
int STEP1 = 1; //asking question part
int STEP2 = 2; //confirm answer part
int question = 0;
int noOfQuestionAnswered = 0;
int state = INITIAL;
String comp;
public String processAnswer(String input)
{
String output = null;
if (state == STEP1) //asking question part
{
if (input.equalsIgnoreCase("y"))
{
output = riddle[question];
state = STEP2;
}
else
{
if(input.indexOf("Satisfied") != -1)
{
if (noOfQuestionAnswered == 0)
output = isOk;
else
output = bye;
}
else
{
output = isOk;
}
}
}
else if (state == STEP2) // confirm answer part
{
if (input.indexOf(answer[question]) != -1)
{
output = correct +next;
state = STEP1;
}
else
{
output = "Oh no, you answer wrongly ! "+ "Correct Answer: " + actualAnswer[question]+ " . " + again;
state = STEP1;
}
question = (question + 1) % 19;
noOfQuestionAnswered++;
}
else if (state == INITIAL)
{
output = init;//"Learn Learn English. Are you ready to play now?<Y?N>(try to get Y/N)"
虽然做到了是好事,但是还是搞清楚怎么回事,it work but I don't know why是大忌
你的 tcpprotocol object 也就是jframe 在client 和 server 各有一个。你update 了client 的object 却显示 server 的object。这大概就是其中的原因.