import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* 服务器和客户端之间通信依靠socket的getIntputStream和getOutputStream来相互通信
*/
public class Test03 {
@Test
public void Client(){
Socket socket = null;
InetAddress inet = null;
FileInputStream fr = null;
OutputStream os = null;
InputStream isFromServer = null;
try {
inet = InetAddress.getByName("127.1.1");
socket = new Socket(inet,55535);//ip 端口号
fr = new FileInputStream("线路板.jpg");//读取文件
os = socket.getOutputStream();//通过流与服务器之间传输数据
byte[] transBytes = new byte[1024];
int readCount;
while((readCount = fr.read(transBytes))!=-1){
os.write(transBytes,0,readCount);
}
System.out.println("传输完毕");
socket.shutdownOutput();
isFromServer = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();//这样防止文字倍“劈开”
byte[] bytes = new byte[20];
int len;
while((len = isFromServer.read(bytes)) != -1){
// String str = new String(bytes,0,len);
// System.out.println(str);
baos.write(bytes,0,len);
}
System.out.println(baos.toString());
System.out.println("反馈接受完毕");
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
isFromServer.close();
} catch (IOException e) {
e.printStackTrace();
}
// try {
// baos.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
}
}
@Test
public void Server(){
ServerSocket ss = null;
Socket socket = null;//用来接收ss.accept()返回的socekt
InputStream is = null;//用来接受传输文件的读取流
FileOutputStream fos = null;//用来在本地新建一个文件
//新建一个用来发送信息的流
OutputStream fosForClient = null;
try {
ss = new ServerSocket(55535);//创建服务器套接字
socket = ss.accept();//接受文件传输用的套接字
is = socket.getInputStream();//读入传输文件的流
fos = new FileOutputStream("myPictureHaHa.jpg");
fosForClient = socket.getOutputStream();//给客户端反馈
int readCount;
byte[] transBytes = new byte[1024];
while ((readCount = is.read(transBytes))!=-1){
fos.write(transBytes,0,readCount);
}
System.out.println("保存成功");
fosForClient.write("收到,您的电路板构造很精妙".getBytes());
System.out.println("反馈输出完毕");
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fosForClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
java实现网络通信小demo(src.zip
需积分: 9 184 浏览量
2021-11-18
17:06:53
上传
评论
收藏 3KB ZIP 举报
目送飞鸟
- 粉丝: 6
- 资源: 2
最新资源
- 论文(最终)_20240430235101.pdf
- 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0