问题遇到的现象和发生背景
利用Java socket输出到浏览器
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
尝试了两种访问方式:
http://localhost:8080
http://127.0.0.1:8080/
都没有在浏览器上打印出想要的结果
我想要达到的结果
再浏览器上输出1234567
利用Java socket输出到浏览器
尝试了两种访问方式:
http://localhost:8080
http://127.0.0.1:8080/
都没有在浏览器上打印出想要的结果
再浏览器上输出1234567
代码执行完毕,它这个端口,会放开不?
package com.zhoufenqin.socket.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
public class Client {
public static final int port = 8080;
public static final String host = "localhost";
public static void main(String[] args) {
System.out.println("Client Start...");
while (true) {
Socket socket = null;
try {
//创建一个流套接字并将其连接到指定主机上的指定端口号
socket = new Socket(host,port);
//读取服务器端数据
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//向服务器端发送数据
PrintStream out = new PrintStream(socket.getOutputStream());
System.out.print("请输入: \t");
String str = new BufferedReader(new InputStreamReader(System.in)).readLine();
out.println(str);
String ret = input.readLine();
System.out.println("服务器端返回过来的是: " + ret);
// 如接收到 "OK" 则断开连接
if ("OK".equals(ret)) {
System.out.println("客户端将关闭连接");
Thread.sleep(500);
break;
}
out.close();
input.close();
} catch (Exception e) {
System.out.println("客户端异常:" + e.getMessage());
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
socket = null;
System.out.println("客户端 finally 异常:" + e.getMessage());
}
}
}
}
}
}