资源简介

课程设计作业,java版,导入eclipse中运行,运行StartFrame,或者分别运行PlayChess1,PlayChess2

资源截图

代码片段和文件信息

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import javax.swing.JOptionPane;

public class Client {
static String ipText = ““;
Socket sock;
PrintWriter writer;
BufferedReader reader;
String clientMessage = PlayChess1.message;

void connect() {
try {
ipText = JOptionPane.showInputDialog(“输入对方的IP地址:“ “127.0.0.1“);
sock = new Socket(ipText 4242);
if (sock.isConnected()) {
JOptionPane.showMessageDialog(null “连接成功“);
InputStreamReader streamReader = new InputStreamReader(
sock.getInputStream());
reader = new BufferedReader(streamReader);
writer = new PrintWriter(sock.getOutputStream());
}
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null “连接失败,请检查IP地址“);
}
}

Thread outThread = new Thread(new Runnable() {
public void run() {
while (true) {
if (PlayChess1.message != null) {
clientMessage = PlayChess1.message;
writer.println(clientMessage);
writer.flush();
PlayChess1.message = null;
}
}
}

});

/*
 * Thread inThread = new Thread(new Runnable() { public void run() { try {
 * while (true) { serverMsg = reader.readLine();
 * //System.out.println(serverMsg); //PlayChess1.judge(); } } catch
 * (IOException e) { e.printStackTrace();
 * System.out.println(“inThread线程出错啦“); } } });
 */

}

评论

共有 条评论