java

位置:IT落伍者 >> java >> 浏览文章

Java多线程Socket操作猜数游戏样例[2]


发布日期:2021年01月06日
 
Java多线程Socket操作猜数游戏样例[2]

客户端程序发送每次猜的数字

package test;

import javaio*;

import javanet*;

public class ClientApp {

private Socket socket;

private BufferedReader netIn;

private PrintWriter netOut;

private BufferedReader keyboardIn;

xxxx

static Boolean finished = false;

public ClientApp() throws IOException {

Systemoutprintln(请输入服务器地址连接本地服务器请输入localhost);

keyboardIn = new BufferedReader(new InputStreamReader(Systemin));

try {

if (keyboardInreadLine()equalsIgnoreCase(localhost)) {

socket = new Socket(InetAddressgetLocalHost() ServerAppPORT);

} else {

socket = new Socket(InetAddressgetByName(keyboardInreadLine()) ServerAppPORT);

}

netIn = new BufferedReader(new InputStreamReader(socketgetInputStream()));

xxxx

netOut = new PrintWriter(socketgetOutputStream());

} catch (UnknownHostException e) {

Systemerrprintln(连接不到服务器);

Systemexit();

}

Systemoutprintln(连接成功);

while (!finished) {

Systemoutprintln(请输入之间的数字);

netOutprintln(keyboardInreadLine());

netOutflush();

String str = netInreadLine();

if (str == null) {

finished = true;

xxxx

break;

}

Systemoutprintln(str);

if (strstartsWith(OK)) {

finished = true;

break;

}

}

netInclose();

netOutclose();

keyboardInclose();

socketclose();

}

public static void main(String[] args) throws IOException {

new ClientApp(); xxxx

}

}

[] []

               

上一篇:Java程序开发中如何应用线程[1]

下一篇:Java多线程Socket操作猜数游戏样例[1]