博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TCP网络编程
阅读量:3964 次
发布时间:2019-05-24

本文共 9911 字,大约阅读时间需要 33 分钟。

代码示例1:

实现TCP的网络编程

例子1:客户端发送信息给服务端,服务端将数据显示在控制台上

因为只自己的电脑,所以都是自己连自己

使用is.close()、os.close()、br.close()等都会造成socket被关闭
*/

public class TCPTest1 {
@Test public void client(){
Socket socket = null; OutputStream os = null; try {
//1.创建Socket对象,指明服务器端的ip和端口号 socket = new Socket("127.0.0.1",8866); //2.获取一个输出流,用于输出数据 os = socket.getOutputStream(); //3.写出数据的操作 os.write("我是客户端!".getBytes()); } catch (IOException e) {
e.printStackTrace(); } finally {
if(os != null){
//4.资源的关闭 try {
os.close(); } catch (IOException e) {
e.printStackTrace(); } } if(socket != null){
try {
socket.close(); } catch (IOException e) {
e.printStackTrace(); } } } } @Test public void server(){
ServerSocket ss = null; Socket socket = null; InputStream is = null; ByteArrayOutputStream baos = null; try {
//1.创建服务器端的ServerSocket,指明自己的端口号 ss = new ServerSocket(8866); //2.调用accept()表示接收来自于客户端的socket socket = ss.accept(); //3.获取输入流 is = socket.getInputStream(); //4.读取输入流中的数据 //不建议这样写,可能会乱码,字节流传输中文可能出现乱码// byte[] buffer = new byte[5];// int len;// while((len = is.read(buffer)) != null){
// String str = new String(buffer,0,len);// System.out.println(str);// } baos = new ByteArrayOutputStream(); byte[] buffer = new byte[5]; int len; while((len = is.read(buffer)) != -1){
baos.write(buffer,0,len); } System.out.println(baos.toString()); System.out.println("收到了来自于:" + socket.getInetAddress().getHostAddress() + "的数据"); } catch (IOException e) {
e.printStackTrace(); } finally {
//5.关闭资源 if(baos != null){
try {
baos.close(); } catch (IOException e) {
e.printStackTrace(); } } if(is != null){
try {
is.close(); } catch (IOException e) {
e.printStackTrace(); } } if(socket != null){
try {
socket.close(); } catch (IOException e) {
e.printStackTrace(); } } if(ss != null){
try {
ss.close(); } catch (IOException e) {
e.printStackTrace(); } } } }}

代码示例2:

实现TCP的网络编程

例题2:客户端发送文件给服务端,服务端将文件保存在本地。

public class TCPTest1 {
@Test public void client(){
Socket socket = null; OutputStream os = null; try {
//1.创建Socket对象,指明服务器端的ip和端口号 socket = new Socket("127.0.0.1",8866); //2.获取一个输出流,用于输出数据 os = socket.getOutputStream(); //3.写出数据的操作 os.write("我是客户端!".getBytes()); } catch (IOException e) {
e.printStackTrace(); } finally {
if(os != null){
//4.资源的关闭 try {
os.close(); } catch (IOException e) {
e.printStackTrace(); } } if(socket != null){
try {
socket.close(); } catch (IOException e) {
e.printStackTrace(); } } } } @Test public void server(){
ServerSocket ss = null; Socket socket = null; InputStream is = null; ByteArrayOutputStream baos = null; try {
//1.创建服务器端的ServerSocket,指明自己的端口号 ss = new ServerSocket(8866); //2.调用accept()表示接收来自于客户端的socket socket = ss.accept(); //3.获取输入流 is = socket.getInputStream(); //4.读取输入流中的数据 //不建议这样写,可能会乱码,字节流传输中文可能出现乱码// byte[] buffer = new byte[5];// int len;// while((len = is.read(buffer)) != null){
// String str = new String(buffer,0,len);// System.out.println(str);// } baos = new ByteArrayOutputStream(); byte[] buffer = new byte[5]; int len; while((len = is.read(buffer)) != -1){
baos.write(buffer,0,len); } System.out.println(baos.toString()); System.out.println("收到了来自于:" + socket.getInetAddress().getHostAddress() + "的数据"); } catch (IOException e) {
e.printStackTrace(); } finally {
//5.关闭资源 if(baos != null){
try {
baos.close(); } catch (IOException e) {
e.printStackTrace(); } } if(is != null){
try {
is.close(); } catch (IOException e) {
e.printStackTrace(); } } if(socket != null){
try {
socket.close(); } catch (IOException e) {
e.printStackTrace(); } } if(ss != null){
try {
ss.close(); } catch (IOException e) {
e.printStackTrace(); } } } }}

