服务端:
package day3_2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class TestServer {
static void server(){
new Thread(){
@Override
public void run() {
ServerSocket ss = null;
try {
ss = new ServerSocket(8000);
while (true) {
Socket socket = ss.accept();
ServerThread st = new TestServer(). new ServerThread(socket);
st.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
public static void main(String[] args) throws Exception {
server();
}
class ServerThread extends Thread {
Socket socket;
ServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
BufferedReader br = null;
PrintWriter bw = null;
try {
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
br = new BufferedReader(new InputStreamReader(in));
bw = new PrintWriter(new OutputStreamWriter(out));
String str = "";
while ((str = br.readLine()) != null) { //这里的null是不会被读取到的
System.out.println(str);
bw.println("server:" + str);
bw.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
br.close();
bw.close();
//socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
客户端:
package day3_2;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class TestClient {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("127.0.0.1", 8000);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
while (true) {
String s = new Scanner(System.in).nextLine();
pw.println(s);
pw.flush();
System.out.println(br.readLine());
if("exit".equals(s)){
//System.exit(0);
break;
}
}
socket.close();
}
}
运行结果:
服务端:
客户端:
注意!
服务端:
package day03;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Lab1 {
public static void main(String[] args) throws IOException {
//1、启动服务器
//port端口号:0~65535,其中0-1024被系统占用
ServerSocket ss = new ServerSocket(8000);
//2、结收客户端发来的数据ctrl+1
Socket socket = ss.accept();
//3、获取socket通道里的io流
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
int b = 0;
//如果客户端不加shutdownOutput();则无法读到-1,循环一直不会结束
while((b = in.read()) != -1){
System.out.print((char)b);
}
out.write("sdfsdf23".getBytes());
out.flush();
//关闭资源
socket.close();
ss.close();
in.close();
out.close();
}
}
客户端:
package day03;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class Lab1_1 {
public static void main(String[] args) throws IOException {
//1、连接服务端
//指定服务器ip和端口
Socket s = new Socket("127.0.0.1",8000);
//获取连接通道的io流
OutputStream out = s.getOutputStream();
InputStream in = s.getInputStream();
//2、给服务器发送hello
out.write("fhfghg".getBytes());
out.flush();
s.shutdownOutput(); //下面讲解
int b = 0;
while((b = in.read()) != -1){
System.out.print((char)b);
}
//释放资源
s.close();
in.close();
out.close();
}
}
在客户端中没有 socket.shutdownOutput();这行代码的时候,服务器和客户端将一直处于僵持状态,因为,服务器端一直在等待客户端发来的消息,但是一直不知道客户端什么时候会结束发送消息,因为,客户端并没有给用户发过来一个结束的标志,所以,客户端将一直处于等待的状态,所以将不会再给客户端发消息,即执行不下去了。
socket.shutdownOutput();//关闭客户端的输出流。相当于给流中加入一个结束标志-1,这个时候服务器端的输入流的readUTF()相当于读到了一个-1,所以,就会结束readUTF()方法,接着往下执行。
当然你也可以理解为,该socket.shutdownOutput(); 是关闭的客户端的输出流,那么服务器端的输入流也就随之关闭了。