黑马程序员 Java学习笔记 (day11)
2014-09-03 22:02
302 查看
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
好久没有写博客了,分享一下今天做的一道基础测试题:
/*
10、使用TCP协议完成一个客户端一个服务器。客户端从键盘输入读取一个字符串,发送到
服务器。服务器接收客户端发送的字符串,反转之后发回客户端。客户端接收并打印。
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class Test10{//客户端
public static void main(String[] args) {
Socket s = null;
try {
s = new Socket("192.168.1.2",10004);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//定义读取键盘录入的流对象
BufferedReader bufr = new BufferedReader
(new InputStreamReader(System.in));
//将数据写到socket输出流,发给服务端
BufferedWriter bufOut = null;
try {
bufOut = new BufferedWriter
(new OutputStreamWriter(s.getOutputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//定义一个socket读取流,读取服务端返回的信息
BufferedReader bufIn = null;
try {
bufIn = new BufferedReader
(new InputStreamReader(s.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String line =null;
try {
while((line=bufr.readLine())!=null){
// 输入over,程序停止
if("over".equals(line))
break;
bufOut.write(line);
bufOut.newLine();
bufOut.flush();
//接受服务器的数据
String str = bufIn.readLine();
//打印服务器返回的数据
System.out.println(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//关闭资源
try {
bufr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Server{
public static void main(String[] args){
ServerSocket ss = null;
try {
ss = new ServerSocket(10004);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Socket s = null;
try {
s = ss.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+":connected");
//读取socket读取流中的数据
BufferedReader bufIn = null;
try {
bufIn = new BufferedReader
(new InputStreamReader(s.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//socket输出流
BufferedWriter bufOut = null;
try {
bufOut = new BufferedWriter
(new OutputStreamWriter(s.getOutputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//定义StringBuilder容器用于反转
StringBuilder sb = new StringBuilder();
String line = null;
try {
while((line = bufIn.readLine())!=null){
sb.append(line);
//将数据反转之后输出
bufOut.write(sb.reverse().toString());
bufOut.newLine();
bufOut.flush();
sb.delete(0, sb.length());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//关闭资源
try {
s.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这道题我做了很长时间,做的时候思路也有些乱,不停的用dos命令行验证,还好最后做出来了,主要分享以下部分:
StringBuilder sb = new StringBuilder();
String line = null;
try {
while((line = bufIn.readLine())!=null){
sb.append(line);
//将数据反转之后输出
bufOut.write(sb.reverse().toString());
bufOut.newLine();
bufOut.flush();
sb.delete(0, sb.length());
}
因为要反转字符串,所有我用到了StringBuilder容器的reverse()功能,在bufOut.write(sb.reverse().toString())这一块因为忽略了toString()导致编译无法通过,就这么简单的地方,我花了半个小时才弄出来,看来自己的很多基础部分的理解还不够,下面的学习得加紧了。看着一期期的错过,心里真是有些着急。
好久没有写博客了,分享一下今天做的一道基础测试题:
/*
10、使用TCP协议完成一个客户端一个服务器。客户端从键盘输入读取一个字符串,发送到
服务器。服务器接收客户端发送的字符串,反转之后发回客户端。客户端接收并打印。
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class Test10{//客户端
public static void main(String[] args) {
Socket s = null;
try {
s = new Socket("192.168.1.2",10004);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//定义读取键盘录入的流对象
BufferedReader bufr = new BufferedReader
(new InputStreamReader(System.in));
//将数据写到socket输出流,发给服务端
BufferedWriter bufOut = null;
try {
bufOut = new BufferedWriter
(new OutputStreamWriter(s.getOutputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//定义一个socket读取流,读取服务端返回的信息
BufferedReader bufIn = null;
try {
bufIn = new BufferedReader
(new InputStreamReader(s.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String line =null;
try {
while((line=bufr.readLine())!=null){
// 输入over,程序停止
if("over".equals(line))
break;
bufOut.write(line);
bufOut.newLine();
bufOut.flush();
//接受服务器的数据
String str = bufIn.readLine();
//打印服务器返回的数据
System.out.println(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//关闭资源
try {
bufr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Server{
public static void main(String[] args){
ServerSocket ss = null;
try {
ss = new ServerSocket(10004);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Socket s = null;
try {
s = ss.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+":connected");
//读取socket读取流中的数据
BufferedReader bufIn = null;
try {
bufIn = new BufferedReader
(new InputStreamReader(s.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//socket输出流
BufferedWriter bufOut = null;
try {
bufOut = new BufferedWriter
(new OutputStreamWriter(s.getOutputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//定义StringBuilder容器用于反转
StringBuilder sb = new StringBuilder();
String line = null;
try {
while((line = bufIn.readLine())!=null){
sb.append(line);
//将数据反转之后输出
bufOut.write(sb.reverse().toString());
bufOut.newLine();
bufOut.flush();
sb.delete(0, sb.length());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//关闭资源
try {
s.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这道题我做了很长时间,做的时候思路也有些乱,不停的用dos命令行验证,还好最后做出来了,主要分享以下部分:
StringBuilder sb = new StringBuilder();
String line = null;
try {
while((line = bufIn.readLine())!=null){
sb.append(line);
//将数据反转之后输出
bufOut.write(sb.reverse().toString());
bufOut.newLine();
bufOut.flush();
sb.delete(0, sb.length());
}
因为要反转字符串,所有我用到了StringBuilder容器的reverse()功能,在bufOut.write(sb.reverse().toString())这一块因为忽略了toString()导致编译无法通过,就这么简单的地方,我花了半个小时才弄出来,看来自己的很多基础部分的理解还不够,下面的学习得加紧了。看着一期期的错过,心里真是有些着急。
相关文章推荐
- 黑马程序员 java学习笔记 Day11:反射机制详解
- 黑马程序员---- Java学习笔记:1.异常 2.集合
- 黑马程序员 java学习笔记4-Io流学习后可能会用到的实例
- 黑马程序员---java多线程 学习笔记
- 黑马程序员学习笔记四——Java 面向对象 特点之 继承
- 黑马程序员学习笔记之五(java中的泛型)
- 黑马程序员_对于Java高新技术的学习笔记
- 黑马程序员——Java异常处理学习笔记
- 黑马程序员学习笔记之八(Java 反射机制)
- 黑马程序员---java IO-字符流 学习笔记
- 黑马程序员学习笔记六——java.IO小细节或许影响全局。
- 黑马程序员 java学习笔记2-IO流
- 黑马程序员 java学习笔记--正则表达式
- 黑马程序员---java TCP 学习笔记
- 黑马程序员-javaIO学习笔记2
- 黑马程序员 java学习笔记3-IO流之字节流与装饰设计模式
- 黑马程序员 java学习笔记之一(java中this和super关键字的用法)
- 黑马程序员java学习笔记之二(java中的多态性)
- 黑马程序员(学习笔记二)初识java特点
- 黑马程序员-javaIO学习笔记