开篇感言
之前我已经总结了关于定时任务相关的Quartz和数据库结果封装相关的MyBatis源码,从两个领域去了解知识碎片。接下来,我将从Netty走进另一个领域——网络。另外跟网络息息相关的流处理也可以接触一下。
前戏例子
既然要走进网络,总不能两眼一抹黑,最基础的socket编程还是需要了解的,关于BIO、NIO等理论相关知识我这里不做重复造轮,我们撸起袖子直接从实践中来进行探索。
接下来我会根据自己印象列出我一个手写的例子(先说好,这个例子是错误的),我们来看下:
首先我们肯定是先编写服务端例子:
public class SocketServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress("127.0.0.1",9000));
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
String info = null;
while ( (info = bufferedReader.readLine())!=null){
System.out.println(info);
}
PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
printWriter.write("收到回复你");
printWriter.flush();
} catch (IOException e) {
}finally {
}
}
}
接下来我们先把剩下的client端例子也发出来:
public class SocketCient {
public static void main(String[] args) {
try {
Socket socket = new Socket("127.0.0.1",9000);
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(outputStream,"UTF-8"));
printWriter.write("你好");
printWriter.flush();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String info = null;
while ((info = bufferedReader.readLine())!=null){
System.out.println(info);
}
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
OK,所有的实例都在这了,大家可以自己先看下,感觉会不会有问题~~~~
错误实例解析
好了,这里开始答案揭晓。先明确一点,这两个实例跑起来,在控制台是看不到输出的。刚开始的时候很郁闷,明明记得记忆中就是这么简单呀,为什么就不行呢~~ 但是我发现他们连接上之后,关闭掉任意一端,两端都会停止运行,这说明我们的连接是没有问题的,问题出在读取或者发送流数据上。
之后各种试, 效果甚微。查资料得到,原来是出在了细节上面。可以看到在上述中,我们用的是BufferedReader的readLine方法来读取流数据的,这里有个细节是这是读取行数据的,必须要有行结束标志\n,哎,只怪自己对流这块还是不太敏感熟悉。
加上\n之后,我们可以再服务端中收到数据,但是客户端还是没有返回数据。但这时就难不到我了,通过自己分析,感觉是出在读取数据的while上,在这卡主,导致服务端发送不了数据,这时我把while循环一删,果然就OK了,两端都收到了数据。
修改完后的实例
服务端:
public class SocketServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress("127.0.0.1",9000));
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
String info = bufferedReader.readLine();
System.out.println(info);
PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
printWriter.write("收到回复你\n");
printWriter.flush();
printWriter.close();
bufferedReader.close();
} catch (IOException e) {
}finally {
}
}
}
客户端实例:
public class SocketCient {
public static void main(String[] args) {
try {
Socket socket = new Socket("127.0.0.1",9000);
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(outputStream,"UTF-8"));
printWriter.write("你好\n");
printWriter.flush();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String info = bufferedReader.readLine();
System.out.println(info);
bufferedReader.close();
printWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
今日总结
今天完成了一个简单的Socket实例,之前有点眼高手低,感觉这么简单的代码,自己脑中过下就行,果然自己一出手还遇到了这么多坑。以后还是得多亲身尝试,感受才会更多~~~