代码示例3:

实现TCP的网络编程

例题3:从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。并关闭相应的连接。

public class TCPTest3 {
@Test public void client(){
Socket socket = null; OutputStream os = null; FileInputStream fis = null; InputStream is = null; ByteArrayOutputStream baos = null; try {
socket = new Socket("127.0.0.1",4455); os = socket.getOutputStream(); fis = new FileInputStream("灿烈.jpg"); byte[] buffer = new byte[20]; int len; while((len = fis.read(buffer)) != -1){
os.write(buffer,0,len); } //关闭数据的输出 socket.shutdownOutput(); //5.接收来自于服务器端的数据,并显示到控制台上 is = socket.getInputStream(); baos = new ByteArrayOutputStream(); byte[] buffer2 = new byte[5]; int len2; while((len2 = is.read(buffer2)) != -1){
baos.write(buffer2,0,len2); } System.out.println(baos.toString()); } catch (IOException e) {
e.printStackTrace(); } finally {
if(fis != null){
try {
fis.close(); } catch (IOException e) {
e.printStackTrace(); } } if(os != null){
try {
os.close(); } catch (IOException e) {
e.printStackTrace(); } } if(socket != null){
try {
socket.close(); } catch (IOException e) {
e.printStackTrace(); } } if(is != null){
try {
is.close(); } catch (IOException e) {
e.printStackTrace(); } } if(baos != null){
try {
baos.close(); } catch (IOException e) {
e.printStackTrace(); } } } } @Test public void server(){
ServerSocket ss = null; Socket socket = null; InputStream is = null; FileOutputStream fos = null; OutputStream os = null; try {
ss = new ServerSocket(4455); socket = ss.accept(); is = socket.getInputStream(); fos = new FileOutputStream("灿烈4.jpg"); byte[] buffer = new byte[20]; int len; while((len = is.read(buffer)) != -1){
fos.write(buffer,0,len); } //6.服务器端给予客户端反馈 os = socket.getOutputStream(); os.write("你好,我已经接收到了你的消息!".getBytes()); } catch (IOException e) {
e.printStackTrace(); } finally {
if(fos != null){
try {
fos.close(); } catch (IOException e) {
e.printStackTrace(); } } if(is != null){
try {
is.close(); } catch (IOException e) {
e.printStackTrace(); } } if(socket != null){
try {
socket.close(); } catch (IOException e) {
e.printStackTrace(); } } if(ss != null){
try {
ss.close(); } catch (IOException e) {
e.printStackTrace(); } } if(os != null){
try {
os.close(); } catch (IOException e) {
e.printStackTrace(); } } } }}

转载地址:http://lvuki.baihongyu.com/

你可能感兴趣的文章
比较字符串
查看>>
Java EE 精萃
查看>>
Open Source 精萃
查看>>
Java EE 简介
查看>>
Weblogic 简介
查看>>
观察者模式 (Observer)
查看>>
Java 集合框架
查看>>
Weblogic 精萃
查看>>
Servlet 精萃
查看>>
XStream 精萃
查看>>
XStream 环境设置
查看>>
Git 分支
查看>>
Git 冲突
查看>>
Git Merging vs. Rebasing
查看>>
[第9课] 箱线图
查看>>
[第10课] 箱线图2
查看>>
[第11课]统计:集中趋势
查看>>
[第12课] 统计:样本和总体
查看>>
[第13课] 统计:总体方差
查看>>
[第14课] 统计:样本方差
查看>>