我按照视频中一模一样打的代码为什么服务器端运行了,客户端运行时什么东西都没有输出
客户端代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.imageio.stream.ImageInputStream;
public class Cilent {
public static void main(String[] args) {
//1.创建客户端SOcket,指定服务器额端口
try {
Socket socket=new Socket("localhost",8888);
//2/获取输出流,向服务器发送信息
OutputStream os=socket.getOutputStream();
PrintWriter pw=new PrintWriter(os);
pw.write("用户名:admin;密码:123");
pw.flush();
socket.shutdownOutput();
//3.获取输入流
InputStream ins=socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(ins));
String info=null ;
while((info=br.readLine())!=null){
System.out.println("我是客户端,服务器说:"+info);
info=br.readLine();
}
//3.关闭资源
br.close();
ins.close();
pw.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
客户端的报错
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at Socket.Cilent.main(Cilent.java:19)
服务端的代码
package Socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/*
* 给予TCP协议的端
*/
public class Sever {
public static void main(String[] args){
//1.创建一个服务器
try {
ServerSocket scoket1=new ServerSocket(8888);
//2.调用accept方法
System.out.println("服务器即将启动,等待客户端的链接***");
Socket socket=scoket1.accept();
//3.获取输入流,获取登录信息
InputStream is=socket.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String info=null ;
System.out.println("开始连接");
while((info=br.readLine())!=null){
System.out.println("我服务器,客户端说:"+info);
info=br.readLine();
}
socket.shutdownInput();
//4.获取输出流
OutputStream os=socket.getOutputStream();
PrintWriter pw=new PrintWriter(os);
pw.write("欢迎你!");
pw.flush();
//5.关闭资源
pw.close();
os.close();
isr.close();
is.close();
br.close();
socket.close();
scoket1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果:
服务器即将启动,等待客户端的链接***
开始连接
我服务器,客户端说:用户名:admin;密码:123