如何创建基本的Java服务器

如何创建基本的Java服务器,第1张

以下是Sun提供的一个简单的“Knock Knock"”服务器
import javanet;import javaio;public class KnockKnockServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
Systemerrprintln("Could not listen on port: 4444");
Systemexit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocketaccept();
} catch (IOException e) {
Systemerrprintln("Accept failed");
Systemexit(1);
}
PrintWriter out = new PrintWriter(clientSocketgetOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocketgetInputStream()));
String inputLine, outputLine;
KnockKnockProtocol kkp = new KnockKnockProtocol();
outputLine = kkpprocessInput(null);
outprintln(outputLine);
while ((inputLine = inreadLine()) != null) {
outputLine = kkpprocessInput(inputLine);
outprintln(outputLine);
if (outputLineequals("Bye"))
break;
}
outclose();
inclose();
clientSocketclose();
serverSocketclose();
}}
再简单不过了


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10863256.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-11
下一篇 2023-05-11

发表评论

登录后才能评论

评论列表(0条)

保存