java网络编程,这里出现什么问题,怎么客户端跟服务器连接不上,而且也不会输出
服务器:packagecom.j0615;importjava.io.BufferedReader;importjava.io.IOException;importjav...
服务器:
package com.j0615;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
// 创建服务器
ServerSocket server;
try {
server = new ServerSocket(3444);
System.out.println("-------服务器启动成功-------");
Socket s = server.accept();// 接收客户端的连接
BufferedReader dis = new BufferedReader(new InputStreamReader(
s.getInputStream(), "UTF-8"));
dis.readLine();
PrintWriter ou = new PrintWriter(s.getOutputStream());
ou.write("----------连接成功--------");
ou.flush();
ou.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端:
package com.j0615;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class socket {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1", 3444);
PrintWriter out = new PrintWriter(s.getOutputStream());
out.write("你好!");
BufferedReader in = new BufferedReader(new InputStreamReader(
s.getInputStream(), "UTF-8"));
in.readLine();
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
结果服务器控制台输出:-------服务器启动成功-------
客户端控制台输出空白:
请问各位大神怎么回事,哪里出错了???代码没报错, 展开
package com.j0615;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
// 创建服务器
ServerSocket server;
try {
server = new ServerSocket(3444);
System.out.println("-------服务器启动成功-------");
Socket s = server.accept();// 接收客户端的连接
BufferedReader dis = new BufferedReader(new InputStreamReader(
s.getInputStream(), "UTF-8"));
dis.readLine();
PrintWriter ou = new PrintWriter(s.getOutputStream());
ou.write("----------连接成功--------");
ou.flush();
ou.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端:
package com.j0615;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class socket {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1", 3444);
PrintWriter out = new PrintWriter(s.getOutputStream());
out.write("你好!");
BufferedReader in = new BufferedReader(new InputStreamReader(
s.getInputStream(), "UTF-8"));
in.readLine();
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
结果服务器控制台输出:-------服务器启动成功-------
客户端控制台输出空白:
请问各位大神怎么回事,哪里出错了???代码没报错, 展开
1个回答
展开全部
PrintWriter out = new PrintWriter(s.getOutputStream());
out.write("你好!");
替换成
PrintStream out = new PrintStream(s.getOutputStream());
out.print("你好!");
out.write("你好!");
替换成
PrintStream out = new PrintStream(s.getOutputStream());
out.print("你好!");
更多追问追答
追问
试过了 ,也不行
追答
还有一种简单改法,你要在write之后,调用flush。就是在你的client的out.write("你好!”)的后面out.flush(),不然你发送给服务器的时候,client因为没有调用flush,会一直阻塞在那。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询