SocketServer 实现多客户端图片传输



程序实现的是一个读取照片(可改为其文件类型)的服务端,可同时接受多个客户端连接,并且同时接受多张图片的数据。主要是通过多线程控制,每次检测到有请求连接,则开辟一个新线程,新线程的作用是接受图片, 通过不同线程同时运行达到可同时接收多张图片。

1. 这是服务端的源代码:
import java.io.*;
import java.net.*;
public class LoadPicServer {
public static void main(String[] args) throws IOException {
int listen_port = 10005; //监听的端口号
long filecount = 1;
ServerSocket ss = new ServerSocket(listen_port); //监听listen_port端口
if(ss.isBound())
System.out.println("The Server is listenning the port " + listen_port);
while(true) {
Socket s = ss.accept(); //检查是否有连接,该语句是阻塞语句,如果没有则会停在这。
if(s.isConnected()) { //如果有连接则返回true,执行下面语句
String filename = new String("ServerPic" + filecount++ + ".jpg");
System.out.println(s.getInetAddress().getHostAddress()
+ " is connected!"); //获取已连接的客户端的IP
new Thread(new LoadPic(s,filename)).start(); //开启新线程接收图片,主线程继续回去while最开始检查有无连接。
}
}
}
}
/*
* 该类实现Runnable接口,用于实现多线程复制图片。
* 该类的作用就是与主线程传入的Socket连接进行通信,从网络流获取对方的传送的文件数据。
* */
class LoadPic implements Runnable {
Socket s = null;
String filename = null;
BufferedInputStream bufin = null;
BufferedOutputStream bufout = null;
PrintWriter return_txt = null;

public LoadPic(Socket s,String filename) {
this.s = s;
this.filename = filename;
}

public void run() {
try {
bufin = new BufferedInputStream(s.getInputStream()); //获取传入Socket的输入流,用于读取图片数据
bufout = new BufferedOutputStream(new FileOutputStream(filename)); //在当前文件夹创建名为filename的文件,作为输出流的目的。
return_txt = new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true); //该流用来向客户端发送确认信息。
byte[] bufdata = new byte[1024];
int len;
while((len = bufin.read(bufdata)) != -1) { //从输入流读取数据,不为-1,即文件结束则继续读。
bufout.write(bufdata,0,len); //往文件里写数据
bufout.flush();
}

return_txt.println("服务器接收成功!");

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
s.close();
bufin.close();
bufout.close();
return_txt.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


2. 这是客户端源代码,可同时开启多个客户端,做图片并行传输测试。因为多个客户端代码基本一样,所以只需看下面代码即可:


import java.net.*;
import java.io.*;
//该类是客户端类,向服务器段发送请求连接,连接后就可以发送图片数据。
class UpPicClient {
public static void main(String[] args) throws IOException,InterruptedException {
Socket s = new Socket("192.168.1.7",10005);
if(s.isBound()) {
System.out.println("Connect successful!");
BufferedInputStream bufin = new BufferedInputStream(new FileInputStream("1.jpg"));
BufferedOutputStream bufout = new BufferedOutputStream(s.getOutputStream());
BufferedInputStream confirm_txt = new BufferedInputStream(s.getInputStream());
byte[] bufdata = new byte[1024];
int len;
while((len = bufin.read(bufdata)) != -1) {
bufout.write(bufdata,0,len);
bufout.flush();
}
s.shutdownOutput();
System.out.println("发送结束!");
len = confirm_txt.read(bufdata,0,bufdata.length);
String str = new String(bufdata,0,len);
System.out.println(str);
s.close();
bufin.close();
bufout.close();
confirm_txt.close();
}
}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值