文章目录
java实现服务端与客户端的聊天
tcpService服务端
package cn.xjt.ints;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
/**
* @author xujiangtao
* @create 2020-08-04-14:14
*/
public class tcpService {
public static void main(String[] args) throws IOException {
String messageSend; //定义一个字符串
Scanner sc = new Scanner(System.in); //输入函数
ServerSocket server = new ServerSocket(8899); //建立一个服务器网络,指定端口号
Socket client = server.accept(); //接受一个客户端对象的连接,得到客户端对象
if (client != null) {
System.out.println("客户端连接成功");
}
InputStream inputStream = client.getInputStream(); //创建一个输入流
OutputStream outputStream = client.getOutputStream(); //得到输出流
for(int i=0;i<10;i++)
{
new Thread(()->{
byte[] data = new byte[1024]; //定义一个字节数组
int len = 0; //读取数据
try {
len = inputStream.read(data);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("你接受到一条消息:"+new String(data,0,len)); //将数据输出
}).start();
new Thread(()->{
}).start();
messageSend = sc.nextLine(); //从控制台得到字符串
if(messageSend.equals("end"))
{
sc.close();
server.close();
client.close();
}
outputStream.write(messageSend.getBytes()); //将字符串发送
}
}
}
tcpClient客户端
package cn.xjt.ints;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
/**
* @author xujiangtao
* @create 2020-08-04-13:44
*/
public class tcpClient {
public static void main(String[] args) throws IOException {
String messageC;
Scanner sc =new Scanner(System.in);
//建立tcp客户端,指定ip地址,端口号
Socket client = new Socket("192.168.137.1",8899);
OutputStream os = client.getOutputStream(); //得到一个输出流
InputStream inputStream = client.getInputStream(); //得到输入流
for (int i = 0; i < 10; i++) {
new Thread(()->{
byte[] data = new byte[1024];
int leng = 0; //读取数据
try {
leng = inputStream.read(data);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("你接受到一条消息:"+new String(data,0,leng)); //将数据输出
}).start();
messageC=sc.nextLine();
if(messageC.equals("end"))
{
client.close();
}
os.write(messageC.getBytes()); //将字符串写入发送
}
}
}
**
存在部分不足。欢迎讨论修改!
**
java操作文件的上传(第一版)
文件上传服务端ServerFileUpload
package cn.xjt.TcpFileLoad;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author xujiangtao
* @create 2020-08-04-15:10
*/
public class ServerFileUpload {
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream(new File("E:\\AE Folder\\苹果.ai"));
ServerSocket server = new ServerSocket(9999);
System.out.println("等待客户端连接");
Socket client = server.accept();
System.out.println("连接成功");
InputStream serIS = client.getInputStream();
byte[] data = new byte[5000];
int lenght = -1;
System.out.println("数据正在上传...");
// lenght=serIS.read(data);
// System.out.println(lenght);
// fileOutputStream.write(data,0,lenght);
while ((lenght=serIS.read(data))!=-1)
{
if(lenght<data.length)
System.out.println(lenght+"#######################"+lenght);
System.out.println("再循环上传");
fileOutputStream.write(data,0,lenght);
// if(lenght<5000) //判断如果读取的长度小于字节长度,则跳出循环
// {
// break; //因客户端使用结束次套接次的输出流,所以不用加次判断跳出
// }
}
System.out.println("数据已经上传成功");
// serIS.read(data);
// fileOutputStream.write(data);
OutputStream os = client.getOutputStream();
os.write("上传成功".getBytes());
}
}
文件上传客户端
package cn.xjt.TcpFileLoad;
import java.io.*;
import java.net.Socket;
/**
* @author xujiangtao
* @create 2020-08-04-14:51
*/
public class ClientFileUpload {
public static void main(String[] args) throws IOException {
String str = null;
Socket client = new Socket("192.168.137.1",9999);
//得到一个网络输入流
InputStream inputStream = client.getInputStream();
//将读取到的输入流,发送到服务器
OutputStream outputStream = client.getOutputStream();
FileInputStream is =new FileInputStream(new File("E:\\AI练习\\苹果.ai"));
byte[] data = new byte[5000];
int len=-1;
int count =0;
while ((len=is.read(data))!=-1)
{
count++;
outputStream.write(data,0,len);
System.out.println("我一直在循环");
System.out.println(len+"#########################");
}
//上传完文件,给服务器写一个结束标记
client.shutdownOutput(); //禁用次套接字的输出流,结束了
//对于一个TCP套接字,先前写入的数据将被发送后正常关闭TCP连接。如果你向一个socket写入输出流后调用shutdownoutput()插座上,流将抛出一个异常。
System.out.println("我出来了"+count+"次循环");
byte[] upload = new byte[1024];
int leng = inputStream.read(upload);
System.out.println(new String(upload,0,leng));
}
}
java操作文件的上传(优化)
服务端service
package cn.xjt.TcpFileLoadOptimized;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author xujiangtao
* @create 2020-08-04-15:10
*/
public class ServerFileUpload {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(9999);
System.out.println("等待客户端连接");
while (true)
{
String FileName = "xujiangtao"+System.currentTimeMillis()+".jpg";
//服务器上传的地址
FileOutputStream fileOutputStream = new FileOutputStream(new File("E:\\tupian\\"+FileName));
Socket client = server.accept();
System.out.println("连接成功");
//开启多个线程
new Thread(()->{
try {
//连接成功
InputStream serIS = client.getInputStream();
byte[] data = new byte[5000];
int lenght = -1;
System.out.println("数据正在上传...");
while ((lenght=serIS.read(data))!=-1)
{
fileOutputStream.write(data,0,lenght);
}
System.out.println("数据已经上传成功");
//成功回馈
OutputStream os = client.getOutputStream();
os.write("上传成功".getBytes());
}catch (IOException e)
{
System.out.println(e);
}
}).start();
}
}
}
客户端client
package cn.xjt.TcpFileLoadOptimized;
import java.io.*;
import java.net.Socket;
/**
* @author xujiangtao
* @create 2020-08-04-14:51
*/
public class ClientFileUpload {
public static void main(String[] args) throws IOException {
String str = null;
Socket client = new Socket("192.168.0.112",9999);
//得到一个网络输入流
InputStream inputStream = client.getInputStream();
//将读取到的输入流,发送到服务器
OutputStream outputStream = client.getOutputStream();
FileInputStream is =new FileInputStream(new File("E:\\图片\\H2vo2sM061.jpg"));
//发送
byte[] data = new byte[5000];
int len=0;
int count=0;
System.out.println("开始发送数据");
//通过字节流循环的发送数据,当len等于-1的时候,停止循环,发送成功!
while ((len=is.read(data))!=-1)
{
System.out.println(len);
count++;
outputStream.write(data,0,len); //将字节流发送,长度为0到len
System.out.println(count);
}
//上传完文件,给服务器写一个结束标记
client.shutdownOutput(); //禁用次套接字的输出流,结束了
//回收
byte[] upload = new byte[1024];
int leng = inputStream.read(upload);
System.out.println(new String(upload,0,leng));
}
}