这是我参与更文挑战的第6天,活动详情查看: 更文挑战
本文正在参加「Java主题月 - Java 开发实战」,详情查看 活动链接
相关文章
Java网络编程:Java网络编程
一、TCP实现发送消息
客户端代码:
/**
*客户端
*/
public class TcpClientDemo {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
//首先得知道服务端的地址和端口号
//这里记得异常不要抛出去,捕获起来方便处理
try {
//服务器地址,用InetAddress来接收
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
//端口号
int port = 7777;
//创建一个套接字链接(socket)
socket = new Socket(inetAddress,port);
//发送消息的时候要以io流的方式来发送,发送消息肯定是输出流啦
os = socket.getOutputStream();
//创建Scanner对象,接受从控制台输入
Scanner input=new Scanner(System.in);
//接受String类型
String messge = input.next();
//发送消息
os.write(messge.getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
e.printStackTrace();
}finally {
//这里是重点!!!千万不要忘记关闭链接和流
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
复制代码
服务端代码:
/**
*服务端
*/
public class TcpServerDemo {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream byteArrayOutputStream = null;
//首先建立一个服务端
try {
//1,ip地址
serverSocket = new ServerSocket(7777);
while (true) {
//2、等待客户端连接
socket = serverSocket.accept();
//3.消息过来了我们得读取客户消息,接受消息肯定是输入流啦
is = socket.getInputStream();
//这个时候消息已经接受到了,我们怎么样读取呢?
//管道输出流,ByteArrayOutputStream
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];//字节数组来接受
int length;//长度
while ((length = is.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, length);
}
System.out.println(byteArrayOutputStream.toString());
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//同样的,得关闭流
if (serverSocket != null){
try {
serverSocket.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 (byteArrayOutputStream != null){
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
复制代码
客户端执行结果:
服务端接受结果:
总结: 看代码说话!
-
客户端
- 连接服务器Socket
- 发送消息
-
服务端
- 建立服务的端口ServerScoket
- 等待用户的连接accept
- 接收用户的消息
二、TCP实现文件上传
- 客户端代码
/**
* 客户端
*/
public class TcpUplodeFileClient {
public static void main(String[] args) throws Exception{
//1、创建一个Socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 7777);
//2、创建一个输出流
OutputStream os = socket.getOutputStream();
//3、读取文件
FileInputStream fis = new FileInputStream(new File("C:\\Users\\大大丁大大\\Desktop\\有用文件\\test.jpg"));
//4、写出文件
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
//通知服务器,我已经结束了
socket.shutdownOutput(); //我已经传输完了!
//确定服务器接收完毕,才能够断开连接
InputStream inputStream = socket.getInputStream();
//String byte[]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2;
while ((len2 = inputStream.read(buffer2)) != -1) {
baos.write(buffer2, 0, len2);
}
System.out.println(baos.toString());
//5、关闭资源
baos.close();
inputStream.close();
fis.close();
os.close();
socket.close();
}
}
复制代码
- 服务端代码
/**
* 服务端
*/
public class TcpUplodeFileServer {
public static void main(String[] args) throws Exception{
//创建服务
ServerSocket serverSocket = new ServerSocket(7777);
//监听连接
Socket socket = serverSocket.accept();
//获取输入流
InputStream is = socket.getInputStream();
//文件输出
FileOutputStream fos = new FileOutputStream(new File("test.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
//通知客户端接收完毕
OutputStream os = socket.getOutputStream();
os.write("接收完成".getBytes());
//千万记得关闭所有资源
serverSocket.close();
socket.close();
is.close();
os.close();
}
}
复制代码
- 客户端执行结果:
- 服务端执行结果
路漫漫其修远兮,吾必将上下求索~
如果你认为i博主写的不错!写作不易,请点赞、关注、评论给博主一个鼓励吧~hahah
近期评论