1、TCP-上传文件
来大家接着看屏幕,昨天呢,我们说了一下这个利用网络进行文件的拷贝,
从我这边拷到你那边去,或从你那边拷到我这边来,那么接下来我们说一下图片的问题。
我们这边会涉及到什么,多人上传的一个问题。
来,先演示一下这个单人。上传图片我们分几步来完成,
为了保证图片的数据,我们是不是可以用TCP传输。
第一个叫做客户端第二个叫做服务端,客户端需要做哪些事情呢,大家。
第一步叫做创建码头。
第二读取客户端已有的图片数据。
第三步通过socket输出流,将数据发给服务端。
第四步读取服务端反馈信息。
第五步关闭。
/*
需求:上传图片。
*/
import java.net.*;
import java.io.*;
class PicClient{
public static void main(String[] args)throws IOException{
//1,创建码头
Socket s=new Socket("10.2.172.93",10004);
//2,确定输入数据流
BufferedReader bufr=new BufferedReader(new FileReader("1.jpg"));
//FileInputStream fis=new FileInputStream("1.jpg");
//3,定义发送数据流
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
//OutputStream out=s.getOutputStream();
String line=null;
while((line=bufr.readLine())!=null){
out.println(line);
}
/*byte[] buf=new byte[1024];
int len=0;
while((len=fis.read(buf))!=-1){
out.write(buf,0,len);
}*/
s.shutdownOutput(); //关闭客户端的输出流,相当于给流中加入一个结束标记-1。
//4,定义接收数据流
BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=bufIn.readLine();
sop(str);
/*InputStream in=s.getInputStream();
byte[] bufIn=new byte[1024];
int num=in.read(bufIn);
sop(new String(bufIn,0,num));*/
//5,关闭
bufr.close();
//fis.close();
s.close();
}
public static void sop(Object obj){
System.out.println(obj);
}
}
class PicServer{
public static void main(String[] args)throws IOException{
//1,创建码头
ServerSocket ss=new ServerSocket(10004);
Socket s=ss.accept();
//2,确定输入数据流
BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
//InputStream in=s.getInputStream();
//3,定义写入数据流
PrintWriter out=new PrintWriter(new FileWriter("dd.jpg"),true);
//FileOutputStream fos=new FileOutputStream("dd.jpg");
String line=null;
while((line=bufIn.readLine())!=null){
out.println(line);
}
/*byte[] buf=new byte[1024];
int len=0;
while((len=in.read(buf))!=-1){
fos.write(buf,0,len);
}*/
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println("上传成功");
//5,关闭
out.close();
//fos.close();
ss.close();
s.close();
}
public static void sop(Object obj){
System.out.println(obj);
}
}
2,、TCP-客户端并发上传图片
来大家接着看啊,刚才上传图片的例子搞定了,
但是呢我是不是只能自娱自乐啊,能不能和大家互动一下。
在服务端这里搞一个while(true)就可以了吧
/*
需求:并发上传图片。
*/
import java.net.*;
import java.io.*;
class PicClient{
public static void main(String[] args)throws IOException{
if(args.length!=1){
sop("请选择一个Jpg格式的图片");
return ;
}
File file=new File(args[0]);
if(!(file.exists()&&file.isFile())){
sop("该文件有问题,要么不存在,要么不是文件");
return ;
}
if(!(file.getName().endsWith(".jpg"))){
sop("图片格式错误,请重新选择");
return ;
}
if(file.length()>1024*1024*6){
sop("文件过大,没安好心");
return ;
}
//1,创建码头
Socket s=new Socket("10.2.172.93",10004);
//2,确定输入数据流
FileInputStream fis=new FileInputStream(file);
//3,定义发送数据流
OutputStream out=s.getOutputStream();
byte[] buf=new byte[1024];
int len=0;
while((len=fis.read(buf))!=-1){
out.write(buf,0,len);
}
s.shutdownOutput(); //关闭客户端的输出流,相当于给流中加入一个结束标记-1。
//4,定义接收数据流
InputStream in=s.getInputStream();
byte[] bufIn=new byte[1024];
int num=in.read(bufIn);
sop(new String(bufIn,0,num));
//5,关闭
fis.close();
s.close();
}
public static void sop(Object obj){
System.out.println(obj);
}
}
class PicThread implements Runnable{
private Socket s;
PicThread(Socket s){
this.s=s;
}
public void run(){
String ip=s.getInetAddress().getHostAddress();
try{
int i=1;
sop(ip+"....connected");
//2,确定输入数据流
InputStream in=s.getInputStream();
//3,定义写入数据流
File file=new File(ip+"("+i+")"+".jpg");
while(file.exists())
file=new File(ip+"("+(i++)+")"+".jpg");
FileOutputStream fos=new FileOutputStream(file);
byte[] buf=new byte[1024];
int len=0;
while((len=in.read(buf))!=-1){
fos.write(buf,0,len);
}
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println("上传成功");
//5,关闭
fos.close();
s.close();
}catch(Exception e){
throw new RuntimeException(ip+"上传失败");
}
}
public static void sop(Object obj){
System.out.println(obj);
}
}
class PicServer{
public static void main(String[] args)throws IOException{
//1,创建码头
ServerSocket ss=new ServerSocket(10004);
while(true){
Socket s=ss.accept();
new Thread(new PicThread(s)).start();
}
//ss.close();
}
public static void sop(Object obj){
System.out.println(obj);
}
}
3、TCP-客户端并发登录
上面我们大概知道了客户端并发上传文件,下面我们进一步客户端并发登录
/*
客户端通过键盘录入用户名。
服务端对这个用户名进行校验。
如果该用户存在,在服务端显示xxx,已登录。
并在客户端显示xxx,欢迎光临。
如果该用户存在,在服务端显示xxx,尝试登陆。
并在客户端显示xxx,该用户不存在。
最多登陆三次。
*/
import java.io.*;
import java.net.*;
class LoginClient{
public static void main(String[] args)throws IOException{
//1,创建码头
Socket s=new Socket("10.2.172.93",10005);
//2,确定输入数据流
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
//3,定义发送数据流
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
//4,定义服务端反馈数据流
BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
for(int x=0;x<3;x++){
String line=bufr.readLine();
if(line==null)
break;
out.println(line);
String info=bufIn.readLine();
sop("info:"+info);
if(info.contains("欢迎"))
break;
}
bufr.close();
s.close();
}
public static void sop(Object obj){
System.out.println(obj);
}
}
class UserThread implements Runnable{
private Socket s;
UserThread(Socket s){
this.s=s;
}
public void run(){
String ip=s.getInetAddress().getHostAddress();
sop(ip+".....connected");
try{
for(int x=0;x<3;x++){
//2,确定输入数据流
BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
String name=bufIn.readLine();
//3,定义发送数据流
BufferedReader bufr=new BufferedReader(new FileReader("user.txt"));
//4,定义服务端反馈数据流
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
boolean flag=false;
String line=null;
while((line=bufr.readLine())!=null){
if(line.equals(name)){
flag=true;
break;
}
}
if(flag){
sop(name+",已登录");
out.println(name+",欢迎光临");
break;
}else{
sop(name+",尝试登录");
out.println(name+",用户名不存在");
}
}
s.close();
}catch(Exception e){
throw new RuntimeException(ip+"校验失败");
}
}
public static void sop(Object obj){
System.out.println(obj);
}
}
class LoginServer{
public static void main(String[] args)throws Exception{
//1,创建码头
ServerSocket ss=new ServerSocket(10005);
while(true){
Socket s=ss.accept();
new Thread(new UserThread(s)).start();
}
}
}