java
public class SocketTCP04Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket =new ServerSocket(9999);
System.out.println("Server:"+serverSocket.getClass());
System.out.println("正在等待用户连接...");
//当有用户连接时,会返回一个socket对象
Socket socket = serverSocket.accept();
System.out.println("Client:"+serverSocket.getClass());//查看连接到的用户
//接受Client的输入信息
String read =null;
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(inputStream));
while ((read= bufferedReader.readLine())!=null){
System.out.println(read);
}
// String s = bufferedReader.readLine();
// System.out.println(s);
// socket.shutdownInput();
//现在向Client发送数据
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter =new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("Hello, Client 字符流");
bufferedWriter.newLine();//插入换行符,表示回复内容的结束
bufferedWriter.flush();
System.out.println("服务器已关闭~");
bufferedWriter.close();
bufferedReader.close();
socket.close();
serverSocket.close();
}
}
java
public class SocketTCP04Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
System.out.println("获取的Client的socket:" + socket.getClass());
//现在向Server发送信息
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("Hello,server 字符流");
bufferedWriter.newLine();
/**插入换行符后,相当于写入结束,就不用socket.shutdownOutput();
此时对面一定要使用readLine()一次读一行代表读取结束*/
bufferedWriter.flush();
//现在接受Server的信息
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String read = null;
while ((read = bufferedReader.readLine()) != null) {
System.out.println(read);
}
// String s = bufferedReader.readLine();
// System.out.println(s);
socket.shutdownInput();
bufferedReader.close();
bufferedWriter.close();
socket.close();
}
}
此时的服务器还在等待输入,客户端也在等待服务器的应答,产生死锁,僵持不下。
经过一番尝试后发现,
当使用单行读取的时候,使用**bufferedWriter.newLine();此时就不用关闭socket的输出流,**即socket.shutdownOutput();可以省略,当然写上去也没错。
当使用多行读取的时候,此时,必须使用关闭socket的输出流,即socket.shutdownOutput();不能省略!!!
单行接受代码如下:
这是Client:
java
//现在向Server发送信息
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("Hello,server 字符流");
bufferedWriter.newLine();
/**插入换行符后,相当于写入结束,就不用socket.shutdownOutput();
此时对面一定要使用readLine()一次读一行代表读取结束*/
bufferedWriter.flush();
socket.shutdownOutput();//可以省略,加上不为错
这是Server端:
java
//接受Client的输入信息
String read =null;
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(inputStream));
String s = bufferedReader.readLine();
System.out.println(s);
socket.shutdownInput();
多行读取时:
这是Client端:
java
//现在向Server发送信息
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("Hello,server 字符流");
bufferedWriter.newLine();
/**插入换行符后,相当于写入结束,就不用socket.shutdownOutput();
此时对面一定要使用readLine()一次读一行代表读取结束*/
bufferedWriter.flush();
socket.shutdownOutput();//一定不能省略
这是Server端:
java
//接受Client的输入信息
String read =null;
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(inputStream));
while ((read= bufferedReader.readLine())!=null){
System.out.println(read);
}
socket.shutdownInput();//不可省